-
Notifications
You must be signed in to change notification settings - Fork 6
/
createApolloMock.ts
145 lines (125 loc) · 4.21 KB
/
createApolloMock.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { FetchResult } from "@apollo/client";
import { MockedResponse } from "@apollo/client/testing";
import { GraphQLError, OperationDefinitionNode } from "graphql";
import { OperationVariables, TypedDocumentNode } from "./types";
export interface TypedMockedResponse<
TVariables extends OperationVariables,
TData
> extends MockedResponse {
request: {
query: TypedDocumentNode<TVariables, TData>;
variables: TVariables;
};
result?: FetchResult<TData>;
}
export interface ApolloMockOptions {
addTypename?: boolean;
scalarValues?: { [scalarTypeName: string]: unknown };
}
export type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
export type ApolloMockFn = {
<TVariables extends OperationVariables, TData>(
documentNode: TypedDocumentNode<TVariables, TData>,
variables?: RecursivePartial<TVariables>,
result?: FetchResult<RecursivePartial<TData>>,
options?: ApolloMockOptions
): TypedMockedResponse<TVariables, TData>;
<TVariables extends OperationVariables, TData>(
documentNode: TypedDocumentNode<TVariables, TData>,
variables: RecursivePartial<TVariables>,
error: Error,
options?: ApolloMockOptions
): TypedMockedResponse<TVariables, TData>;
};
const getDefaultScalarValue = ({
scalarTypeName,
mappedTypeName,
fieldName,
__typename,
scalarValues,
}: {
scalarTypeName: string;
mappedTypeName: string;
fieldName: string;
__typename: string;
scalarValues?: { [scalarTypeName: string]: unknown };
}) => {
if (scalarValues && scalarTypeName in scalarValues) {
const value = scalarValues[scalarTypeName];
if (value === undefined || value === null) {
throw new Error(
`Scalar value for "${scalarTypeName}" must not be null or undefined. Please fix "scalarValues" option.`
);
}
return value;
}
if (mappedTypeName === "number") {
return 1;
} else if (mappedTypeName === "string") {
return [__typename, fieldName].filter((v) => v).join("-");
} else if (mappedTypeName === "boolean") {
return false;
} else {
throw new Error(
`Could not generate default value for "${scalarTypeName}". ` +
`Please either provide a mapping in "scalarValues" option ` +
`or provide a mapping in "scalars" option of codegen plugins to map "${scalarTypeName}" to number, string or boolean.`
);
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default (operations: any): ApolloMockFn =>
<TVariables extends OperationVariables, TData>(
documentNode: TypedDocumentNode<TVariables, TData>,
variables?: RecursivePartial<TVariables>,
resultOrError?: FetchResult<RecursivePartial<TData>> | Error,
options?: ApolloMockOptions
): TypedMockedResponse<TVariables, TData> => {
const definitionNode = documentNode.definitions[0];
const operationNode = definitionNode as OperationDefinitionNode;
if (!operationNode.name) {
throw new Error("Missing operation name");
}
const operationName = operationNode.name.value;
const operation = operations[operationName];
if (!operation) {
throw new Error(`Couldn't find operation "${operationName}"`);
}
let error: Error | undefined;
let errors: ReadonlyArray<GraphQLError> | undefined;
let data: RecursivePartial<TData> | null | undefined;
if (resultOrError instanceof Error) {
error = resultOrError;
} else if (resultOrError) {
const result = resultOrError;
errors = result.errors;
data = result.data;
}
options = Object.assign(
{ addTypename: true, getDefaultScalarValue },
options || {}
);
const response: TypedMockedResponse<TVariables, TData> = {
request: {
query: documentNode,
variables: operation.variables(variables || undefined, options),
},
};
if (error) {
response.error = error;
} else if (errors) {
response.result = {};
response.result.errors = errors;
if (data === null) {
response.result.data = null;
} else if (data) {
response.result.data = operation.data(data, options);
}
} else {
response.result = {};
response.result.data = operation.data(data || undefined, options);
}
return response;
};