Skip to content

Commit e3ec35e

Browse files
authored
Handle array field types used for alias field names (#4961)
* Handle array field types used for alias field name * changeset
1 parent 6dc2aba commit e3ec35e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

.changeset/wicked-mugs-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
Bug fix: better handle array field types used for alias field names

packages/utils/src/build-operation-for-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ function resolveField({
476476
const fieldPathStr = fieldPath.join('.');
477477
let fieldName = field.name;
478478
if (fieldTypeMap.has(fieldPathStr) && fieldTypeMap.get(fieldPathStr) !== field.type.toString()) {
479-
fieldName += (field.type as any).toString().replace('!', 'NonNull');
479+
fieldName += (field.type as any).toString().replace('!', 'NonNull').replace('[', 'List').replace(']', '');
480480
}
481481
fieldTypeMap.set(fieldPathStr, field.type.toString());
482482

packages/utils/tests/build-operation-node-for-field.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { print, parse, buildSchema, ASTNode, OperationTypeNode } from 'graphql';
22

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

56
function clean(doc: string | ASTNode) {
67
return print(typeof doc === 'string' ? parse(doc) : doc).trim();
@@ -46,13 +47,24 @@ const schema = buildSchema(/* GraphQL */ `
4647
comments(filter: String!): [String!]!
4748
}
4849
50+
type BestFood {
51+
recommendation: Food
52+
}
53+
54+
type BestFoods {
55+
recommendation: [Food]
56+
}
57+
58+
union Recommendations = BestFood | BestFoods
59+
4960
type Query {
5061
me: User
5162
user(id: ID!): User
5263
users: [User!]
5364
menu: [Food]
5465
menuByIngredients(ingredients: [String!]!): [Food]
5566
feed: [Post]
67+
recommendations: Recommendations
5668
}
5769
5870
type Mutation {
@@ -640,3 +652,16 @@ test('selectedFields', async () => {
640652
`)
641653
);
642654
});
655+
656+
test('should handle array field types used for alias field names', async () => {
657+
const document = buildOperationNodeForField({
658+
schema,
659+
kind: 'query' as OperationTypeNode,
660+
field: 'recommendations',
661+
depthLimit: 3,
662+
})!;
663+
const virtualFileName = document.name?.value || 'defaultName';
664+
const rawSDL = print(document);
665+
const source = parseGraphQLSDL(`${virtualFileName}.graphql`, rawSDL);
666+
expect(source).toBeDefined();
667+
});

0 commit comments

Comments
 (0)