-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
280 lines (280 loc) · 13.9 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
function plugin(schema, documents, config) {
var inputTypeNames = {};
return join.apply(void 0, [disclaimer,
imports,
''].concat(documents.map(function (documentFile) { return formatDocumentFile(documentFile); }), ['',
formatInputNames(),
boilerplate]));
function formatInputNames() {
findRecursiveInputReferences(Object.keys(inputTypeNames));
if (Object.keys(inputTypeNames).length == 0)
return '';
return join.apply(void 0, ['/*',
' * GraphQL InputTypes',
' */',
''].concat(Object.keys(inputTypeNames).map(function (name) {
var node = schema.getType(name);
return formatGraphQLInputObjectType(node);
})));
}
function findRecursiveInputReferences(newNames) {
if (newNames.length == 0)
return;
var newNames2 = [];
for (var _i = 0, newNames_1 = newNames; _i < newNames_1.length; _i++) {
var name_1 = newNames_1[_i];
var node = schema.getType(name_1);
var fieldMap = node.getFields();
var fields = Object.values(fieldMap);
for (var _a = 0, fields_1 = fields; _a < fields_1.length; _a++) {
var field = fields_1[_a];
handleInputType(field.type);
}
}
findRecursiveInputReferences(newNames2);
function handleInputType(node) {
if (graphql_1.isNonNullType(node)) {
handleInputType(node.ofType);
return;
}
if (graphql_1.isListType(node)) {
handleInputType(node.ofType);
return;
}
if (graphql_1.isInputObjectType(node)) {
if (!inputTypeNames[node.name]) {
inputTypeNames[node.name] = true;
newNames2.push(node.name);
}
return;
}
}
}
function formatGraphQLInputObjectType(node) {
var fieldMap = node.getFields();
var fields = Object.values(fieldMap);
return join.apply(void 0, ['interface ' + node.name + '{'].concat(fields.map(function (field) { return formatInputField(field); }), ['}\n']));
}
function formatInputField(field) {
var isRequired = graphql_1.isNonNullType(field.type);
var questionMark = isRequired ? '' : '?';
return field.name + questionMark + ': ' + formatInputType(field.type);
}
function formatInputType(node) {
if (graphql_1.isNonNullType(node)) {
return node.ofType;
}
if (graphql_1.isListType(node)) {
return 'ReadonlyArray<' + formatInputType(node.ofType) + '>';
}
if (graphql_1.isEnumType(node)) {
// ! TODO
throw 'Unhandled GraphQLEnumType in formatInputType';
}
if (graphql_1.isScalarType(node)) {
return formatGraphQLScalarType(node);
}
if (graphql_1.isInputObjectType(node)) {
return node.name;
}
throw 'Unhandled GraphQLInputType';
}
function formatDocumentFile(file) {
return join('', '', '/*', ' * Operations from ' + file.filePath, ' */', '', formatDocumentNode(file.content));
}
function formatDocumentNode(node) {
return node.definitions
.map(function (definitionNode) { return formatDefinition(definitionNode); })
.join('\n');
}
function formatDefinition(node) {
switch (node.kind) {
case 'OperationDefinition':
return formatOperationDefinition(node);
default:
throw 'unhandled DefinitionNode.kind = ' + node.kind;
}
}
function formatOperationDefinition(node) {
return ('export const ' +
node.name.value +
' = ' +
definerFunction(node.operation) +
'<' +
formatVariableDefinitions(node.variableDefinitions) +
',' +
formatSelectionSet(node.selectionSet, selectSchemaObject(node.operation)) +
'>(gql`\n' +
indent(formatLoc(node.loc), ' ') +
'\n`)\n');
function definerFunction(operation) {
switch (operation) {
case 'query':
return 'defineQuery';
case 'mutation':
return 'defineMutation';
case 'subscription':
throw 'TODO';
}
}
}
function formatLoc(loc) {
return loc.source.body.substring(loc.start, loc.end);
}
function formatVariableDefinitions(nodes) {
var list = nodes.map(function (variableNode) {
return formatVariableDefinition(variableNode);
});
return join.apply(void 0, ['{', ' /* variables */'].concat(list, ['}']));
}
function formatVariableDefinition(node) {
// todo defaultValue
var isRequired = node.type.kind == 'NonNullType';
var questionMark = isRequired ? '' : '?';
return (node.variable.name.value + questionMark + ': ' + formatTypeNode(node.type));
}
function formatTypeNode(node) {
if (node.kind == 'NonNullType') {
return formatTypeNodeNotNull(node.type);
}
return 'null | ' + formatTypeNodeNotNull(node);
}
function formatTypeNodeNotNull(node) {
switch (node.kind) {
case 'ListType':
return 'Array<' + formatTypeNode(node.type) + '>';
case 'NamedType':
return formatNameTypeNode(node);
}
}
function formatNameTypeNode(node) {
switch (node.name.value) {
case 'String':
return 'string';
case 'Int':
return 'number';
case 'Float':
return 'number';
case 'Boolean':
return 'boolean';
case 'ID':
return config.idType || 'string';
default:
var nodeType = schema.getType(node.name.value).astNode.kind;
if (nodeType == 'ScalarTypeDefinition') {
return formatCustomScalar(node.name.value);
}
else {
inputTypeNames[node.name.value] = true;
return node.name.value;
}
}
}
function formatSelectionSet(node, schemaObject) {
var list = node.selections.map(function (selectionNode) {
return formatSelectionNode(selectionNode, schemaObject, ' ');
});
return join.apply(void 0, ['{', ' /* data */'].concat(list, ['}']));
}
function formatSelectionNode(node, schemaObject, offset) {
switch (node.kind) {
case 'Field':
return formatFieldNode(node, schemaObject, offset);
default:
throw 'unhandled SelectionNode.kind = ' + node.kind;
}
}
function formatFieldNode(node, schemaObject, offset) {
var schemaName = node.name.value;
var aliasName = node.alias ? node.alias.value : schemaName;
var schemaField = schemaObject.getFields()[schemaName];
var isRequired = graphql_1.isNonNullType(schemaField.type);
var questionMark = isRequired ? '' : '?';
return (offset +
aliasName +
questionMark +
': ' +
formatGraphQLOutputType(schemaField.type, node.selectionSet, offset + ' '));
}
function formatGraphQLOutputType(type, selectionSet, offset) {
if (graphql_1.isNonNullType(type)) {
return formatGraphQLOutputTypeNotNull(type.ofType, selectionSet, offset);
}
return ('null | ' + formatGraphQLOutputTypeNotNull(type, selectionSet, offset));
}
function formatGraphQLOutputTypeNotNull(type, selectionSet, offset) {
if (graphql_1.isScalarType(type)) {
return formatGraphQLScalarType(type);
}
if (graphql_1.isObjectType(type)) {
return formatGraphQLObjectType(type, selectionSet, offset);
}
if (graphql_1.isListType(type)) {
return ('Array<' +
formatGraphQLOutputType(type.ofType, selectionSet, offset) +
'>');
}
if (graphql_1.isEnumType(type)) {
var enumValues = type.getValues();
return enumValues.map(function (value) { return "'" + value.name + "'"; }).join(' | ');
}
throw 'unhandled GraphQLOutputType "' + type + '"';
}
function formatGraphQLScalarType(type) {
switch (type.name) {
case 'String':
return 'string';
case 'Int':
return 'number';
case 'Float':
return 'number';
case 'Boolean':
return 'boolean';
case 'ID':
return config.idType || 'string';
default:
return formatCustomScalar(type.name);
}
}
function formatCustomScalar(name) {
var customName = config.scalarTypes && config.scalarTypes[name];
return customName || 'any';
}
function formatGraphQLObjectType(type, selectionSet, offset) {
var list = selectionSet.selections.map(function (selectionNode) {
return formatSelectionNode(selectionNode, type, offset);
});
return join.apply(void 0, ['{'].concat(list, ['}']));
}
function selectSchemaObject(operation) {
switch (operation) {
case 'query':
return schema.getQueryType();
case 'mutation':
return schema.getMutationType();
case 'subscription':
return schema.getSubscriptionType();
}
}
}
exports.plugin = plugin;
var disclaimer = "\n/*\n * This file was generated by graphql-code-generator with the apollo-hooks-codegen plugin.\n * Any changes made to the file will be overwritten.\n */\n";
var imports = "\nimport * as React from 'react'\nimport { createContext, useEffect, useState, useContext } from 'react'\nimport ApolloClient, {\n MutationOptions,\n ObservableQuery,\n WatchQueryOptions,\n} from 'apollo-client'\nimport { FetchResult } from 'apollo-link'\nimport { DocumentNode } from 'graphql'\nimport gql from 'graphql-tag'\n";
var boilerplate = "\n/*\n * Boilerplate\n */\n\ntype Omit<T, K> = Pick<T, Exclude<keyof T, K>>\ntype Error = any\ntype QueryOpts<V> = Omit<WatchQueryOptions<V>, 'query'>\ntype MutateOpts<D, V> = Omit<MutationOptions<D, V>, 'mutation'>\n\n// We grab the ApolloClient from this context within our hooks\ntype ContextType = { apolloClient?: ApolloClient<any> }\nconst apolloContext = createContext<ContextType>({})\n\n// Must be inserted at the root of all components that want to use the hook\n// functions supplied by this file.\nexport function ApolloHooksProvider({\n children,\n apolloClient,\n}: {\n children: React.ReactNode\n apolloClient?: ApolloClient<any> | undefined\n}) {\n const elementType = apolloContext.Provider\n const elementProps: React.ProviderProps<ContextType> = {\n value: { apolloClient },\n }\n return React.createElement(elementType, elementProps, children)\n}\n\n// Converts a gql-snippet into a user-callable function that takes options,\n// which can then be passed to useApolloQuery to execute the query.\nfunction defineQuery<V, D>(doc: DocumentNode) {\n return function configureQuery(opts: QueryOpts<V> = {}) {\n return function executeQuery(client: ApolloClient<any>) {\n return client.watchQuery<D>({ query: doc, ...opts })\n }\n }\n}\n\n// Executes a query that has been created by calling the exported function with\n// the same name as the query operation.\n// The React Hooks rules apply - this function must be called unconditionally\n// within a functional React Component and will rerender the component whenever\n// the query result changes.\nexport function useApolloQuery<D, V>(\n configuredQuery: (client: ApolloClient<any>) => ObservableQuery<D, V>\n): [D | undefined, Error | undefined] {\n const { apolloClient } = useContext(apolloContext)\n if (!apolloClient) throw 'No ApolloClient provided'\n\n const watchQuery = configuredQuery(apolloClient)\n\n const [data, setData] = useState<D | undefined>(undefined)\n const [error, setError] = useState<Error | undefined>(undefined)\n useEffect(() => {\n const subscription = watchQuery.subscribe(\n result => setData(result.data),\n error => setError(error)\n )\n return () => subscription.unsubscribe()\n }, [])\n\n return [data, error]\n}\n\n// Converts a gql-snippet into a user-callable function that takes options,\n// which can then be passed to useApolloMutation to provide the mutate function.\nfunction defineMutation<V, D>(mutation: DocumentNode) {\n return function configureMutation(opts: MutateOpts<D, V> = {}) {\n return function loadMutation(client: ApolloClient<any>) {\n return function executeMutation(opts2: MutateOpts<D, V> = {}) {\n return client.mutate<D>({ mutation, ...opts, ...opts2 })\n }\n }\n }\n}\n\n// Prepares a mutate function when supplied with the exported function with\n// the same name as the mutation operation.\n// The React Hooks rules apply - this function must be called unconditionally\n// within a functional React Component.\nexport function useApolloMutation<D, V>(\n configuredMutation: (\n client: ApolloClient<any>\n ) => (opts?: MutateOpts<D, V>) => Promise<FetchResult<D>>\n) {\n const { apolloClient } = useContext(apolloContext)\n if (!apolloClient) throw 'No ApolloClient provided'\n const mutate = configuredMutation(apolloClient)\n return mutate\n}\n";
function join() {
var lines = [];
for (var _i = 0; _i < arguments.length; _i++) {
lines[_i] = arguments[_i];
}
return lines.join('\n');
}
function indent(multilineText, offset) {
return multilineText
.trim()
.split('\n')
.map(function (line) { return offset + line; })
.join('\n');
}