Skip to content

Commit

Permalink
Handle array field types used for alias field name
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Jan 11, 2023
1 parent 356b26b commit 84e8cc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/utils/src/build-operation-for-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function resolveField({
const fieldPathStr = fieldPath.join('.');
let fieldName = field.name;
if (fieldTypeMap.has(fieldPathStr) && fieldTypeMap.get(fieldPathStr) !== field.type.toString()) {
fieldName += (field.type as any).toString().replace('!', 'NonNull');
fieldName += (field.type as any).toString().replace('!', 'NonNull').replace('[', 'List').replace(']', '');
}
fieldTypeMap.set(fieldPathStr, field.type.toString());

Expand Down
25 changes: 25 additions & 0 deletions packages/utils/tests/build-operation-node-for-field.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { print, parse, buildSchema, ASTNode, OperationTypeNode } from 'graphql';

import { buildOperationNodeForField } from '../src/build-operation-for-field.js';
import { parseGraphQLSDL } from '../src/parse-graphql-sdl.js';

function clean(doc: string | ASTNode) {
return print(typeof doc === 'string' ? parse(doc) : doc).trim();
Expand Down Expand Up @@ -46,13 +47,24 @@ const schema = buildSchema(/* GraphQL */ `
comments(filter: String!): [String!]!
}
type BestFood {
recommendation: Food
}
type BestFoods {
recommendation: [Food]
}
union Recommendations = BestFood | BestFoods
type Query {
me: User
user(id: ID!): User
users: [User!]
menu: [Food]
menuByIngredients(ingredients: [String!]!): [Food]
feed: [Post]
recommendations: Recommendations
}
type Mutation {
Expand Down Expand Up @@ -640,3 +652,16 @@ test('selectedFields', async () => {
`)
);
});

test('should handle array field types used for alias field names', async () => {
const document = buildOperationNodeForField({
schema,
kind: 'query' as OperationTypeNode,
field: 'recommendations',
depthLimit: 3,
})!;
const virtualFileName = document.name?.value || 'defaultName';
const rawSDL = print(document);
const source = parseGraphQLSDL(`${virtualFileName}.graphql`, rawSDL);
expect(source).toBeDefined();
});

0 comments on commit 84e8cc6

Please sign in to comment.