|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import type { GraphQLNonNull, NamedTypeNode, NonNullTypeNode } from 'graphql'; |
| 5 | +import { |
| 6 | + GraphQLInputObjectType, |
| 7 | + GraphQLObjectType, |
| 8 | + GraphQLSchema, |
| 9 | + GraphQLString, |
| 10 | + Kind, |
| 11 | +} from 'graphql'; |
| 12 | + |
| 13 | +import { toExecutorSchema } from '..'; |
| 14 | + |
| 15 | +describe('ExecutorSchema:', () => { |
| 16 | + const input = new GraphQLInputObjectType({ |
| 17 | + name: 'Input', |
| 18 | + fields: { |
| 19 | + inputField: { |
| 20 | + type: GraphQLString, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }); |
| 24 | + const query = new GraphQLObjectType({ |
| 25 | + name: 'Query', |
| 26 | + fields: { |
| 27 | + field: { |
| 28 | + type: GraphQLString, |
| 29 | + args: { |
| 30 | + arg: { |
| 31 | + type: input, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + const schema = new GraphQLSchema({ |
| 38 | + query, |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not throw', () => { |
| 42 | + expect(() => toExecutorSchema(schema)).not.to.throw(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('allows retrieving output types', () => { |
| 46 | + const executorSchema = toExecutorSchema(schema); |
| 47 | + const namedTypeNode: NamedTypeNode = { |
| 48 | + kind: Kind.NAMED_TYPE, |
| 49 | + name: { |
| 50 | + kind: Kind.NAME, |
| 51 | + value: 'Query', |
| 52 | + }, |
| 53 | + }; |
| 54 | + const type = executorSchema.getType(namedTypeNode); |
| 55 | + expect(type).to.equal(query); |
| 56 | + }); |
| 57 | + |
| 58 | + it('allows retrieving input types', () => { |
| 59 | + const executorSchema = toExecutorSchema(schema); |
| 60 | + const namedTypeNode: NamedTypeNode = { |
| 61 | + kind: Kind.NAMED_TYPE, |
| 62 | + name: { |
| 63 | + kind: Kind.NAME, |
| 64 | + value: 'Input', |
| 65 | + }, |
| 66 | + }; |
| 67 | + const type = executorSchema.getType(namedTypeNode); |
| 68 | + expect(type).to.equal(input); |
| 69 | + expect(executorSchema.isInputType(type)).to.equal(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('allows retrieving input types defined in schema wrapped with non-null', () => { |
| 73 | + const executorSchema = toExecutorSchema(schema); |
| 74 | + const nonNullTypeNode: NonNullTypeNode = { |
| 75 | + kind: Kind.NON_NULL_TYPE, |
| 76 | + type: { |
| 77 | + kind: Kind.NAMED_TYPE, |
| 78 | + name: { |
| 79 | + kind: Kind.NAME, |
| 80 | + value: 'Input', |
| 81 | + }, |
| 82 | + }, |
| 83 | + }; |
| 84 | + const type = executorSchema.getType(nonNullTypeNode); |
| 85 | + expect(type).to.not.equal(undefined); |
| 86 | + expect(executorSchema.isNonNullType(type)).to.equal(true); |
| 87 | + expect(executorSchema.isInputType(type)).to.equal(true); |
| 88 | + expect((type as GraphQLNonNull<any>).ofType).to.equal(input); |
| 89 | + }); |
| 90 | +}); |
0 commit comments