|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import { describe, it } from 'mocha'; |
| 10 | +import { graphqlSync } from '../../graphql'; |
| 11 | +import { execute } from '../execute'; |
| 12 | +import { parse } from '../../language'; |
| 13 | +import { GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../type'; |
| 14 | + |
| 15 | +describe('Execute: synchronously when possible', () => { |
| 16 | + const schema = new GraphQLSchema({ |
| 17 | + query: new GraphQLObjectType({ |
| 18 | + name: 'Query', |
| 19 | + fields: { |
| 20 | + syncField: { |
| 21 | + type: GraphQLString, |
| 22 | + resolve(rootValue) { |
| 23 | + return rootValue; |
| 24 | + }, |
| 25 | + }, |
| 26 | + asyncField: { |
| 27 | + type: GraphQLString, |
| 28 | + async resolve(rootValue) { |
| 29 | + return await rootValue; |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }), |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not return a Promise for initial errors', () => { |
| 37 | + const doc = 'fragment Example on Query { syncField }'; |
| 38 | + const result = execute({ |
| 39 | + schema, |
| 40 | + document: parse(doc), |
| 41 | + rootValue: 'rootValue', |
| 42 | + }); |
| 43 | + expect(result).to.deep.equal({ |
| 44 | + errors: [ |
| 45 | + { |
| 46 | + message: 'Must provide an operation.', |
| 47 | + locations: undefined, |
| 48 | + path: undefined, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not return a Promise if fields are all synchronous', () => { |
| 55 | + const doc = 'query Example { syncField }'; |
| 56 | + const result = execute({ |
| 57 | + schema, |
| 58 | + document: parse(doc), |
| 59 | + rootValue: 'rootValue', |
| 60 | + }); |
| 61 | + expect(result).to.deep.equal({ data: { syncField: 'rootValue' } }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns a Promise if any field is asynchronous', async () => { |
| 65 | + const doc = 'query Example { syncField, asyncField }'; |
| 66 | + const result = execute({ |
| 67 | + schema, |
| 68 | + document: parse(doc), |
| 69 | + rootValue: 'rootValue', |
| 70 | + }); |
| 71 | + expect(result).to.be.instanceOf(Promise); |
| 72 | + expect(await result).to.deep.equal({ |
| 73 | + data: { syncField: 'rootValue', asyncField: 'rootValue' }, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('graphqlSync', () => { |
| 78 | + it('does not return a Promise for syntax errors', () => { |
| 79 | + const doc = 'fragment Example on Query { { { syncField }'; |
| 80 | + const result = graphqlSync({ |
| 81 | + schema, |
| 82 | + source: doc, |
| 83 | + }); |
| 84 | + expect(result).to.containSubset({ |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'Syntax Error GraphQL request (1:29) Expected Name, found {\n\n' + |
| 89 | + '1: fragment Example on Query { { { syncField }\n' + |
| 90 | + ' ^\n', |
| 91 | + locations: [{ line: 1, column: 29 }], |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does not return a Promise for validation errors', () => { |
| 98 | + const doc = 'fragment Example on Query { unknownField }'; |
| 99 | + const result = graphqlSync({ |
| 100 | + schema, |
| 101 | + source: doc, |
| 102 | + }); |
| 103 | + expect(result).to.containSubset({ |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + message: |
| 107 | + 'Cannot query field "unknownField" on type "Query". Did you ' + |
| 108 | + 'mean "syncField" or "asyncField"?', |
| 109 | + locations: [{ line: 1, column: 29 }], |
| 110 | + }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('does not return a Promise for sync execution', () => { |
| 116 | + const doc = 'query Example { syncField }'; |
| 117 | + const result = graphqlSync({ |
| 118 | + schema, |
| 119 | + source: doc, |
| 120 | + rootValue: 'rootValue', |
| 121 | + }); |
| 122 | + expect(result).to.deep.equal({ data: { syncField: 'rootValue' } }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('throws if encountering async execution', () => { |
| 126 | + const doc = 'query Example { syncField, asyncField }'; |
| 127 | + expect(() => { |
| 128 | + graphqlSync({ |
| 129 | + schema, |
| 130 | + source: doc, |
| 131 | + rootValue: 'rootValue', |
| 132 | + }); |
| 133 | + }).to.throw('GraphQL execution failed to complete synchronously.'); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments