|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { GraphQLError } from '../error/GraphQLError.js'; |
| 5 | + |
| 6 | +import { Source } from '../language/source.js'; |
| 7 | + |
| 8 | +import { GraphQLObjectType } from '../type/definition.js'; |
| 9 | +import { GraphQLString } from '../type/scalars.js'; |
| 10 | +import { GraphQLSchema } from '../type/schema.js'; |
| 11 | + |
| 12 | +import type { ValidationRule } from '../validation/ValidationContext.js'; |
| 13 | + |
| 14 | +import { graphql, graphqlSync } from '../graphql.js'; |
| 15 | + |
| 16 | +const schema = new GraphQLSchema({ |
| 17 | + query: new GraphQLObjectType({ |
| 18 | + name: 'Query', |
| 19 | + fields: { |
| 20 | + a: { |
| 21 | + type: GraphQLString, |
| 22 | + resolve: () => 'A', |
| 23 | + }, |
| 24 | + b: { |
| 25 | + type: GraphQLString, |
| 26 | + resolve: () => 'B', |
| 27 | + }, |
| 28 | + contextEcho: { |
| 29 | + type: GraphQLString, |
| 30 | + resolve: (_source, _args, contextValue) => String(contextValue), |
| 31 | + }, |
| 32 | + syncField: { |
| 33 | + type: GraphQLString, |
| 34 | + resolve: (rootValue) => rootValue, |
| 35 | + }, |
| 36 | + asyncField: { |
| 37 | + type: GraphQLString, |
| 38 | + resolve: (rootValue) => Promise.resolve(rootValue), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }), |
| 42 | +}); |
| 43 | + |
| 44 | +describe('graphql', () => { |
| 45 | + it('passes source through to parse', async () => { |
| 46 | + const source = new Source('{', 'custom-query.graphql'); |
| 47 | + |
| 48 | + const result = await graphql({ schema, source }); |
| 49 | + |
| 50 | + expect(result.errors?.[0]?.source?.name).to.equal('custom-query.graphql'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('passes rules through to validate', async () => { |
| 54 | + const customRule: ValidationRule = (context) => ({ |
| 55 | + Field(node) { |
| 56 | + context.reportError( |
| 57 | + new GraphQLError('custom rule error', { |
| 58 | + nodes: node, |
| 59 | + }), |
| 60 | + ); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const result = await graphql({ |
| 65 | + schema, |
| 66 | + source: '{ a }', |
| 67 | + rules: [customRule], |
| 68 | + }); |
| 69 | + |
| 70 | + expect(result.errors?.[0]?.message).to.equal('custom rule error'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('passes parse options through to parse', async () => { |
| 74 | + const customRule: ValidationRule = (context) => ({ |
| 75 | + OperationDefinition(node) { |
| 76 | + context.reportError( |
| 77 | + new GraphQLError( |
| 78 | + node.loc === undefined ? 'no location' : 'has location', |
| 79 | + { |
| 80 | + nodes: node, |
| 81 | + }, |
| 82 | + ), |
| 83 | + ); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const result = await graphql({ |
| 88 | + schema, |
| 89 | + source: '{ a }', |
| 90 | + noLocation: true, |
| 91 | + rules: [customRule], |
| 92 | + }); |
| 93 | + |
| 94 | + expect(result.errors?.[0]?.message).to.equal('no location'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('passes validation options through to validate', async () => { |
| 98 | + const result = await graphql({ |
| 99 | + schema, |
| 100 | + source: '{ contextEho }', |
| 101 | + hideSuggestions: true, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result.errors?.[0]?.message).to.equal( |
| 105 | + 'Cannot query field "contextEho" on type "Query".', |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('passes execution args through to execute', async () => { |
| 110 | + const result = await graphql({ |
| 111 | + schema, |
| 112 | + source: ` |
| 113 | + query First { |
| 114 | + a |
| 115 | + } |
| 116 | +
|
| 117 | + query Second { |
| 118 | + b |
| 119 | + } |
| 120 | + `, |
| 121 | + operationName: 'Second', |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result).to.deep.equal({ |
| 125 | + data: { |
| 126 | + b: 'B', |
| 127 | + }, |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('returns schema validation errors', async () => { |
| 132 | + const badSchema = new GraphQLSchema({}); |
| 133 | + const result = await graphql({ |
| 134 | + schema: badSchema, |
| 135 | + source: '{ __typename }', |
| 136 | + }); |
| 137 | + |
| 138 | + expect(result.errors?.[0]?.message).to.equal( |
| 139 | + 'Query root type must be provided.', |
| 140 | + ); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('graphqlSync', () => { |
| 145 | + it('returns result for synchronous execution', () => { |
| 146 | + const result = graphqlSync({ |
| 147 | + schema, |
| 148 | + source: '{ syncField }', |
| 149 | + rootValue: 'rootValue', |
| 150 | + }); |
| 151 | + |
| 152 | + expect(result).to.deep.equal({ data: { syncField: 'rootValue' } }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('throws for asynchronous execution', () => { |
| 156 | + expect(() => { |
| 157 | + graphqlSync({ |
| 158 | + schema, |
| 159 | + source: '{ asyncField }', |
| 160 | + rootValue: 'rootValue', |
| 161 | + }); |
| 162 | + }).to.throw('GraphQL execution failed to complete synchronously.'); |
| 163 | + }); |
| 164 | +}); |
0 commit comments