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 AddTypenameToAbstract = (function () {
|
15 | function AddTypenameToAbstract(targetSchema) {
|
16 | this.targetSchema = targetSchema;
|
17 | }
|
18 | AddTypenameToAbstract.prototype.transformRequest = function (originalRequest) {
|
19 | var document = addTypenameToAbstract(this.targetSchema, originalRequest.document);
|
20 | return __assign({}, originalRequest, { document: document });
|
21 | };
|
22 | return AddTypenameToAbstract;
|
23 | }());
|
24 | exports.default = AddTypenameToAbstract;
|
25 | function addTypenameToAbstract(targetSchema, document) {
|
26 | var _a;
|
27 | var typeInfo = new graphql_1.TypeInfo(targetSchema);
|
28 | return graphql_1.visit(document, graphql_1.visitWithTypeInfo(typeInfo, (_a = {},
|
29 | _a[graphql_1.Kind.SELECTION_SET] = function (node) {
|
30 | var parentType = typeInfo.getParentType();
|
31 | var selections = node.selections;
|
32 | if (parentType &&
|
33 | (parentType instanceof graphql_1.GraphQLInterfaceType ||
|
34 | parentType instanceof graphql_1.GraphQLUnionType) &&
|
35 | !selections.find(function (_) {
|
36 | return _.kind === graphql_1.Kind.FIELD &&
|
37 | _.name.value === '__typename';
|
38 | })) {
|
39 | selections = selections.concat({
|
40 | kind: graphql_1.Kind.FIELD,
|
41 | name: {
|
42 | kind: graphql_1.Kind.NAME,
|
43 | value: '__typename',
|
44 | },
|
45 | });
|
46 | }
|
47 | if (selections !== node.selections) {
|
48 | return __assign({}, node, { selections: selections });
|
49 | }
|
50 | },
|
51 | _a)));
|
52 | }
|
53 |
|
\ | No newline at end of file |