/
createTestClient.ts
50 lines (42 loc) 路 1.68 KB
/
createTestClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ApolloServerBase } from 'apollo-server-core';
import { GraphQLResponse as GraphQLResponseType } from 'apollo-server-types';
import { print, DocumentNode } from 'graphql';
type StringOrAst = string | DocumentNode;
// A query must not come with a mutation (and vice versa).
type Query<TVariables = Record<string, any>> = {
query: StringOrAst;
mutation?: undefined;
variables?: TVariables;
operationName?: string;
};
type Mutation<TVariables = Record<string, any>> = {
mutation: StringOrAst;
query?: undefined;
variables?: TVariables;
operationName?: string;
};
type GraphQLResponse<TData> = Omit<GraphQLResponseType, 'data'> & {
data?: TData;
};
export interface ApolloServerTestClient {
query<TData = any, TVariables = Record<string, any>>(query: Query<TVariables>): Promise<GraphQLResponse<TData>>;
mutate<TData = any, TVariables = Record<string, any>>(mutation: Mutation<TVariables>): Promise<GraphQLResponse<TData>>;
}
export default (server: ApolloServerBase): ApolloServerTestClient => {
const executeOperation = server.executeOperation.bind(server);
const test = ({ query, mutation, ...args }: Query | Mutation) => {
const operation = query || mutation;
if (!operation || (query && mutation)) {
throw new Error(
'Either `query` or `mutation` must be passed, but not both.',
);
}
return executeOperation({
// Convert ASTs, which are produced by `graphql-tag` but not currently
// used by `executeOperation`, to a String using `graphql/language/print`.
query: typeof operation === 'string' ? operation : print(operation),
...args,
});
};
return { query: test, mutate: test } as ApolloServerTestClient;
};