Skip to content

Commit

Permalink
Handle array field types used for alias field names (#4961)
Browse files Browse the repository at this point in the history
* Handle array field types used for alias field name

* changeset
  • Loading branch information
gilgardosh committed Jan 11, 2023
1 parent 6dc2aba commit e3ec35e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/wicked-mugs-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

Bug fix: better handle array field types used for alias field names
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 e3ec35e

Please sign in to comment.