1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const graphql_1 = require("graphql");
|
4 | const types_1 = require("@babel/types");
|
5 | const types_2 = require("./types");
|
6 | const intermediates_1 = require("./intermediates");
|
7 | const genericTypes_1 = require("./genericTypes");
|
8 | const compact_1 = require("./compact");
|
9 | const generator_1 = require("@babel/generator");
|
10 | const types_3 = require("@babel/types");
|
11 | const types_4 = require("@babel/types");
|
12 | const types_5 = require("@babel/types");
|
13 | const types_6 = require("@babel/types");
|
14 | const objectPropertyForGraphQLInputField = (field) => types_1.objectProperty(types_1.identifier(field.name), types_1.identifier(field.name), false, true);
|
15 | const exactValueForGraphQLInputType = (type, fieldIdentifier) => {
|
16 | if (graphql_1.isInputObjectType(type)) {
|
17 | return types_4.logicalExpression("&&", fieldIdentifier, types_3.callExpression(types_1.identifier(type.name), [fieldIdentifier]));
|
18 | }
|
19 | else if (graphql_1.isNonNullType(type)) {
|
20 | const expression = exactValueForGraphQLInputType(type.ofType, fieldIdentifier);
|
21 | return types_5.isLogicalExpression(expression) ? expression.right : expression;
|
22 | }
|
23 | else if (graphql_1.isListType(type)) {
|
24 | const expression = exactValueForGraphQLInputType(type.ofType, fieldIdentifier);
|
25 | return types_1.isIdentifier(expression) && expression.name == fieldIdentifier.name
|
26 | ? expression
|
27 | : types_4.logicalExpression("&&", fieldIdentifier, types_6.memberExpression(fieldIdentifier, types_3.callExpression(types_1.identifier("map"), [
|
28 | types_1.arrowFunctionExpression([types_1.identifier("x")], exactValueForGraphQLInputType(type.ofType, types_1.identifier("x")))
|
29 | ])));
|
30 | }
|
31 | else {
|
32 | return fieldIdentifier;
|
33 | }
|
34 | };
|
35 | const exactPropertyForGraphQLInputField = (field) => types_1.objectProperty(types_1.identifier(field.name), exactValueForGraphQLInputType(field.type, types_1.identifier(field.name)), false, true);
|
36 | exports.typedIdentifier = (name, type) => (Object.assign({}, types_1.identifier(name), { typeAnnotation: types_1.TSTypeAnnotation(type) }));
|
37 | const identifierForVariable = (variable) => exports.typedIdentifier(variable.name, types_2.typeForInputType(intermediates_1.InputType(variable.type)));
|
38 | const objectPropertiesForOperation = (operation) => compact_1.default(types_1.objectProperty(types_1.identifier("query"), types_2.stringIdentifier(operation.operationName)), operation.variables.length > 0 &&
|
39 | types_1.objectProperty(types_1.identifier("variables"), types_1.objectExpression(operation.variables.map(variable => types_1.objectProperty(types_1.identifier(variable.name), graphql_1.isInputType(variable.type)
|
40 | ? exactValueForGraphQLInputType(variable.type, types_1.identifier(variable.name))
|
41 | : types_1.identifier(variable.name), false, true)))));
|
42 | exports.constructorDeclaration = (name, parameters, returnType, properties) => types_1.variableDeclaration("const", [
|
43 | types_1.variableDeclarator(types_1.identifier(name), Object.assign({}, types_1.arrowFunctionExpression(parameters, types_1.parenthesizedExpression(types_1.objectExpression(properties))), { returnType: types_1.TSTypeAnnotation(returnType) }))
|
44 | ]);
|
45 | exports.constructorDeclarationForGraphQLInputObjectType = (type) => ((fields) => exports.constructorDeclaration(type.name, [
|
46 | Object.assign({}, types_1.identifier(generator_1.default(types_1.objectExpression(fields.map(objectPropertyForGraphQLInputField))).code), { typeAnnotation: types_1.TSTypeAnnotation(types_2.typeReference(type.name)) })
|
47 | ], types_2.typeReference(type.name), fields.map(exactPropertyForGraphQLInputField)))(Object.values(type.getFields()));
|
48 | exports.constructorDeclarationForOperation = (operation) => exports.constructorDeclaration(operation.operationName, operation.variables.map(identifierForVariable), genericTypes_1.OperationType(types_2.typeReference(operation.operationName)), objectPropertiesForOperation(operation));
|
49 |
|
\ | No newline at end of file |