|
| 1 | +import { ApolloServer } from 'apollo-server-fastify'; |
| 2 | +import fastify from 'fastify'; |
| 3 | +import { DocumentNode, print } from 'graphql'; |
| 4 | + |
| 5 | +export type StringOrAst = string | DocumentNode; |
| 6 | + |
| 7 | +export type TestClientConfig = { |
| 8 | + // The ApolloServer instance that will be used for handling the queries you run in your tests. |
| 9 | + // Must be an instance of the ApolloServer class from `apollo-server-fastify` (or a compatible subclass). |
| 10 | + apolloServer: ApolloServer; |
| 11 | + // Extends the mocked Request object with additional data. |
| 12 | + // Useful when your apolloServer `context` option is a callback that operates on the passed in `req` key, |
| 13 | + // and you want to inject data into that `req` object. |
| 14 | + requestOptions?: fastify.HTTPInjectOptions; |
| 15 | +}; |
| 16 | + |
| 17 | +export type SetOptionsFn = (options: { |
| 18 | + requestOptions?: fastify.HTTPInjectOptions; |
| 19 | +}) => void; |
| 20 | + |
| 21 | +type Query = { |
| 22 | + query: StringOrAst; |
| 23 | + mutation?: undefined; |
| 24 | + variables?: { |
| 25 | + [name: string]: any; |
| 26 | + }; |
| 27 | + operationName?: string; |
| 28 | +}; |
| 29 | +type Mutation = { |
| 30 | + mutation: StringOrAst; |
| 31 | + query?: undefined; |
| 32 | + variables?: { |
| 33 | + [name: string]: any; |
| 34 | + }; |
| 35 | + operationName?: string; |
| 36 | +}; |
| 37 | + |
| 38 | +export interface ApolloServerTestClient { |
| 39 | + query: (query: Query) => Promise<fastify.HTTPInjectResponse>; |
| 40 | + mutate: (mutation: Mutation) => Promise<fastify.HTTPInjectResponse>; |
| 41 | + setOptions: SetOptionsFn, |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * This function takes in an apollo server instance and returns a function that you can use to run operations |
| 46 | + * against your schema, and assert on the results. |
| 47 | + * @example |
| 48 | + * const apolloServer = await createApolloServer({ schema }) |
| 49 | + * const query = createTestClient({ |
| 50 | + * apolloServer, |
| 51 | + * }); |
| 52 | + * @param {TestClientConfig} options |
| 53 | + * @returns {ApolloServerTestClient} |
| 54 | + */ |
| 55 | +export function createTestClient({ |
| 56 | + apolloServer, |
| 57 | + requestOptions = {}, |
| 58 | +}: TestClientConfig): ApolloServerTestClient { |
| 59 | + const app = fastify(); |
| 60 | + app.register(apolloServer.createHandler()); |
| 61 | + |
| 62 | + let mockRequestOptions = requestOptions; |
| 63 | + |
| 64 | + /** |
| 65 | + * Set the options after TestClient creation |
| 66 | + * Useful when you don't want to create a new instance just for a specific change in the request. |
| 67 | + */ |
| 68 | + const setOptions: SetOptionsFn = ({ |
| 69 | + requestOptions, |
| 70 | + }) => { |
| 71 | + if (requestOptions) { |
| 72 | + mockRequestOptions = requestOptions; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * Run an operation against the server |
| 78 | + * @example |
| 79 | + * const result = await query({ |
| 80 | + * query: `{ currentUser { id } }` |
| 81 | + * }) |
| 82 | + * |
| 83 | + * expect(result.json()).toEqual({ |
| 84 | + * data: { |
| 85 | + * currentUser: { |
| 86 | + * id: '1' |
| 87 | + * } |
| 88 | + * } |
| 89 | + * }); |
| 90 | + * @param {Query | Mutation} params |
| 91 | + * @returns {fastify.HTTPInjectResponse} fastify response |
| 92 | + */ |
| 93 | + const test = ({ query, mutation, ...args }: Query | Mutation): Promise<fastify.HTTPInjectResponse> => { |
| 94 | + const operation = query || mutation; |
| 95 | + |
| 96 | + if (!operation || (query && mutation)) { |
| 97 | + throw new Error( |
| 98 | + 'Either `query` or `mutation` must be passed, but not both.', |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + const opts: fastify.HTTPInjectOptions = { |
| 103 | + url: apolloServer.graphqlPath, |
| 104 | + method: 'POST', |
| 105 | + payload: { |
| 106 | + query: typeof operation === 'string' ? operation : print(operation), |
| 107 | + ...args, |
| 108 | + }, |
| 109 | + ...mockRequestOptions, |
| 110 | + }; |
| 111 | + |
| 112 | + return app.inject(opts); |
| 113 | + }; |
| 114 | + |
| 115 | + return { |
| 116 | + query: test, |
| 117 | + mutate: test, |
| 118 | + setOptions |
| 119 | + }; |
| 120 | +} |
0 commit comments