1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const immutable_1 = require("immutable");
|
4 | const graphql_1 = require("graphql");
|
5 | exports.Typename = (possibleTypes) => ({
|
6 | kind: "Typename",
|
7 | possibleTypes
|
8 | });
|
9 | exports.List = (ofType) => ({ kind: "List", ofType });
|
10 | exports.Maybe = (ofType) => ({ kind: "Maybe", ofType });
|
11 | const InputObject = (name) => ({
|
12 | kind: "InputObject",
|
13 | name
|
14 | });
|
15 | const Enum = (type) => ({
|
16 | kind: "Enum",
|
17 | name: type.name,
|
18 | values: type.getValues().map(value => value.value)
|
19 | });
|
20 | const Scalar = (name, isTypeName) => ({
|
21 | kind: "Scalar",
|
22 | name,
|
23 | isTypename: isTypeName || false
|
24 | });
|
25 | const Leaf = (type) => graphql_1.isEnumType(type) ? Enum(type) : Scalar(type.name);
|
26 | const fail = (message) => {
|
27 | throw Error(message);
|
28 | };
|
29 | const FragmentOrSelectionOrLeaf = (type, selection) => selection
|
30 | ? selection
|
31 | : graphql_1.isCompositeType(type)
|
32 | ? fail("Composite type without selection.")
|
33 | : Leaf(type);
|
34 | const NonListOutputType = (type, selection) => graphql_1.isNonNullType(type)
|
35 | ? FragmentOrSelectionOrLeaf(type.ofType, selection)
|
36 | : exports.Maybe(FragmentOrSelectionOrLeaf(type, selection));
|
37 | const NonNullOutputType = (type, selection) => graphql_1.isListType(type)
|
38 | ? exports.List(NonListOutputType(type.ofType, selection))
|
39 | : FragmentOrSelectionOrLeaf(type, selection);
|
40 | const OutputType = (type, selection) => graphql_1.isNonNullType(type)
|
41 | ? NonNullOutputType(type.ofType, selection)
|
42 | : exports.Maybe(NonNullOutputType(type, selection));
|
43 | const InputObjectOrLeaf = (type) => graphql_1.isInputObjectType(type) ? InputObject(type.name) : Leaf(type);
|
44 | const NonListInputType = (type) => graphql_1.isNonNullType(type)
|
45 | ? InputObjectOrLeaf(type.ofType)
|
46 | : exports.Maybe(InputObjectOrLeaf(type));
|
47 | const NonNullInputType = (type) => graphql_1.isListType(type)
|
48 | ? exports.List(NonListInputType(type.ofType))
|
49 | : InputObjectOrLeaf(type);
|
50 | exports.InputType = (type) => graphql_1.isNonNullType(type)
|
51 | ? NonNullInputType(type.ofType)
|
52 | : exports.Maybe(NonNullInputType(type));
|
53 | const Field = (field, possibleTypes) => ({
|
54 | name: field.alias ? field.alias : field.name,
|
55 | type: field.name == "__typename"
|
56 | ? exports.Typename(possibleTypes)
|
57 | : OutputType(field.type, field.selectionSet && exports.InlineSelection(field.selectionSet))
|
58 | });
|
59 | exports.FragmentReference = (name, possibleTypes) => ({
|
60 | kind: "FragmentReference",
|
61 | name,
|
62 | possibleTypes
|
63 | });
|
64 | exports.InlineSelection = (selectionSet) => {
|
65 | const emptyType = {
|
66 | kind: "InlineSelection",
|
67 | possibleTypes: immutable_1.Set(selectionSet.possibleTypes.map(type => type.name)),
|
68 | intersections: [],
|
69 | fields: [],
|
70 | booleanConditions: [],
|
71 | typeConditions: []
|
72 | };
|
73 | return selectionSet.selections.reduce((type, selection) => {
|
74 | switch (selection.kind) {
|
75 | case "Field":
|
76 | return Object.assign({}, type, { fields: [...type.fields, Field(selection, type.possibleTypes)] });
|
77 | case "BooleanCondition":
|
78 | return Object.assign({}, type, { booleanConditions: [
|
79 | ...type.booleanConditions,
|
80 | exports.AnyObject(selection.selectionSet)
|
81 | ] });
|
82 | case "TypeCondition":
|
83 | const conditionalType = exports.AnyObject(selection.selectionSet);
|
84 | return conditionalType.possibleTypes.equals(type.possibleTypes)
|
85 | ? conditionalType.kind == "FragmentReference"
|
86 | ? Object.assign({}, type, { intersections: [...type.intersections, conditionalType] }) : Object.assign({}, type, { intersections: type.intersections.concat(conditionalType.intersections), fields: type.fields.concat(conditionalType.fields), booleanConditions: type.booleanConditions.concat(conditionalType.booleanConditions), typeConditions: type.typeConditions.concat(conditionalType.typeConditions) })
|
87 | : Object.assign({}, type, { typeConditions: [...type.typeConditions, conditionalType] });
|
88 | case "FragmentSpread":
|
89 | const possibleTypes = immutable_1.Set(selection.selectionSet.possibleTypes.map(type => type.name));
|
90 | const fragmentType = exports.FragmentReference(selection.fragmentName, possibleTypes);
|
91 | return possibleTypes.isSuperset(type.possibleTypes)
|
92 | ? Object.assign({}, type, { intersections: [...type.intersections, fragmentType] }) : Object.assign({}, type, { typeConditions: [...type.typeConditions, fragmentType] });
|
93 | }
|
94 | }, emptyType);
|
95 | };
|
96 | exports.AnyObject = (selectionSet) => {
|
97 | const unnamed = exports.InlineSelection(selectionSet);
|
98 | return unnamed.intersections.length == 1 &&
|
99 | unnamed.fields.length == 0 &&
|
100 | unnamed.booleanConditions.length == 0 &&
|
101 | unnamed.typeConditions.length == 0
|
102 | ? unnamed.intersections[0]
|
103 | : unnamed;
|
104 | };
|
105 |
|
\ | No newline at end of file |