Skip to content

Commit bb93abc

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

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

packages/codemirror-graphql/README.md

Lines changed: 8 additions & 11 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

@@ -99,8 +96,8 @@ const ExampleRule = (context: ValidationContext | SDLValidationContext) => {
9996
const schema = context.getSchema();
10097
const document = context.getDocument();
10198
// do stuff
102-
if (containsSomethingWeDontWant(document, schema)) {
103-
context.reportError('Nope not here');
99+
if (operationContainsCondition(document, schema)) {
100+
context.reportError('this contains the condition, oops!');
104101
}
105102
};
106103

packages/codemirror-graphql/src/__tests__/hint-test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function createEditorWithHint() {
4040
schema: TestSchema,
4141
closeOnUnfocus: false,
4242
completeSingle: false,
43-
externalFragments: [],
43+
externalFragments: 'fragment Example on Test { id }',
4444
},
4545
});
4646
}
@@ -767,6 +767,11 @@ describe('graphql-hint', () => {
767767
type: TestType,
768768
description: 'fragment Foo on Test',
769769
},
770+
{
771+
text: 'Example',
772+
type: TestType,
773+
description: 'fragment Example on Test',
774+
},
770775
];
771776
const expectedSuggestions = getExpectedSuggestions(list);
772777
expect(suggestions.list).to.deep.equal(expectedSuggestions);
@@ -783,6 +788,11 @@ describe('graphql-hint', () => {
783788
type: TestType,
784789
description: 'fragment Foo on Test',
785790
},
791+
{
792+
text: 'Example',
793+
type: TestType,
794+
description: 'fragment Example on Test',
795+
},
786796
];
787797
const expectedSuggestions = getExpectedSuggestions(list);
788798
expect(suggestions.list).to.deep.equal(expectedSuggestions);

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/getFragmentDefinitions';
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(options.externalFragments)
56+
? options.externalFragments
57+
: getFragmentDefinitions(options.externalFragments),
5658
);
5759

5860
const results = {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { visit, parse } from 'graphql';
2+
import type { FragmentDefinitionNode } from 'graphql';
3+
4+
export function getFragmentDefinitions(graphqlString: string) {
5+
const definitions: FragmentDefinitionNode[] = [];
6+
visit(parse(graphqlString), {
7+
FragmentDefinition(node) {
8+
definitions.push(node);
9+
},
10+
});
11+
return definitions;
12+
}

packages/graphiql/src/components/QueryEditor.tsx

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
GraphQLSchema,
1313
GraphQLType,
1414
ValidationRule,
15-
visit,
16-
parse,
1715
} from 'graphql';
1816
import MD from 'markdown-it';
1917
import { normalizeWhitespace } from '../utility/normalizeWhitespace';
@@ -40,15 +38,6 @@ type QueryEditorProps = {
4038
externalFragments?: string | FragmentDefinitionNode[];
4139
};
4240

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-
}
5241
/**
5342
* QueryEditor
5443
*
@@ -101,27 +90,6 @@ export class QueryEditor extends React.Component<QueryEditorProps, {}>
10190
require('codemirror-graphql/jump');
10291
require('codemirror-graphql/mode');
10392

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-
12593
const editor: CM.Editor = (this.editor = CodeMirror(this._node, {
12694
value: this.props.value || '',
12795
lineNumbers: true,
@@ -139,8 +107,16 @@ export class QueryEditor extends React.Component<QueryEditorProps, {}>
139107
lint: {
140108
schema: this.props.schema,
141109
validationRules: this.props.validationRules ?? null,
110+
// linting accepts string or FragmentDefinitionNode[]
111+
externalFragments: this.props?.externalFragments,
112+
},
113+
hintOptions: {
114+
schema: this.props.schema,
115+
closeOnUnfocus: false,
116+
completeSingle: false,
117+
container: this._node,
118+
externalFragments: this.props?.externalFragments,
142119
},
143-
hintOptions,
144120
info: {
145121
schema: this.props.schema,
146122
renderDescription: (text: string) => md.render(text),

0 commit comments

Comments
 (0)