|
| 1 | +import { DocumentNode } from 'graphql' |
1 | 2 | import { Headers } from 'headers-utils/lib' |
2 | 3 | import { context } from '..' |
3 | 4 | import { createMockedRequest } from '../../test/support/utils' |
| 5 | +import { GetUserDetailDocument, LoginDocument } from '../graphql.test-data' |
4 | 6 | import { response } from '../response' |
5 | 7 | import { |
6 | 8 | GraphQLContext, |
@@ -79,6 +81,134 @@ describe('info', () => { |
79 | 81 | expect(handler.info).toHaveProperty('operationType', 'mutation') |
80 | 82 | expect(handler.info).toHaveProperty('operationName', 'Login') |
81 | 83 | }) |
| 84 | + |
| 85 | + test('parses operation name out of DocumentNode for query', () => { |
| 86 | + const handler = new GraphQLHandler( |
| 87 | + 'query', |
| 88 | + GetUserDetailDocument, |
| 89 | + '*', |
| 90 | + resolver, |
| 91 | + ) |
| 92 | + |
| 93 | + expect(handler.info).toHaveProperty( |
| 94 | + 'header', |
| 95 | + 'query GetUserDetail (origin: *)', |
| 96 | + ) |
| 97 | + expect(handler.info).toHaveProperty('operationType', 'query') |
| 98 | + expect(handler.info).toHaveProperty('operationName', 'GetUserDetail') |
| 99 | + }) |
| 100 | + |
| 101 | + test('parses operation name out of DocumentNode for mutation', () => { |
| 102 | + const handler = new GraphQLHandler('mutation', LoginDocument, '*', resolver) |
| 103 | + |
| 104 | + expect(handler.info).toHaveProperty('header', 'mutation Login (origin: *)') |
| 105 | + expect(handler.info).toHaveProperty('operationType', 'mutation') |
| 106 | + expect(handler.info).toHaveProperty('operationName', 'Login') |
| 107 | + }) |
| 108 | + |
| 109 | + test('throws exception for mismatch operation type on DocumentNode for query', () => { |
| 110 | + const getUserDetailDocument: DocumentNode = { |
| 111 | + kind: 'Document', |
| 112 | + definitions: [ |
| 113 | + { |
| 114 | + kind: 'OperationDefinition', |
| 115 | + operation: 'mutation', |
| 116 | + name: { kind: 'Name', value: 'GetUserDetail' }, |
| 117 | + variableDefinitions: [ |
| 118 | + { |
| 119 | + kind: 'VariableDefinition', |
| 120 | + variable: { |
| 121 | + kind: 'Variable', |
| 122 | + name: { kind: 'Name', value: 'userId' }, |
| 123 | + }, |
| 124 | + type: { |
| 125 | + kind: 'NonNullType', |
| 126 | + type: { |
| 127 | + kind: 'NamedType', |
| 128 | + name: { kind: 'Name', value: 'String' }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + selectionSet: { |
| 134 | + kind: 'SelectionSet', |
| 135 | + selections: [ |
| 136 | + { |
| 137 | + kind: 'Field', |
| 138 | + name: { kind: 'Name', value: 'user' }, |
| 139 | + selectionSet: { |
| 140 | + kind: 'SelectionSet', |
| 141 | + selections: [ |
| 142 | + { kind: 'Field', name: { kind: 'Name', value: 'id' } }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + }, |
| 149 | + ], |
| 150 | + } |
| 151 | + expect( |
| 152 | + () => new GraphQLHandler('query', getUserDetailDocument, '*', resolver), |
| 153 | + ).toThrowError() |
| 154 | + }) |
| 155 | + |
| 156 | + test('throws exception for mismatch operation type on DocumentNode for mutation', () => { |
| 157 | + const loginDocument: DocumentNode = { |
| 158 | + kind: 'Document', |
| 159 | + definitions: [ |
| 160 | + { |
| 161 | + kind: 'OperationDefinition', |
| 162 | + operation: 'query', |
| 163 | + name: { kind: 'Name', value: 'Login' }, |
| 164 | + variableDefinitions: [ |
| 165 | + { |
| 166 | + kind: 'VariableDefinition', |
| 167 | + variable: { |
| 168 | + kind: 'Variable', |
| 169 | + name: { kind: 'Name', value: 'username' }, |
| 170 | + }, |
| 171 | + type: { |
| 172 | + kind: 'NonNullType', |
| 173 | + type: { |
| 174 | + kind: 'NamedType', |
| 175 | + name: { kind: 'Name', value: 'String' }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + ], |
| 180 | + selectionSet: { |
| 181 | + kind: 'SelectionSet', |
| 182 | + selections: [ |
| 183 | + { |
| 184 | + kind: 'Field', |
| 185 | + name: { kind: 'Name', value: 'login' }, |
| 186 | + arguments: [ |
| 187 | + { |
| 188 | + kind: 'Argument', |
| 189 | + name: { kind: 'Name', value: 'username' }, |
| 190 | + value: { |
| 191 | + kind: 'Variable', |
| 192 | + name: { kind: 'Name', value: 'username' }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + ], |
| 196 | + selectionSet: { |
| 197 | + kind: 'SelectionSet', |
| 198 | + selections: [ |
| 199 | + { kind: 'Field', name: { kind: 'Name', value: 'id' } }, |
| 200 | + ], |
| 201 | + }, |
| 202 | + }, |
| 203 | + ], |
| 204 | + }, |
| 205 | + }, |
| 206 | + ], |
| 207 | + } |
| 208 | + expect( |
| 209 | + () => new GraphQLHandler('mutation', loginDocument, '*', resolver), |
| 210 | + ).toThrowError() |
| 211 | + }) |
82 | 212 | }) |
83 | 213 |
|
84 | 214 | describe('parse', () => { |
|
0 commit comments