Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schema and resolvers finder #152

Open
wants to merge 1 commit into
base: feat/v3
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
npm-debug*
yarn-*
.tmp
.idea
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
"fbemitter": "2.1.1",
"find-config": "1.0.0",
"flow-runtime": "0.17.0",
"glob": "7.1.3",
"glob-parent": "3.1.0",
"globby": "8.0.1",
"graphql": "0.13.2",
"graphql-language-service-interface": "1.1.0",
"graphql-language-service-parser": "1.1.0",
"graphql-resolvers-finder": "0.0.7-alpha-5aaa6c6.0",
"import-from": "2.1.0",
"invariant": "2.2.4",
"json5": "1.0.1",
Expand Down
5 changes: 5 additions & 0 deletions src/gql-config/GQLConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class GQLConfig {
return this._configFileResolved.query;
}

// return resolver config
getResolverConfig(): $PropertyType<GQLConfigFileResolved, 'resolver'> {
return this._configFileResolved.resolver;
}

// returns config for given file path
// file can be schema file or any query file
getFileConfig(filePath: string): SchemaFileConfig | QueryFileConfig | null {
Expand Down
137 changes: 70 additions & 67 deletions src/gql-config/resolveConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,76 +37,79 @@ export default function resolveConfigFile(
configFilePath,
);

const schemaConfigResolved = {
files: config.schema.files,
validate: resolveValidateConfig(schemaConfig.validate),
extendSchema: schemaConfig.extendSchema,
parser: [
SchemaParserClass,
{
// NOTE: preset should come first
...schemaConfig.parserOptions,
...schemaParserOptions,
},
],
...(config.schema.graphQLOptions
? { graphQLOptions: config.schema.graphQLOptions }
: {}),
};

if (!config.query) {
return {
schema: schemaConfigResolved,
version: config.version,
};
}

return {
schema: schemaConfigResolved,
query: {
files: config.query.files.map(({ match, parser, presets, validate }) => {
// load presets packages
const queryPresets = loadQueryPresets(presets, configFilePath);
const queryPresetInline = {
name: 'inline',
parser,
validate,
};
// merge presets to generate config
const presetConfig = mergeQueryPresets([
...queryPresets,
queryPresetInline,
]);
schema: {
files: config.schema.files,
validate: resolveValidateConfig(schemaConfig.validate),
extendSchema: schemaConfig.extendSchema,
parser: [
SchemaParserClass,
{
// NOTE: preset should come first
...schemaConfig.parserOptions,
...schemaParserOptions,
},
],
...(config.schema.graphQLOptions
? { graphQLOptions: config.schema.graphQLOptions }
: {}),
},
...(config.query && {
query: {
files: config.query.files.map(
({ match, parser, presets, validate }) => {
// load presets packages
const queryPresets = loadQueryPresets(presets, configFilePath);
const queryPresetInline = {
name: 'inline',
parser,
validate,
};
// merge presets to generate config
const presetConfig = mergeQueryPresets([
...queryPresets,
queryPresetInline,
]);

const [ParserClass, parserOptions] = loadQueryParser(
// NOTE: settings in .gqlconfig overrides preset
presetConfig.parser,
configFilePath,
);
const [ParserClass, parserOptions] = loadQueryParser(
// NOTE: settings in .gqlconfig overrides preset
presetConfig.parser,
configFilePath,
);

return {
match,
validate: resolveValidateConfig(
presetConfig.validate,
presetConfig.__overrides
? presetConfig.__overrides.ValidationContext
: null,
),
extendSchema: presetConfig.extendSchema,
fragmentScopes: normalizeFragmentScopes(presetConfig.fragmentScopes),
parser: [
ParserClass,
{
...presetConfig.parserOptions,
...parserOptions,
},
],
QueryContext: presetConfig.__overrides
? presetConfig.__overrides.QueryContext
: null,
};
}),
},
return {
match,
validate: resolveValidateConfig(
presetConfig.validate,
presetConfig.__overrides
? presetConfig.__overrides.ValidationContext
: null,
),
extendSchema: presetConfig.extendSchema,
fragmentScopes: normalizeFragmentScopes(
presetConfig.fragmentScopes,
),
parser: [
ParserClass,
{
...presetConfig.parserOptions,
...parserOptions,
},
],
QueryContext: presetConfig.__overrides
? presetConfig.__overrides.QueryContext
: null,
};
},
),
},
}),
...(config.resolver && {
resolver: {
files: config.resolver.files,
baseDir: config.resolver.baseDir,
},
}),
version: config.version,
};
}
Expand Down
12 changes: 12 additions & 0 deletions src/gql-config/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type GQLConfigFile = {|
query?: {
files: Array<QueryConfig>,
},
resolver?: ResolverConfig,
version?: SemverVersion,
|};

Expand All @@ -50,11 +51,17 @@ export type QueryConfig = {|
fragmentScopes?: Array<FragmentScope>,
|};

export type ResolverConfig = {|
files: FileMatchConfig,
baseDir: string,
|};

export type GQLConfigFileResolved = {|
schema: SchemaConfigResolved,
query?: {
files: Array<QueryConfigResolved>,
},
resolver?: ResolverConfigResolved,
version: ?SemverVersion,
|};

Expand All @@ -75,6 +82,11 @@ export type QueryConfigResolved = {|
QueryContext?: ?IQueryContext,
|};

export type ResolverConfigResolved = {|
files: FileMatchConfig,
baseDir: string,
|};

export type GraphQLOptions = {|
commentDescriptions?: boolean,
allowLegacySDLEmptyFields?: boolean,
Expand Down
24 changes: 22 additions & 2 deletions src/gql-schema-service/GQLSchemaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import presetExtendSchema from 'gql-shared/presetExtendSchema';
// commands
import findRefsOfTokenAtPosition from './commands/findRefsOfTokenAtPosition';
import getDefinitionAtPosition from './commands/getDefinitionAtPosition';
import getLenses from './commands/getLenses';
import getHintsAtPosition from './commands/getHintsAtPosition';
import getInfoOfTokenAtPosition from './commands/getInfoOfTokenAtPosition';

Expand All @@ -39,16 +40,20 @@ import GQLConfig from 'gql-config';
import { type SchemaConfigResolved } from 'gql-config/types';
import { type WatchFile } from 'gql-watcher';
import invariant from 'invariant';
import { type FileMatchConfig } from '../gql-config/types';

type Options = {|
config: GQLConfig,
watcher: GQLWatcher,
|};

type CommandParams = {
filePath: string,
fileContent: string,
fileOptions: SchemaConfigResolved,
fileOptions?: SchemaConfigResolved,
position: GQLPosition,
resolversGlob?: FileMatchConfig,
resolversBaseDir?: string,
};

type ParsedSchemaFile = $ReadOnly<{
Expand Down Expand Up @@ -115,10 +120,25 @@ export default class GQLSchemaService extends GQLBaseService {
getDefinitionAtPosition(params: CommandParams): ?GQLLocation {
return this._catchThrownErrors(() => {
return getDefinitionAtPosition({
parser: createParser(params.fileOptions.parser),
parser: params.fileOptions
? createParser(params.fileOptions.parser)
: null,
schema: this.getSchema(),
sourceText: params.fileContent,
position: params.position,
filePath: params.filePath,
resolversGlob: params.resolversGlob,
resolversBaseDir: params.resolversBaseDir,
});
}, null);
}

getLenses(params: CommandParams): ?GQLLocation {
return this._catchThrownErrors(() => {
return getLenses({
schema: this.getSchema(),
filePath: params.filePath,
type: params.filePath.endsWith('graphql') ? 'schema' : 'resolver',
});
}, null);
}
Expand Down
Loading