UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const t = __importStar(require("@babel/types"));
11const graphql_tool_utilities_1 = require("graphql-tool-utilities");
12const context_1 = require("./context");
13const utilities_1 = require("./utilities");
14const language_1 = require("./language");
15const generate = require('@babel/generator').default;
16function printDocument({ path, operation, fragments }, ast, options) {
17 const file = new context_1.FileContext(path, options);
18 if (operation == null) {
19 const fileBody = fragments.reduce((statements, fragment) => {
20 const context = new context_1.OperationContext(fragment, ast, options, file);
21 const body = language_1.tsInterfaceBodyForObjectField(fragment, fragment.typeCondition, new utilities_1.ObjectStack(fragment.typeCondition), context);
22 const { namespace } = context;
23 return [
24 ...statements,
25 ...(namespace ? [t.exportNamedDeclaration(namespace, [])] : []),
26 t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(context.typeName), null, null, body), []),
27 ];
28 }, []);
29 const { schemaImports } = file;
30 if (schemaImports) {
31 fileBody.unshift(schemaImports);
32 }
33 return generate(t.file(t.program(fileBody), [], [])).code;
34 }
35 const context = new context_1.OperationContext(operation, ast, options, file);
36 const partialContext = new context_1.OperationContext(operation, ast, Object.assign({}, options, { partial: true }), file);
37 let rootType;
38 if (operation.operationType === graphql_tool_utilities_1.OperationType.Query) {
39 rootType = ast.schema.getQueryType();
40 }
41 else if (operation.operationType === graphql_tool_utilities_1.OperationType.Mutation) {
42 rootType = ast.schema.getMutationType();
43 }
44 else {
45 rootType = ast.schema.getSubscriptionType();
46 }
47 const variables = operation.variables.filter(graphql_tool_utilities_1.isTypedVariable).length > 0
48 ? context.export(language_1.variablesInterface(operation.variables, context))
49 : null;
50 const operationInterface = t.tsInterfaceDeclaration(t.identifier(context.typeName), null, null, language_1.tsInterfaceBodyForObjectField(operation, rootType, new utilities_1.ObjectStack(rootType), context));
51 const operationPartialInterface = t.tsInterfaceDeclaration(t.identifier(partialContext.typeName), null, null, language_1.tsInterfaceBodyForObjectField(operation, rootType, new utilities_1.ObjectStack(rootType), partialContext));
52 const { schemaImports } = file;
53 const { namespace } = context;
54 const { namespace: partialNamespace } = partialContext;
55 const documentNodeImport = t.importDeclaration([
56 t.importSpecifier(t.identifier('DocumentNode'), t.identifier('DocumentNode')),
57 ], t.stringLiteral('graphql-typed'));
58 const documentNodeDeclaratorIdentifier = t.identifier('document');
59 documentNodeDeclaratorIdentifier.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('DocumentNode'), t.tsTypeParameterInstantiation([
60 t.tsTypeReference(t.identifier(context.typeName)),
61 variables || t.tsNeverKeyword(),
62 t.tsTypeReference(t.identifier(partialContext.typeName)),
63 ])));
64 const documentNodeDeclaration = t.variableDeclaration('const', [
65 t.variableDeclarator(documentNodeDeclaratorIdentifier),
66 ]);
67 documentNodeDeclaration.declare = true;
68 const documentNodeExport = t.exportDefaultDeclaration(t.identifier('document'));
69 const fileBody = [documentNodeImport];
70 if (schemaImports) {
71 fileBody.push(schemaImports);
72 }
73 if (partialNamespace) {
74 fileBody.push(t.exportNamedDeclaration(partialNamespace, []));
75 }
76 fileBody.push(t.exportNamedDeclaration(operationPartialInterface, []));
77 if (namespace) {
78 fileBody.push(t.exportNamedDeclaration(namespace, []));
79 }
80 fileBody.push(t.exportNamedDeclaration(operationInterface, []), documentNodeDeclaration, documentNodeExport);
81 return generate(t.file(t.program(fileBody), [], [])).code;
82}
83exports.printDocument = printDocument;