Skip to content

Commit 099b0b4

Browse files
committed
chore: make interfaces consistent
1 parent ce5bfcd commit 099b0b4

File tree

3 files changed

+19
-42
lines changed

3 files changed

+19
-42
lines changed

packages/codemirror-graphql/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ CodeMirror.fromTextArea(myTextarea, {
4545
If you want to have autcompletion for external fragment definitions, there's a new configuration setting available
4646

4747
```ts
48-
import { parse, visit } from 'graphql';
4948
import CodeMirror from 'codemirror';
5049
import 'codemirror/addon/hint/show-hint';
5150
import 'codemirror/addon/lint/lint';
5251
import 'codemirror-graphql/hint';
5352
import 'codemirror-graphql/lint';
5453
import 'codemirror-graphql/mode';
5554

56-
const externalFragmentsExample = `
55+
const externalFragments = `
5756
fragment MyFragment on Example {
5857
id: ID!
5958
name: String!
@@ -64,26 +63,24 @@ const externalFragmentsExample = `
6463
}
6564
`;
6665

67-
const fragmentDefinitions = visit(parse(externalFragmentsExample), {
68-
FragmentDefinition(node) {
69-
return node;
70-
},
71-
});
72-
7366
CodeMirror.fromTextArea(myTextarea, {
7467
mode: 'graphql',
7568
lint: {
7669
schema: myGraphQLSchema,
7770
},
7871
hintOptions: {
7972
schema: myGraphQLSchema,
80-
externalFragmentDefinitions: fragmentDefinitions,
73+
// here we use a string, but
74+
// you can also provide an array of FragmentDefinitionNodes
75+
externalFragments,
8176
},
8277
});
8378
```
8479

8580
### Custom Validation Rules
8681

82+
If you want to show custom validation, you can do that too!
83+
8784
```js
8885
import type { ValidationContext, SDLValidationContext } from 'graphql';
8986

packages/codemirror-graphql/src/hint.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import CodeMirror from 'codemirror';
1414
import { getAutocompleteSuggestions } from 'graphql-language-service-interface';
1515
import { Position } from 'graphql-language-service-utils';
16-
16+
import { getFragmentDefinitions } from './utils/getFragmentDefintions';
1717
/**
1818
* Registers a "hint" helper for CodeMirror.
1919
*
@@ -52,7 +52,9 @@ CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
5252
editor.getValue(),
5353
position,
5454
token,
55-
options.externalFragmentDefinitions || undefined,
55+
Array.isArray(this.props?.externalFragments)
56+
? this.props.externalFragments
57+
: getFragmentDefinitions(this.props.externalFragments),
5658
);
5759

5860
const results = {

packages/graphiql/src/components/QueryEditor.tsx

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ type QueryEditorProps = {
4040
externalFragments?: string | FragmentDefinitionNode[];
4141
};
4242

43-
function gatherFragmentDefinitions(graphqlString: string) {
44-
const definitions: FragmentDefinitionNode[] = [];
45-
visit(parse(graphqlString), {
46-
FragmentDefinition(node) {
47-
definitions.push(node);
48-
},
49-
});
50-
return definitions;
51-
}
5243
/**
5344
* QueryEditor
5445
*
@@ -101,27 +92,6 @@ export class QueryEditor extends React.Component<QueryEditorProps, {}>
10192
require('codemirror-graphql/jump');
10293
require('codemirror-graphql/mode');
10394

104-
const hintOptions: {
105-
schema?: GraphQLSchema;
106-
externalFragmentDefinitions?: FragmentDefinitionNode[];
107-
closeOnUnfocus: boolean;
108-
completeSingle: boolean;
109-
container: any;
110-
} = {
111-
schema: this.props.schema,
112-
closeOnUnfocus: false,
113-
completeSingle: false,
114-
container: this._node,
115-
};
116-
117-
if (this.props.externalFragments) {
118-
hintOptions.externalFragmentDefinitions = Array.isArray(
119-
this.props?.externalFragments,
120-
)
121-
? this.props.externalFragments
122-
: gatherFragmentDefinitions(this.props.externalFragments);
123-
}
124-
12595
const editor: CM.Editor = (this.editor = CodeMirror(this._node, {
12696
value: this.props.value || '',
12797
lineNumbers: true,
@@ -139,8 +109,16 @@ export class QueryEditor extends React.Component<QueryEditorProps, {}>
139109
lint: {
140110
schema: this.props.schema,
141111
validationRules: this.props.validationRules ?? null,
112+
// linting accepts string or FragmentDefinitionNode[]
113+
externalFragments: this.props?.externalFragments,
114+
},
115+
hintOptions: {
116+
schema: this.props.schema,
117+
closeOnUnfocus: false,
118+
completeSingle: false,
119+
container: this._node,
120+
externalFragments: this.props?.externalFragments,
142121
},
143-
hintOptions,
144122
info: {
145123
schema: this.props.schema,
146124
renderDescription: (text: string) => md.render(text),

0 commit comments

Comments
 (0)