|
| 1 | +import { parse } from 'graphql' |
1 | 2 | import { Headers } from 'headers-utils/lib' |
2 | 3 | import { context } from '..' |
3 | 4 | import { createMockedRequest } from '../../test/support/utils' |
|
7 | 8 | GraphQLHandler, |
8 | 9 | GraphQLRequest, |
9 | 10 | GraphQLRequestBody, |
| 11 | + isDocumentNode, |
10 | 12 | } from './GraphQLHandler' |
11 | 13 | import { MockedRequest, ResponseResolver } from './RequestHandler' |
12 | 14 |
|
@@ -79,6 +81,51 @@ describe('info', () => { |
79 | 81 | expect(handler.info).toHaveProperty('operationType', 'mutation') |
80 | 82 | expect(handler.info).toHaveProperty('operationName', 'Login') |
81 | 83 | }) |
| 84 | + |
| 85 | + test('parses a query operation name from a given DocumentNode', () => { |
| 86 | + const node = parse(` |
| 87 | + query GetUser { |
| 88 | + user { |
| 89 | + firstName |
| 90 | + } |
| 91 | + } |
| 92 | + `) |
| 93 | + |
| 94 | + const handler = new GraphQLHandler('query', node, '*', resolver) |
| 95 | + |
| 96 | + expect(handler.info).toHaveProperty('header', 'query GetUser (origin: *)') |
| 97 | + expect(handler.info).toHaveProperty('operationType', 'query') |
| 98 | + expect(handler.info).toHaveProperty('operationName', 'GetUser') |
| 99 | + }) |
| 100 | + |
| 101 | + test('parses a mutation operation name from a given DocumentNode', () => { |
| 102 | + const node = parse(` |
| 103 | + mutation Login { |
| 104 | + user { |
| 105 | + id |
| 106 | + } |
| 107 | + } |
| 108 | + `) |
| 109 | + const handler = new GraphQLHandler('mutation', node, '*', resolver) |
| 110 | + |
| 111 | + expect(handler.info).toHaveProperty('header', 'mutation Login (origin: *)') |
| 112 | + expect(handler.info).toHaveProperty('operationType', 'mutation') |
| 113 | + expect(handler.info).toHaveProperty('operationName', 'Login') |
| 114 | + }) |
| 115 | + |
| 116 | + test('throws an exception given a DocumentNode with a mismatched operation type', () => { |
| 117 | + const node = parse(` |
| 118 | + mutation CreateUser { |
| 119 | + user { |
| 120 | + firstName |
| 121 | + } |
| 122 | + } |
| 123 | + `) |
| 124 | + |
| 125 | + expect(() => new GraphQLHandler('query', node, '*', resolver)).toThrow( |
| 126 | + 'Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "query", but got "mutation").', |
| 127 | + ) |
| 128 | + }) |
82 | 129 | }) |
83 | 130 |
|
84 | 131 | describe('parse', () => { |
@@ -371,3 +418,25 @@ describe('run', () => { |
371 | 418 | expect(result).toBeNull() |
372 | 419 | }) |
373 | 420 | }) |
| 421 | + |
| 422 | +describe('isDocumentNode', () => { |
| 423 | + it('returns true given a valid DocumentNode', () => { |
| 424 | + const node = parse(` |
| 425 | + query GetUser { |
| 426 | + user { |
| 427 | + login |
| 428 | + } |
| 429 | + } |
| 430 | + `) |
| 431 | + |
| 432 | + expect(isDocumentNode(node)).toEqual(true) |
| 433 | + }) |
| 434 | + |
| 435 | + it('returns false given an arbitrary input', () => { |
| 436 | + expect(isDocumentNode(null)).toEqual(false) |
| 437 | + expect(isDocumentNode(undefined)).toEqual(false) |
| 438 | + expect(isDocumentNode('')).toEqual(false) |
| 439 | + expect(isDocumentNode('value')).toEqual(false) |
| 440 | + expect(isDocumentNode(/value/)).toEqual(false) |
| 441 | + }) |
| 442 | +}) |
0 commit comments