|
1 |
| -import { ApolloServer, HeaderMap } from '..'; |
2 |
| -import type { ApolloServerOptions } from '..'; |
| 1 | +import type { GatewayInterface } from '@apollo/server-gateway-interface'; |
| 2 | +import { makeExecutableSchema } from '@graphql-tools/schema'; |
| 3 | +import { describe, expect, it, jest } from '@jest/globals'; |
| 4 | +import assert from 'assert'; |
3 | 5 | import {
|
4 | 6 | FormattedExecutionResult,
|
5 | 7 | GraphQLError,
|
6 | 8 | GraphQLSchema,
|
7 |
| - parse, |
8 | 9 | TypedQueryDocumentNode,
|
| 10 | + parse, |
9 | 11 | } from 'graphql';
|
| 12 | +import gql from 'graphql-tag'; |
| 13 | +import type { ApolloServerOptions } from '..'; |
| 14 | +import { ApolloServer, HeaderMap } from '..'; |
10 | 15 | import type { ApolloServerPlugin, BaseContext } from '../externalTypes';
|
| 16 | +import type { GraphQLResponseBody } from '../externalTypes/graphql'; |
11 | 17 | import { ApolloServerPluginCacheControlDisabled } from '../plugin/disabled/index.js';
|
12 | 18 | import { ApolloServerPluginUsageReporting } from '../plugin/usageReporting/index.js';
|
13 |
| -import { makeExecutableSchema } from '@graphql-tools/schema'; |
14 | 19 | import { mockLogger } from './mockLogger.js';
|
15 |
| -import gql from 'graphql-tag'; |
16 |
| -import type { GatewayInterface } from '@apollo/server-gateway-interface'; |
17 |
| -import { jest, describe, it, expect } from '@jest/globals'; |
18 |
| -import type { GraphQLResponseBody } from '../externalTypes/graphql'; |
19 |
| -import assert from 'assert'; |
20 | 20 |
|
21 | 21 | const typeDefs = gql`
|
22 | 22 | type Query {
|
@@ -176,6 +176,100 @@ describe('ApolloServer construction', () => {
|
176 | 176 | `);
|
177 | 177 | await server.stop();
|
178 | 178 | });
|
| 179 | + |
| 180 | + it('async stringifyResult', async () => { |
| 181 | + const server = new ApolloServer({ |
| 182 | + typeDefs, |
| 183 | + resolvers, |
| 184 | + stringifyResult: async (value: FormattedExecutionResult) => { |
| 185 | + let result = await Promise.resolve( |
| 186 | + JSON.stringify(value, null, 10000), |
| 187 | + ); |
| 188 | + result = result.replace('world', 'stringifyResults works!'); // replace text with something custom |
| 189 | + return result; |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + await server.start(); |
| 194 | + |
| 195 | + const request = { |
| 196 | + httpGraphQLRequest: { |
| 197 | + method: 'POST', |
| 198 | + headers: new HeaderMap([['content-type', 'application-json']]), |
| 199 | + body: { query: '{ hello }' }, |
| 200 | + search: '', |
| 201 | + }, |
| 202 | + context: async () => ({}), |
| 203 | + }; |
| 204 | + |
| 205 | + const { body } = await server.executeHTTPGraphQLRequest(request); |
| 206 | + assert(body.kind === 'complete'); |
| 207 | + expect(body.string).toMatchInlineSnapshot(` |
| 208 | + "{ |
| 209 | + "data": { |
| 210 | + "hello": "stringifyResults works!" |
| 211 | + } |
| 212 | + }" |
| 213 | + `); |
| 214 | + await server.stop(); |
| 215 | + }); |
| 216 | + |
| 217 | + it('throws the custom parsed error from stringifyResult', async () => { |
| 218 | + const server = new ApolloServer({ |
| 219 | + typeDefs, |
| 220 | + resolvers, |
| 221 | + stringifyResult: (_: FormattedExecutionResult) => { |
| 222 | + throw new Error('A custom synchronous error'); |
| 223 | + }, |
| 224 | + }); |
| 225 | + |
| 226 | + await server.start(); |
| 227 | + |
| 228 | + const request = { |
| 229 | + httpGraphQLRequest: { |
| 230 | + method: 'POST', |
| 231 | + headers: new HeaderMap([['content-type', 'application-json']]), |
| 232 | + body: { query: '{ error }' }, |
| 233 | + search: '', |
| 234 | + }, |
| 235 | + context: async () => ({}), |
| 236 | + }; |
| 237 | + |
| 238 | + await expect( |
| 239 | + server.executeHTTPGraphQLRequest(request), |
| 240 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 241 | + `"A custom synchronous error"`, |
| 242 | + ); |
| 243 | + |
| 244 | + await server.stop(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('throws the custom parsed error from async stringifyResult', async () => { |
| 248 | + const server = new ApolloServer({ |
| 249 | + typeDefs, |
| 250 | + resolvers, |
| 251 | + stringifyResult: async (_: FormattedExecutionResult) => |
| 252 | + Promise.reject('A custom asynchronous error'), |
| 253 | + }); |
| 254 | + |
| 255 | + await server.start(); |
| 256 | + |
| 257 | + const request = { |
| 258 | + httpGraphQLRequest: { |
| 259 | + method: 'POST', |
| 260 | + headers: new HeaderMap([['content-type', 'application-json']]), |
| 261 | + body: { query: '{ error }' }, |
| 262 | + search: '', |
| 263 | + }, |
| 264 | + context: async () => ({}), |
| 265 | + }; |
| 266 | + |
| 267 | + await expect( |
| 268 | + server.executeHTTPGraphQLRequest(request), |
| 269 | + ).rejects.toMatchInlineSnapshot(`"A custom asynchronous error"`); |
| 270 | + |
| 271 | + await server.stop(); |
| 272 | + }); |
179 | 273 | });
|
180 | 274 |
|
181 | 275 | it('throws when an API key is not a valid header value', () => {
|
@@ -501,7 +595,7 @@ describe('ApolloServer executeOperation', () => {
|
501 | 595 |
|
502 | 596 | const { body, http } = await server.executeOperation({
|
503 | 597 | query: `#graphql
|
504 |
| - query NeedsArg($arg: CompoundInput!) { needsCompoundArg(aCompound: $arg) } |
| 598 | + query NeedsArg($arg: CompoundInput!) { needsCompoundArg(aCompound: $arg) } |
505 | 599 | `,
|
506 | 600 | // @ts-expect-error for `null` case
|
507 | 601 | variables,
|
|
0 commit comments