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

feat(graphql-language-service): Support for experimental fragment arguments #3761

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@
},
]);
});

const metaArgs = [
{
label: '__DirectiveLocation',
Expand All @@ -409,6 +410,7 @@
'An enum describing what kind of type a given `__Type` is.',
},
];

it('provides correct input type suggestions', () => {
const result = testSuggestions(
'query($exampleVariable: ) { ',
Expand All @@ -423,6 +425,45 @@
{ label: 'String', documentation: GraphQLString.description },
]);
});

it('provides correct input type suggestions for fragments', () => {
const result = testSuggestions(
'fragment someFragment($exampleVariable: ) on Type { ',
new Position(0, 41),
);
expect(result).toEqual([
...metaArgs,
{ label: 'Boolean', documentation: GraphQLBoolean.description },
{ label: 'Episode' },
{ label: 'InputType' },
{ label: 'Int', documentation: GraphQLInt.description },
{ label: 'String', documentation: GraphQLString.description },
]);
});

it.skip('provides correct argument suggestions for fragment-spreads', () => {

Check warning on line 444 in packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts

View workflow job for this annotation

GitHub Actions / ESLint

Disabled test
const externalFragments = parse(`
fragment CharacterDetails($someVariable: Int) on Human {
name
}
`, {experimentalFragmentVariables:true}).definitions as FragmentDefinitionNode[];

const result = testSuggestions(
'query { human(id: "1") { ...CharacterDetails() }}',
new Position(0, 46),
externalFragments,
);

expect(result).toEqual([
{
label: 'someVariable',
insertText: 'someVariable: ',
command: suggestionCommand,
insertTextFormat: 2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this test is not working yet, even with your graphql-js changes, it may be because we aren't outputting the arguments in getTypeInfo

labelDetails: { detail: ' Int' },
},
]);
});

it('provides filtered input type suggestions', () => {
const result = testSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export function getAutocompleteSuggestions(
if (
(kind === RuleKinds.VARIABLE_DEFINITION && step === 2) ||
(kind === RuleKinds.LIST_TYPE && step === 1) ||
(kind === RuleKinds.FRAGMENT_DEFINITION && step === 3) ||
(kind === RuleKinds.NAMED_TYPE &&
prevState &&
(prevState.kind === RuleKinds.VARIABLE_DEFINITION ||
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-language-service/src/parser/Rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const ParseRules: { [name: string]: ParseRule } = {

Arguments: [p('('), list('Argument'), p(')')],
Argument: [name('attribute'), p(':'), 'Value'],
FragmentSpread: [p('...'), name('def'), list('Directive')],
FragmentSpread: [p('...'), name('def'), opt('Arguments'), list('Directive')],
InlineFragment: [
p('...'),
opt('TypeCondition'),
Expand All @@ -154,6 +154,7 @@ export const ParseRules: { [name: string]: ParseRule } = {
FragmentDefinition: [
word('fragment'),
opt(butNot(name('def'), [word('on')])),
opt('VariableDefinitions'),
'TypeCondition',
list('Directive'),
'SelectionSet',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,63 @@ describe('onlineParser', () => {

t.eol();
});

it('parses query with fragment spread containing arguments', () => {
const { t, stream } = getUtils(`
query SomeQuery {
someField {
...fragmentName(someVariable: 6)
}
}
`);

t.keyword('query', { kind: 'Query' });
t.def('SomeQuery');
t.punctuation('{', { kind: 'SelectionSet' });

t.property('someField', { kind: 'Field' });
t.punctuation('{', { kind: 'SelectionSet' });

t.punctuation('...', { kind: 'FragmentSpread' });
t.def('fragmentName');
expectArgs(
{ t, stream },
{ onKind: 'FragmentSpread', args: [{ name: 'someVariable', value: '6', valueType: 'Number', kind: 'NumberValue' }] },
);

t.punctuation('}', { kind: 'SelectionSet' });

t.punctuation('}', { kind: 'Document' });

t.eol();
});

it('parses query with fragment variables', () => {
const { t, stream } = getUtils(`
fragment fragmentName($someVariable: Int) on SomeType {
anotherField
}
`);

t.keyword('fragment', { kind: 'FragmentDefinition' });
t.def('fragmentName');
expectVarsDef(
{ t, stream },
{
onKind: 'FragmentDefinition',
vars: [{ name: 'someVariable', type: 'Int' }],
},
);
t.keyword('on', { kind: 'TypeCondition' });
t.name('SomeType', { kind: 'NamedType' });
t.punctuation('{', { kind: 'SelectionSet' });

t.property('anotherField', { kind: 'Field' });

t.punctuation('}', { kind: 'Document' });

t.eol();
});

it('parses mutation', () => {
const { t } = getUtils(`
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql-language-service/src/parser/getTypeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
argDefs =
directiveDef && (directiveDef.args as GraphQLArgument[]);
break;
case RuleKinds.FRAGMENT_SPREAD:

Check warning on line 181 in packages/graphql-language-service/src/parser/getTypeInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service/src/parser/getTypeInfo.ts#L181

Added line #L181 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether getting the type-info is actually needed here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you are lexing properly with the changes in Rules, but you still need to track the arguments here

// TODO: lookup fragment and return variable definitions (?)
break;

Check warning on line 183 in packages/graphql-language-service/src/parser/getTypeInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service/src/parser/getTypeInfo.ts#L183

Added line #L183 was not covered by tests
// TODO: needs more tests
case RuleKinds.ALIASED_FIELD: {
const name = state.prevState?.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import {
ValidationRule,
DocumentNode,
// TODO: this should contain fragment-arguments rules
specifiedRules,
validate,
GraphQLError,
Expand Down
Loading