Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/generateConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from "path"

import { ApolloConfigFormat } from "apollo-language-server/lib/config"
import { ValidationRule } from "graphql"
import { defaultValidationRules, RelayConfig, RelayCompilerMain } from "./dependencies"
Expand Down Expand Up @@ -53,6 +52,12 @@ export function generateConfig(compat: boolean = false) {

const includesGlobPattern = (inputExtensions: string[]) => `**/*.{graphql,${inputExtensions.join(",")}}`

const configInclude = relayConfig ? relayConfig.include : undefined
const src = (relayConfig || DEFAULTS).src
const includes = configInclude
? configInclude.map(i => path.join(src, i))
: [path.join((relayConfig || DEFAULTS).src, includesGlobPattern(extensions))]

const config: ApolloConfigFormat = {
client: {
service: {
Expand All @@ -70,8 +75,8 @@ export function generateConfig(compat: boolean = false) {
(rule: ValidationRule) => !ValidationRulesToExcludeForRelay.includes(rule.name)
),
],
includes: [directivesFile, path.join((relayConfig || DEFAULTS).src, includesGlobPattern(extensions))],
excludes: relayConfig ? relayConfig.exclude : [],
includes: [directivesFile, ...includes],
excludes: relayConfig && relayConfig.exclude ? relayConfig.exclude.map(ex => path.join(src, ex)) : [],
tagName: "graphql",
},
}
Expand Down
44 changes: 36 additions & 8 deletions tests/generateConfig-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { Config } from "relay-compiler/lib/bin/RelayCompilerMain"
import { LocalServiceConfig } from "apollo-language-server/lib/config"
import { ValidationRule } from "graphql/validation"

const defaultMockedConfig = {
config: {
schema: "path/to/schema.graphql",
src: "path/to/src-root",
} as Config,
}

let mockedConfig: { config: Config } = defaultMockedConfig

function resetToDefaultConfig() {
mockedConfig = defaultMockedConfig
}

jest.mock("cosmiconfig", () => () => ({
searchSync: () => ({
config: {
schema: "path/to/schema.graphql",
src: "path/to/src-root",
exclude: ["path/to/exclude"],
} as Config,
}),
searchSync: () => mockedConfig,
}))

describe(generateConfig, () => {
afterEach(() => {
resetToDefaultConfig()
})
xdescribe("when user does not use relay-config", () => {
it("uses a default schema file", () => {
jest.mock("relay-config", () => ({ loadConfig: () => null }))
Expand All @@ -39,8 +49,14 @@ describe(generateConfig, () => {
})

it("specifies the source files to exclude", () => {
mockedConfig = {
config: {
...defaultMockedConfig.config,
exclude: ["path/to/exclude"],
},
}
const config = generateConfig().config.client!
expect(config.excludes).toContain("path/to/exclude")
expect(config.excludes).toContain("path/to/src-root/path/to/exclude")
})

it("excludes validation rules that are incompatible with Relay", () => {
Expand All @@ -60,5 +76,17 @@ describe(generateConfig, () => {
expect(config.includes).toContainEqual(expect.stringMatching(/relay-compiler-directives-v\d\.\d\.\d/))
})

it("Respects includes", () => {
mockedConfig = {
config: {
...defaultMockedConfig.config,
include: ["some/files/to/include/**"],
},
}

const config = generateConfig().config.client!
expect(config.includes).toContain("path/to/src-root/some/files/to/include/**")
})

it.todo("specifies the source files to include with a different language plugin")
})