|
7 | 7 |
|
8 | 8 | import { expect } from 'chai';
|
9 | 9 | import { describe, it } from 'mocha';
|
| 10 | +import { graphqlSync } from '../../graphql'; |
10 | 11 | import { execute } from '../execute';
|
11 | 12 | import { parse } from '../../language';
|
12 | 13 | import { GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../type';
|
@@ -72,4 +73,64 @@ describe('Execute: synchronously when possible', () => {
|
72 | 73 | data: { syncField: 'rootValue', asyncField: 'rootValue' },
|
73 | 74 | });
|
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 | + }); |
75 | 136 | });
|
0 commit comments