Skip to content

Commit

Permalink
fix(visitResult): handle introspection fields correctly with an intro…
Browse files Browse the repository at this point in the history
…spection query result (#4541)
  • Loading branch information
ardatan committed Jun 24, 2022
1 parent 923605c commit a0abbbc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-coats-kick.md
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

fix(visitResult): handle introspection fields correctly with an introspection query result
13 changes: 12 additions & 1 deletion packages/utils/src/visitResult.ts
Expand Up @@ -13,6 +13,7 @@ import {
GraphQLError,
TypeNameMetaFieldDef,
FragmentDefinitionNode,
SchemaMetaFieldDef,
} from 'graphql';
import { collectFields, collectSubFields } from './collectFields';

Expand Down Expand Up @@ -232,7 +233,17 @@ function visitObjectValue(

for (const [responseKey, subFieldNodes] of fieldNodeMap) {
const fieldName = subFieldNodes[0].name.value;
const fieldType = fieldName === '__typename' ? TypeNameMetaFieldDef.type : fieldMap[fieldName]?.type;
let fieldType = fieldMap[fieldName]?.type;
if (fieldType == null) {
switch (fieldName) {
case '__typename':
fieldType = TypeNameMetaFieldDef.type;
break;
case '__schema':
fieldType = SchemaMetaFieldDef.type;
break;
}
}

const newPathIndex = pathIndex + 1;

Expand Down
23 changes: 21 additions & 2 deletions packages/utils/tests/visitResult.test.ts
@@ -1,4 +1,4 @@
import { buildSchema, parse, GraphQLError } from 'graphql';
import { buildSchema, parse, GraphQLError, getIntrospectionQuery, introspectionFromSchema } from 'graphql';

import { createGraphQLError, ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';

Expand Down Expand Up @@ -42,7 +42,7 @@ describe('visiting results', () => {
expect(visitedResult).toEqual(result);
});

it('should visit with a request with introspection fields without throwing', async () => {
it('should visit with a request with typename fields without throwing', async () => {
const introspectionRequest: ExecutionRequest = {
document: parse('{ test { field __typename } }'),
variables: {},
Expand All @@ -58,6 +58,25 @@ describe('visiting results', () => {
expect(() => visitResult(result, introspectionRequest, schema, undefined)).not.toThrow();
});

it('should visit with a request with introspection fields without throwing', async () => {
const introspectionRequest: ExecutionRequest = {
document: parse(getIntrospectionQuery()),
variables: {},
};
const result: any = {
data: introspectionFromSchema(schema),
};
expect(() =>
visitResult(result, introspectionRequest, schema, {
Query: {
__enter(val) {
return val;
},
},
})
).not.toThrow();
});

it('should successfully modify the result using an object type result visitor', async () => {
const result = {
data: {
Expand Down

0 comments on commit a0abbbc

Please sign in to comment.