Skip to content

Commit 4c50024

Browse files
committed
better context detection
1 parent 937bab3 commit 4c50024

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

packages/graphql-language-service/src/interface/__tests__/__schema__/StarWarsSchema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ input InputType {
4747
}
4848

4949
input OneOfInputType @oneOf {
50-
key: String!
50+
key: String
5151
value: Int
5252
obj: InputType
5353
}

packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,14 @@ describe('getAutocompleteSuggestions', () => {
683683
});
684684

685685
it('provides correct oneOf input type field suggestions', () => {
686+
const args = [
687+
{ ...inputArgs[0], detail: 'String' },
688+
inputArgs[1],
689+
inputArgs[2],
690+
];
686691
expect(
687692
testSuggestions('{ oneOfInputTypeTest(oneOf: {', new Position(0, 29)),
688-
).toEqual(inputArgs);
693+
).toEqual(args);
689694
});
690695

691696
it('provides no more field suggestions once a oneOf field is chosen', () => {
@@ -697,6 +702,17 @@ describe('getAutocompleteSuggestions', () => {
697702
).toEqual([]);
698703
});
699704

705+
// TODO: decide if we want this. Discussing with @benjie, we might want to actually give the user flexibility here,
706+
// instead of being strict
707+
// it('provides no more field suggestions once a oneOf field is chose and a user begins typing another field', () => {
708+
// expect(
709+
// testSuggestions(
710+
// '{ oneOfInputTypeTest(oneOf: { value: 2 d',
711+
// new Position(0, 40),
712+
// ),
713+
// ).toEqual([]);
714+
// });
715+
700716
it('provides correct field name suggestion inside inline fragment', () => {
701717
expect(
702718
testSuggestions(

packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,21 @@ export function getAutocompleteSuggestions(
312312
(kind === RuleKinds.OBJECT_FIELD && step === 0)) &&
313313
typeInfo.objectFieldDefs
314314
) {
315-
const objectFields = objectValues(typeInfo.objectFieldDefs);
316-
const completionKind =
317-
kind === RuleKinds.OBJECT_VALUE
318-
? CompletionItemKind.Value
319-
: CompletionItemKind.Field;
320315
// @oneOf logic!
316+
console.log(state.prevState?.prevState?.kind, !!typeInfo.inputType);
321317
if (
322-
typeInfo?.inputType &&
323-
'isOneOf' in typeInfo.inputType &&
324-
typeInfo.inputType.isOneOf === true &&
325-
context.token.string !== '{'
318+
typeInfo?.inputType?.isOneOf === true &&
319+
(state.prevState?.prevState?.kind !== 'Argument' || token.string !== '{')
326320
) {
321+
// return empty array early if a oneOf field has already been provided
327322
return [];
328323
}
324+
const objectFields = objectValues(typeInfo.objectFieldDefs);
325+
const completionKind =
326+
kind === RuleKinds.OBJECT_VALUE
327+
? CompletionItemKind.Value
328+
: CompletionItemKind.Field;
329+
329330
return hintList(
330331
token,
331332
objectFields.map(field => ({

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,11 @@ export function getTypeInfo(
213213
}
214214
}
215215
}
216-
inputType = argDef?.type;
216+
if (argDef?.type) {
217+
inputType = argDef.type;
218+
}
217219
break;
220+
218221
case RuleKinds.VARIABLE_DEFINITION:
219222
case RuleKinds.VARIABLE:
220223
type = inputType;
@@ -241,6 +244,8 @@ export function getTypeInfo(
241244
objectType instanceof GraphQLInputObjectType
242245
? objectType.getFields()
243246
: null;
247+
inputType = objectType;
248+
console.log(inputType);
244249
break;
245250
// TODO: needs tests
246251
case RuleKinds.OBJECT_FIELD:

packages/graphql-language-service/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import type {
2929
GraphQLObjectType,
3030
GraphQLType,
3131
GraphQLDirective,
32+
GraphQLInputType,
33+
GraphQLInputObjectType,
3234
} from 'graphql';
3335

3436
export type Maybe<T> = T | null | undefined;

0 commit comments

Comments
 (0)