1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const intermediates_1 = require("./intermediates");
|
4 | const immutable_1 = require("immutable");
|
5 | const compact_1 = require("./compact");
|
6 | const globalDependency = (name) => ({
|
7 | type: "global",
|
8 | name
|
9 | });
|
10 | const outputTypeDependencies = (type) => {
|
11 | switch (type.kind) {
|
12 | case "Maybe":
|
13 | return [
|
14 | globalDependency("Maybe"),
|
15 | ...outputTypeDependencies(type.ofType)
|
16 | ];
|
17 | case "List":
|
18 | return outputTypeDependencies(type.ofType);
|
19 | case "FragmentReference":
|
20 | return [fragmentReferenceDependency(type)];
|
21 | case "InlineSelection":
|
22 | return inlineSelectionDependencies(type);
|
23 | case "Enum":
|
24 | return [globalDependency(type.name)];
|
25 | case "Scalar":
|
26 | return [];
|
27 | case "Typename":
|
28 | return [];
|
29 | }
|
30 | };
|
31 | const inputTypeDependencies = (type) => {
|
32 | switch (type.kind) {
|
33 | case "Maybe":
|
34 | return [
|
35 | globalDependency("Optional"),
|
36 | ...inputTypeDependencies(type.ofType)
|
37 | ];
|
38 | case "List":
|
39 | return inputTypeDependencies(type.ofType);
|
40 | case "InputObject":
|
41 | return [globalDependency(type.name)];
|
42 | case "Enum":
|
43 | return [globalDependency(type.name)];
|
44 | case "Scalar":
|
45 | return [];
|
46 | }
|
47 | };
|
48 | const fragmentReferenceDependency = (reference) => ({
|
49 | type: "fragment-type",
|
50 | name: reference.name
|
51 | });
|
52 | const fragmentStringDependency = (name) => ({
|
53 | type: "fragment-string",
|
54 | name
|
55 | });
|
56 | const inlineSelectionDependencies = (selection) => compact_1.default(...selection.intersections.map(fragmentReferenceDependency), ...selection.fields.flatMap(field => outputTypeDependencies(field.type)), ...selection.booleanConditions.flatMap(anyObjectDependencies), ...selection.typeConditions.flatMap(anyObjectDependencies), selection.typeConditions.length > 0 && globalDependency("If"));
|
57 | const selectionFragmentStringDependencies = (selection) => compact_1.default(selection.kind == "FragmentSpread" &&
|
58 | fragmentStringDependency(selection.fragmentName), ...(selection.selectionSet
|
59 | ? selection.selectionSet.selections.flatMap(selectionFragmentStringDependencies)
|
60 | : []));
|
61 | const anyObjectDependencies = (object) => object.kind == "InlineSelection"
|
62 | ? inlineSelectionDependencies(object)
|
63 | : [fragmentReferenceDependency(object)];
|
64 | const dedupedDependenciesOfType = (dependencies, type) => immutable_1.Set(dependencies
|
65 | .filter(dependency => dependency.type == type)
|
66 | .map(dependency => dependency.name)).toArray();
|
67 | const variableDependencies = (variable) => inputTypeDependencies(intermediates_1.InputType(variable.type));
|
68 | const allDependencies = (selectionSet, variables) => [
|
69 | ...inlineSelectionDependencies(intermediates_1.InlineSelection(selectionSet)),
|
70 | ...(variables
|
71 | ? [
|
72 | ...variables.flatMap(variableDependencies),
|
73 | ...selectionSet.selections.flatMap(selectionFragmentStringDependencies),
|
74 | globalDependency("Operation")
|
75 | ]
|
76 | : [])
|
77 | ];
|
78 | const fragmentDependencies = (dependencies) => {
|
79 | const fragmentTypes = immutable_1.Set(dependencies
|
80 | .filter(dependency => dependency.type == "fragment-type")
|
81 | .map(dependency => dependency.name));
|
82 | let fragmentStrings = immutable_1.Set(dependencies
|
83 | .filter(dependency => dependency.type == "fragment-string")
|
84 | .map(dependency => dependency.name));
|
85 | return fragmentTypes
|
86 | .map(fragmentType => {
|
87 | const importString = fragmentStrings.contains(fragmentType);
|
88 | fragmentStrings = fragmentStrings.remove(fragmentType);
|
89 | return { name: fragmentType, importType: true, importString };
|
90 | })
|
91 | .toArray()
|
92 | .concat(fragmentStrings
|
93 | .map(fragmentString => ({
|
94 | name: fragmentString,
|
95 | importType: false,
|
96 | importString: true
|
97 | }))
|
98 | .toArray());
|
99 | };
|
100 | const mapDependencies = (dependencies) => ({
|
101 | global: dedupedDependenciesOfType(dependencies, "global"),
|
102 | fragments: fragmentDependencies(dependencies)
|
103 | });
|
104 | const Dependencies = (selectionSet, variables) => mapDependencies(allDependencies(selectionSet, variables));
|
105 | exports.default = Dependencies;
|
106 |
|
\ | No newline at end of file |