Skip to content

Commit

Permalink
Use noLocation instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 21, 2020
1 parent f6f616d commit 5e537bf
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 22 deletions.
4 changes: 3 additions & 1 deletion packages/import/src/index.ts
Expand Up @@ -102,7 +102,9 @@ function visitFile(
const fileDefinitionMap = new Map<string, Set<DefinitionNode>>();
// To prevent circular dependency
visitedFiles.set(filePath, fileDefinitionMap);
const document = parse(new Source(otherLines, filePath));
const document = parse(new Source(otherLines, filePath), {
noLocation: true,
});
for (const definition of document.definitions) {
if ('name' in definition || definition.kind === Kind.SCHEMA_DEFINITION) {
const definitionName = 'name' in definition ? definition.name.value : 'schema';
Expand Down
5 changes: 2 additions & 3 deletions packages/load/src/load-typedefs/collect-sources.ts
@@ -1,5 +1,5 @@
import { Source, isDocumentString, parseGraphQLSDL, asArray, printSchemaWithDirectives } from '@graphql-tools/utils';
import { isSchema, Kind, parse } from 'graphql';
import { isSchema, Kind } from 'graphql';
import isGlob from 'is-glob';
import { LoadTypedefsOptions } from '../load-typedefs';
import { loadFile, loadFileSync } from './load-file';
Expand Down Expand Up @@ -294,9 +294,8 @@ function addResultOfCustomLoader({
if (isSchema(result)) {
addSource({
source: {
location: pointer,
schema: result,
document: parse(printSchemaWithDirectives(result)),
...parseGraphQLSDL(pointer, printSchemaWithDirectives(result), {}),
},
pointer,
noCache: true,
Expand Down
5 changes: 4 additions & 1 deletion packages/load/src/load-typedefs/parse.ts
Expand Up @@ -67,7 +67,10 @@ function parseSchema(input: Input) {

function parseRawSDL(input: Input) {
if (input.source.rawSDL) {
input.source.document = parse(new GraphQLSource(input.source.rawSDL, input.source.location), input.options);
input.source.document = parse(new GraphQLSource(input.source.rawSDL, input.source.location), {
noLocation: true,
...input.options,
});
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/loaders/code-file/src/exports.ts
Expand Up @@ -60,7 +60,9 @@ function resolveExport(
}

if (isSchemaText(fileExport)) {
return parse(fileExport);
return parse(fileExport, {
noLocation: true,
});
}

if (isWrappedSchemaJson(fileExport)) {
Expand Down
15 changes: 12 additions & 3 deletions packages/merge/src/typedefs-mergers/merge-typedefs.ts
Expand Up @@ -121,9 +121,13 @@ export function mergeGraphQLTypes(
const allNodes: ReadonlyArray<DefinitionNode> = types
.map<DocumentNode>(type => {
if (isSchema(type)) {
return parse(printSchemaWithDirectives(type));
return parse(printSchemaWithDirectives(type), {
noLocation: true,
});
} else if (isStringTypes(type) || isSourceTypes(type)) {
return parse(type);
return parse(type, {
noLocation: true,
});
}

return type;
Expand Down Expand Up @@ -179,5 +183,10 @@ export function mergeGraphQLTypes(
return Object.values(mergedNodes);
}

return [...Object.values(mergedNodes), parse(schemaDefinition).definitions[0]];
return [
...Object.values(mergedNodes),
parse(schemaDefinition, {
noLocation: true,
}).definitions[0],
];
}
4 changes: 2 additions & 2 deletions packages/merge/tests/extract-extensions-from-schema.spec.ts
Expand Up @@ -132,7 +132,7 @@ describe('extensions', () => {
expect(cleanSchema.getType('MyInput').extensions).toBeUndefined();

const modifiedSchema = applyExtensions(cleanSchema, result);

expect(modifiedSchema.extensions).toEqual({ schema: true })
expect(modifiedSchema.getType('MyInput').extensions).toEqual({ input: true });
expect(modifiedSchema.getType('MyType').extensions).toEqual({ type: true });
Expand All @@ -146,4 +146,4 @@ describe('extensions', () => {
expect(modifiedSchema.getQueryType().getFields().t.args[0].extensions).toEqual({ fieldArg: true });
});
})
});
});
4 changes: 4 additions & 0 deletions packages/relay-operation-optimizer/src/index.ts
Expand Up @@ -20,6 +20,10 @@ export function optimizeDocuments(
documents: DocumentNode[],
options: OptimizeDocumentsOptions = {}
) {
options = {
noLocation: true,
...options,
};
// @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
// Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
// transformASTSchema creates a new schema instance instead of mutating the old one
Expand Down
6 changes: 5 additions & 1 deletion packages/schema/src/buildSchemaFromTypeDefinitions.ts
Expand Up @@ -29,8 +29,12 @@ export function isDocumentNode(typeDefinitions: ITypeDefinitions): typeDefinitio

export function buildDocumentFromTypeDefinitions(
typeDefinitions: ITypeDefinitions,
parseOptions?: GraphQLParseOptions
parseOptions: GraphQLParseOptions = {}
): DocumentNode {
parseOptions = {
noLocation: true,
...parseOptions,
};
let document: DocumentNode;
if (typeof typeDefinitions === 'string') {
document = parse(typeDefinitions, parseOptions);
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/fragments.ts
Expand Up @@ -101,7 +101,9 @@ export function parseFragmentToInlineFragment(definitions: string): InlineFragme
}
}

const query = parse(`{${definitions}}`).definitions[0] as OperationDefinitionNode;
const query = parse(`{${definitions}}`, {
noLocation: true,
}).definitions[0] as OperationDefinitionNode;
for (const selection of query.selectionSet.selections) {
if (selection.kind === Kind.INLINE_FRAGMENT) {
return selection;
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/parse-graphql-sdl.ts
Expand Up @@ -3,7 +3,10 @@ import { ParseOptions, DocumentNode, parse, Kind, Source as GraphQLSource } from
export function parseGraphQLSDL(location: string, rawSDL: string, options: ParseOptions) {
let document: DocumentNode;
try {
document = parse(new GraphQLSource(rawSDL, location), options);
document = parse(new GraphQLSource(rawSDL, location), {
noLocation: true,
...options,
});
} catch (e) {
if (e.message.includes('EOF')) {
document = {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/print-schema-with-directives.ts
Expand Up @@ -84,7 +84,9 @@ function correctType<TMap extends { [key: string]: GraphQLNamedType }, TName ext
if (type.astNode && type.extensionASTNodes) {
type.astNode = type.extensionASTNodes ? extendDefinition(type) : type.astNode;
}
const doc = parse(printType(type));
const doc = parse(printType(type), {
noLocation: true,
});
const fixedAstNode = doc.definitions[0] as TypeDefinitionNode;
const originalAstNode = type?.astNode;
if (originalAstNode) {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/selectionSets.ts
@@ -1,7 +1,9 @@
import { OperationDefinitionNode, SelectionSetNode, parse, Kind, GraphQLObjectType, getNamedType } from 'graphql';

export function parseSelectionSet(selectionSet: string): SelectionSetNode {
const query = parse(selectionSet).definitions[0] as OperationDefinitionNode;
const query = parse(selectionSet, {
noLocation: true,
}).definitions[0] as OperationDefinitionNode;
return query.selectionSet;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/webpack-loader/src/index.ts
@@ -1,6 +1,6 @@
import { loadTypedefs } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { parse } from 'graphql';
import { concatAST } from 'graphql';

export default function (this: any, path: string) {
const callback = this.async();
Expand All @@ -10,8 +10,8 @@ export default function (this: any, path: string) {
loadTypedefs(path, {
loaders: [new GraphQLFileLoader()],
}).then(sources => {
const documentStrs = sources.map(source => source.rawSDL).join('\n');
const mergedDoc = parse(documentStrs, { noLocation: true });
const documents = sources.map(source => source.document);
const mergedDoc = concatAST(documents);
return callback(null, `module.exports = ${JSON.stringify(mergedDoc)}`);
});
}
4 changes: 2 additions & 2 deletions packages/wrap/src/introspect.ts
Expand Up @@ -27,7 +27,7 @@ function getSchemaFromIntrospection(introspectionResult: ExecutionResult<Introsp
}

export async function introspectSchema(executor: AsyncExecutor, context?: Record<string, any>): Promise<GraphQLSchema> {
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery());
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery(), { noLocation: true });
const introspectionResult = await executor<IntrospectionQuery>({
document: parsedIntrospectionQuery,
context,
Expand All @@ -36,7 +36,7 @@ export async function introspectSchema(executor: AsyncExecutor, context?: Record
}

export function introspectSchemaSync(executor: SyncExecutor, context?: Record<string, any>): GraphQLSchema {
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery());
const parsedIntrospectionQuery: DocumentNode = parse(getIntrospectionQuery(), { noLocation: true });
const introspectionResult = executor<IntrospectionQuery>({
document: parsedIntrospectionQuery,
context,
Expand Down
2 changes: 1 addition & 1 deletion packages/wrap/src/transforms/ExtendSchema.ts
Expand Up @@ -35,7 +35,7 @@ export default class ExtendSchema implements Transform {
this.transformer.transformSchema(schema);

return addResolversToSchema({
schema: this.typeDefs ? extendSchema(schema, parse(this.typeDefs)) : schema,
schema: this.typeDefs ? extendSchema(schema, parse(this.typeDefs, { noLocation: true })) : schema,
resolvers: this.resolvers != null ? this.resolvers : {},
defaultFieldResolver: this.defaultFieldResolver,
});
Expand Down

0 comments on commit 5e537bf

Please sign in to comment.