1 | var __assign = (this && this.__assign) || function () {
|
2 | __assign = Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | return __assign.apply(this, arguments);
|
11 | };
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var graphql_1 = require("graphql");
|
14 | var AddArgumentsAsVariablesTransform = (function () {
|
15 | function AddArgumentsAsVariablesTransform(schema, args) {
|
16 | this.schema = schema;
|
17 | this.args = args;
|
18 | }
|
19 | AddArgumentsAsVariablesTransform.prototype.transformRequest = function (originalRequest) {
|
20 | var _a = addVariablesToRootField(this.schema, originalRequest.document, this.args), document = _a.document, newVariables = _a.newVariables;
|
21 | var variables = __assign({}, originalRequest.variables, newVariables);
|
22 | return {
|
23 | document: document,
|
24 | variables: variables,
|
25 | };
|
26 | };
|
27 | return AddArgumentsAsVariablesTransform;
|
28 | }());
|
29 | exports.default = AddArgumentsAsVariablesTransform;
|
30 | function addVariablesToRootField(targetSchema, document, args) {
|
31 | var operations = document.definitions.filter(function (def) { return def.kind === graphql_1.Kind.OPERATION_DEFINITION; });
|
32 | var fragments = document.definitions.filter(function (def) { return def.kind === graphql_1.Kind.FRAGMENT_DEFINITION; });
|
33 | var variableNames = {};
|
34 | var newOperations = operations.map(function (operation) {
|
35 | var existingVariables = operation.variableDefinitions.map(function (variableDefinition) {
|
36 | return variableDefinition.variable.name.value;
|
37 | });
|
38 | var variableCounter = 0;
|
39 | var variables = {};
|
40 | var generateVariableName = function (argName) {
|
41 | var varName;
|
42 | do {
|
43 | varName = "_v" + variableCounter + "_" + argName;
|
44 | variableCounter++;
|
45 | } while (existingVariables.indexOf(varName) !== -1);
|
46 | return varName;
|
47 | };
|
48 | var type;
|
49 | if (operation.operation === 'subscription') {
|
50 | type = targetSchema.getSubscriptionType();
|
51 | }
|
52 | else if (operation.operation === 'mutation') {
|
53 | type = targetSchema.getMutationType();
|
54 | }
|
55 | else {
|
56 | type = targetSchema.getQueryType();
|
57 | }
|
58 | var newSelectionSet = [];
|
59 | operation.selectionSet.selections.forEach(function (selection) {
|
60 | if (selection.kind === graphql_1.Kind.FIELD) {
|
61 | var newArgs_1 = {};
|
62 | selection.arguments.forEach(function (argument) {
|
63 | newArgs_1[argument.name.value] = argument;
|
64 | });
|
65 | var name_1 = selection.name.value;
|
66 | var field = type.getFields()[name_1];
|
67 | field.args.forEach(function (argument) {
|
68 | if (argument.name in args) {
|
69 | var variableName = generateVariableName(argument.name);
|
70 | variableNames[argument.name] = variableName;
|
71 | newArgs_1[argument.name] = {
|
72 | kind: graphql_1.Kind.ARGUMENT,
|
73 | name: {
|
74 | kind: graphql_1.Kind.NAME,
|
75 | value: argument.name,
|
76 | },
|
77 | value: {
|
78 | kind: graphql_1.Kind.VARIABLE,
|
79 | name: {
|
80 | kind: graphql_1.Kind.NAME,
|
81 | value: variableName,
|
82 | },
|
83 | },
|
84 | };
|
85 | existingVariables.push(variableName);
|
86 | variables[variableName] = {
|
87 | kind: graphql_1.Kind.VARIABLE_DEFINITION,
|
88 | variable: {
|
89 | kind: graphql_1.Kind.VARIABLE,
|
90 | name: {
|
91 | kind: graphql_1.Kind.NAME,
|
92 | value: variableName,
|
93 | },
|
94 | },
|
95 | type: typeToAst(argument.type),
|
96 | };
|
97 | }
|
98 | });
|
99 | newSelectionSet.push(__assign({}, selection, { arguments: Object.keys(newArgs_1).map(function (argName) { return newArgs_1[argName]; }) }));
|
100 | }
|
101 | else {
|
102 | newSelectionSet.push(selection);
|
103 | }
|
104 | });
|
105 | return __assign({}, operation, { variableDefinitions: operation.variableDefinitions.concat(Object.keys(variables).map(function (varName) { return variables[varName]; })), selectionSet: {
|
106 | kind: graphql_1.Kind.SELECTION_SET,
|
107 | selections: newSelectionSet,
|
108 | } });
|
109 | });
|
110 | var newVariables = {};
|
111 | Object.keys(variableNames).forEach(function (name) {
|
112 | newVariables[variableNames[name]] = args[name];
|
113 | });
|
114 | return {
|
115 | document: __assign({}, document, { definitions: newOperations.concat(fragments) }),
|
116 | newVariables: newVariables,
|
117 | };
|
118 | }
|
119 | function typeToAst(type) {
|
120 | if (type instanceof graphql_1.GraphQLNonNull) {
|
121 | var innerType = typeToAst(type.ofType);
|
122 | if (innerType.kind === graphql_1.Kind.LIST_TYPE ||
|
123 | innerType.kind === graphql_1.Kind.NAMED_TYPE) {
|
124 | return {
|
125 | kind: graphql_1.Kind.NON_NULL_TYPE,
|
126 | type: innerType,
|
127 | };
|
128 | }
|
129 | else {
|
130 | throw new Error('Incorrent inner non-null type');
|
131 | }
|
132 | }
|
133 | else if (type instanceof graphql_1.GraphQLList) {
|
134 | return {
|
135 | kind: graphql_1.Kind.LIST_TYPE,
|
136 | type: typeToAst(type.ofType),
|
137 | };
|
138 | }
|
139 | else {
|
140 | return {
|
141 | kind: graphql_1.Kind.NAMED_TYPE,
|
142 | name: {
|
143 | kind: graphql_1.Kind.NAME,
|
144 | value: type.toString(),
|
145 | },
|
146 | };
|
147 | }
|
148 | }
|
149 |
|
\ | No newline at end of file |