|
| 1 | +import { getOperationAST, isObjectType, Kind, parse, print, SelectionSetNode } from 'graphql'; |
| 2 | +import { makeExecutableSchema } from '@graphql-tools/schema'; |
| 3 | +import { stripWhitespaces } from '../../merge/tests/utils'; |
| 4 | +import { extractUnavailableFields } from '../src/getFieldsNotInSubschema'; |
| 5 | + |
| 6 | +describe('extractUnavailableFields', () => { |
| 7 | + it('should extract correct fields', () => { |
| 8 | + const schema = makeExecutableSchema({ |
| 9 | + typeDefs: /* GraphQL */ ` |
| 10 | + type Query { |
| 11 | + user: User |
| 12 | + } |
| 13 | + type User { |
| 14 | + id: ID! |
| 15 | + name: String! |
| 16 | + } |
| 17 | + `, |
| 18 | + }); |
| 19 | + const userQuery = /* GraphQL */ ` |
| 20 | + query { |
| 21 | + user { |
| 22 | + id |
| 23 | + name |
| 24 | + email |
| 25 | + friends { |
| 26 | + id |
| 27 | + name |
| 28 | + email |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + `; |
| 33 | + const userQueryDoc = parse(userQuery, { noLocation: true }); |
| 34 | + const operationAst = getOperationAST(userQueryDoc, null); |
| 35 | + if (!operationAst) { |
| 36 | + throw new Error('Operation AST not found'); |
| 37 | + } |
| 38 | + const selectionSet = operationAst.selectionSet; |
| 39 | + const userSelection = selectionSet.selections[0]; |
| 40 | + if (userSelection.kind !== 'Field') { |
| 41 | + throw new Error('User selection not found'); |
| 42 | + } |
| 43 | + const queryType = schema.getType('Query'); |
| 44 | + if (!isObjectType(queryType)) { |
| 45 | + throw new Error('Query type not found'); |
| 46 | + } |
| 47 | + const userField = queryType.getFields()['user']; |
| 48 | + if (!userField) { |
| 49 | + throw new Error('User field not found'); |
| 50 | + } |
| 51 | + const unavailableFields = extractUnavailableFields(userField, userSelection); |
| 52 | + const extractedSelectionSet: SelectionSetNode = { |
| 53 | + kind: Kind.SELECTION_SET, |
| 54 | + selections: unavailableFields, |
| 55 | + }; |
| 56 | + expect(stripWhitespaces(print(extractedSelectionSet))).toBe( |
| 57 | + `{ email friends { id name email } }`, |
| 58 | + ); |
| 59 | + }); |
| 60 | + it('excludes the fields only with __typename', () => { |
| 61 | + const schema = makeExecutableSchema({ |
| 62 | + typeDefs: /* GraphQL */ ` |
| 63 | + type Query { |
| 64 | + user: User |
| 65 | + } |
| 66 | + type User { |
| 67 | + id: ID! |
| 68 | + name: String! |
| 69 | + friends: [User] |
| 70 | + } |
| 71 | + `, |
| 72 | + }); |
| 73 | + const userQuery = /* GraphQL */ ` |
| 74 | + query { |
| 75 | + user { |
| 76 | + __typename |
| 77 | + id |
| 78 | + name |
| 79 | + friends { |
| 80 | + __typename |
| 81 | + id |
| 82 | + name |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + `; |
| 87 | + const userQueryDoc = parse(userQuery, { noLocation: true }); |
| 88 | + const operationAst = getOperationAST(userQueryDoc, null); |
| 89 | + if (!operationAst) { |
| 90 | + throw new Error('Operation AST not found'); |
| 91 | + } |
| 92 | + const selectionSet = operationAst.selectionSet; |
| 93 | + const userSelection = selectionSet.selections[0]; |
| 94 | + if (userSelection.kind !== 'Field') { |
| 95 | + throw new Error('User selection not found'); |
| 96 | + } |
| 97 | + const queryType = schema.getType('Query'); |
| 98 | + if (!isObjectType(queryType)) { |
| 99 | + throw new Error('Query type not found'); |
| 100 | + } |
| 101 | + const userField = queryType.getFields()['user']; |
| 102 | + if (!userField) { |
| 103 | + throw new Error('User field not found'); |
| 104 | + } |
| 105 | + const unavailableFields = extractUnavailableFields(userField, userSelection); |
| 106 | + const extractedSelectionSet: SelectionSetNode = { |
| 107 | + kind: Kind.SELECTION_SET, |
| 108 | + selections: unavailableFields, |
| 109 | + }; |
| 110 | + expect(stripWhitespaces(print(extractedSelectionSet))).toBe(''); |
| 111 | + }); |
| 112 | +}); |
0 commit comments