Skip to content

Commit

Permalink
fix: bug with externalFragments in codemirror (#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
acao authored Jan 7, 2021
1 parent 464a885 commit f423e61
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
5 changes: 1 addition & 4 deletions packages/codemirror-graphql/src/hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import CodeMirror from 'codemirror';
import { getAutocompleteSuggestions } from 'graphql-language-service-interface';
import { Position } from 'graphql-language-service-utils';
import { getFragmentDefinitions } from './utils/getFragmentDefinitions';
/**
* Registers a "hint" helper for CodeMirror.
*
Expand Down Expand Up @@ -52,9 +51,7 @@ CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
editor.getValue(),
position,
token,
Array.isArray(options.externalFragments)
? options.externalFragments
: getFragmentDefinitions(options.externalFragments),
options.externalFragments,
);

const results = {
Expand Down
7 changes: 6 additions & 1 deletion packages/codemirror-graphql/src/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ const TYPE = {
*/
CodeMirror.registerHelper('lint', 'graphql', (text, options) => {
const schema = options.schema;
const rawResults = getDiagnostics(text, schema, options.validationRules);
const rawResults = getDiagnostics(
text,
schema,
options.validationRules,
options.externalFragments,
);

const results = rawResults.map(error => ({
message: error.message,
Expand Down
12 changes: 0 additions & 12 deletions packages/codemirror-graphql/src/utils/getFragmentDefinitions.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
isAbstractType,
isCompositeType,
isInputType,
visit,
parse,
} from 'graphql';

import {
Expand All @@ -63,6 +65,24 @@ import {
hintList,
objectValues,
} from './autocompleteUtils';

const collectFragmentDefs = (op: string | undefined) => {
const externalFragments: FragmentDefinitionNode[] = [];
if (op) {
visit(
parse(op, {
experimentalFragmentVariables: true,
}),
{
FragmentDefinition(def) {
externalFragments.push(def);
},
},
);
}
return externalFragments;
};

/**
* Given GraphQLSchema, queryText, and context of the current position within
* the source text, provide a list of typeahead entries.
Expand All @@ -72,7 +92,7 @@ export function getAutocompleteSuggestions(
queryText: string,
cursor: IPosition,
contextToken?: ContextToken,
fragmentDefs?: FragmentDefinitionNode[],
fragmentDefs?: FragmentDefinitionNode[] | string,
): Array<CompletionItem> {
const token = contextToken || getTokenAtPosition(queryText, cursor);

Expand Down Expand Up @@ -207,7 +227,9 @@ export function getAutocompleteSuggestions(
typeInfo,
schema,
queryText,
fragmentDefs,
Array.isArray(fragmentDefs)
? fragmentDefs
: collectFragmentDefs(fragmentDefs),
);
}

Expand Down Expand Up @@ -489,6 +511,9 @@ function getSuggestionsForFragmentSpread(
queryText: string,
fragmentDefs?: FragmentDefinitionNode[],
): Array<CompletionItem> {
if (!queryText) {
return [];
}
const typeMap = schema.getTypeMap();
const defState = getDefinitionState(token.state);
const fragments = getFragmentDefinitions(queryText);
Expand Down

0 comments on commit f423e61

Please sign in to comment.