Skip to content

Commit

Permalink
fix: bug with insertText in non-composite lists! (#2114)
Browse files Browse the repository at this point in the history
Fix major issue with non-leaf types receiving insertText
  • Loading branch information
acao authored Dec 9, 2021
1 parent f206e9b commit d5fca9d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-lies-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-interface': patch
---

Fix major issue with non-leaf types receiving insertText
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Droid implements Character {
appearsIn: [Episode]
secretBackstory: String
primaryFunction: String
instructions: [String]!
}

input InputType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ const expectedResults = {
insertText: `inputTypeTest {\n $1\n}`,
},
appearsIn: {
...commonInsert,
label: 'appearsIn',
detail: '[Episode]',
insertText: `appearsIn {\n $1\n}`,
},
friends: {
...commonInsert,
Expand Down Expand Up @@ -492,6 +490,7 @@ describe('getAutocompleteSuggestions', () => {
expectedResults.appearsIn,
expectedResults.friends,
{ label: 'id', detail: 'String!' },
{ label: 'instructions', detail: '[String]!' },
{ label: 'name', detail: 'String' },
{ label: 'primaryFunction', detail: 'String' },
{ label: 'secretBackstory', detail: 'String' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
Kind,
DirectiveLocation,
GraphQLArgument,
isObjectType,
isListType,
isNonNullType,
} from 'graphql';
Expand Down Expand Up @@ -284,19 +283,24 @@ export function getAutocompleteSuggestions(

const insertSuffix = ` {\n $1\n}`;

/**
* Choose carefully when to insert the `insertText`!
* @param field
* @returns
*/
const getInsertText = (field: GraphQLField<null, null>) => {
const type = field.type;
if (isCompositeType(type)) {
return insertSuffix;
}
if (isListType(type)) {
if (isListType(type) && isCompositeType(type.ofType)) {
return insertSuffix;
}
if (isNonNullType(type)) {
if (isObjectType(type.ofType)) {
if (isCompositeType(type.ofType)) {
return insertSuffix;
}
if (isListType(type.ofType)) {
if (isListType(type.ofType) && isCompositeType(type.ofType.ofType)) {
return insertSuffix;
}
}
Expand Down

2 comments on commit d5fca9d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.