Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle array field types used for alias field names #4961

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(']', '');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding

This replaces only the first occurrence of '\]'.

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding

This replaces only the first occurrence of '\['.
}
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();
});