UNPKG

4.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const graphql_1 = require("graphql");
4const types_1 = require("@babel/types");
5const types_2 = require("./types");
6const intermediates_1 = require("./intermediates");
7const genericTypes_1 = require("./genericTypes");
8const compact_1 = require("./compact");
9const generator_1 = require("@babel/generator");
10const types_3 = require("@babel/types");
11const types_4 = require("@babel/types");
12const types_5 = require("@babel/types");
13const types_6 = require("@babel/types");
14const objectPropertyForGraphQLInputField = (field) => types_1.objectProperty(types_1.identifier(field.name), types_1.identifier(field.name), false, true);
15const 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};
35const exactPropertyForGraphQLInputField = (field) => types_1.objectProperty(types_1.identifier(field.name), exactValueForGraphQLInputType(field.type, types_1.identifier(field.name)), false, true);
36exports.typedIdentifier = (name, type) => (Object.assign({}, types_1.identifier(name), { typeAnnotation: types_1.TSTypeAnnotation(type) }));
37const identifierForVariable = (variable) => exports.typedIdentifier(variable.name, types_2.typeForInputType(intermediates_1.InputType(variable.type)));
38const 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)))));
42exports.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]);
45exports.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()));
48exports.constructorDeclarationForOperation = (operation) => exports.constructorDeclaration(operation.operationName, operation.variables.map(identifierForVariable), genericTypes_1.OperationType(types_2.typeReference(operation.operationName)), objectPropertiesForOperation(operation));
49//# sourceMappingURL=constructors.js.map
\No newline at end of file