1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var graphql_1 = require("graphql");
|
3 | var resolveFromParentTypename_1 = require("./resolveFromParentTypename");
|
4 | var backcompatOptions = { commentDescriptions: true };
|
5 | function typeFromAST(node) {
|
6 | switch (node.kind) {
|
7 | case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
|
8 | return makeObjectType(node);
|
9 | case graphql_1.Kind.INTERFACE_TYPE_DEFINITION:
|
10 | return makeInterfaceType(node);
|
11 | case graphql_1.Kind.ENUM_TYPE_DEFINITION:
|
12 | return makeEnumType(node);
|
13 | case graphql_1.Kind.UNION_TYPE_DEFINITION:
|
14 | return makeUnionType(node);
|
15 | case graphql_1.Kind.SCALAR_TYPE_DEFINITION:
|
16 | return makeScalarType(node);
|
17 | case graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION:
|
18 | return makeInputObjectType(node);
|
19 | case graphql_1.Kind.DIRECTIVE_DEFINITION:
|
20 | return makeDirective(node);
|
21 | default:
|
22 | return null;
|
23 | }
|
24 | }
|
25 | exports.default = typeFromAST;
|
26 | function makeObjectType(node) {
|
27 | return new graphql_1.GraphQLObjectType({
|
28 | name: node.name.value,
|
29 | fields: function () { return makeFields(node.fields); },
|
30 | interfaces: function () {
|
31 | return node.interfaces.map(function (iface) { return createNamedStub(iface.name.value, 'interface'); });
|
32 | },
|
33 | description: graphql_1.getDescription(node, backcompatOptions),
|
34 | });
|
35 | }
|
36 | function makeInterfaceType(node) {
|
37 | return new graphql_1.GraphQLInterfaceType({
|
38 | name: node.name.value,
|
39 | fields: function () { return makeFields(node.fields); },
|
40 | description: graphql_1.getDescription(node, backcompatOptions),
|
41 | resolveType: function (parent, context, info) {
|
42 | return resolveFromParentTypename_1.default(parent, info.schema);
|
43 | },
|
44 | });
|
45 | }
|
46 | function makeEnumType(node) {
|
47 | var values = {};
|
48 | node.values.forEach(function (value) {
|
49 | values[value.name.value] = {
|
50 | description: graphql_1.getDescription(value, backcompatOptions),
|
51 | };
|
52 | });
|
53 | return new graphql_1.GraphQLEnumType({
|
54 | name: node.name.value,
|
55 | values: values,
|
56 | description: graphql_1.getDescription(node, backcompatOptions),
|
57 | });
|
58 | }
|
59 | function makeUnionType(node) {
|
60 | return new graphql_1.GraphQLUnionType({
|
61 | name: node.name.value,
|
62 | types: function () {
|
63 | return node.types.map(function (type) { return resolveType(type, 'object'); });
|
64 | },
|
65 | description: graphql_1.getDescription(node, backcompatOptions),
|
66 | resolveType: function (parent, context, info) {
|
67 | return resolveFromParentTypename_1.default(parent, info.schema);
|
68 | },
|
69 | });
|
70 | }
|
71 | function makeScalarType(node) {
|
72 | return new graphql_1.GraphQLScalarType({
|
73 | name: node.name.value,
|
74 | description: graphql_1.getDescription(node, backcompatOptions),
|
75 | serialize: function () { return null; },
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | parseValue: function () { return false; },
|
81 | parseLiteral: function () { return false; },
|
82 | });
|
83 | }
|
84 | function makeInputObjectType(node) {
|
85 | return new graphql_1.GraphQLInputObjectType({
|
86 | name: node.name.value,
|
87 | fields: function () { return makeValues(node.fields); },
|
88 | description: graphql_1.getDescription(node, backcompatOptions),
|
89 | });
|
90 | }
|
91 | function makeFields(nodes) {
|
92 | var result = {};
|
93 | nodes.forEach(function (node) {
|
94 | var deprecatedDirective = node.directives.find(function (directive) {
|
95 | return directive && directive.name && directive.name.value === 'deprecated';
|
96 | });
|
97 | var deprecatedArgument = deprecatedDirective &&
|
98 | deprecatedDirective.arguments &&
|
99 | deprecatedDirective.arguments.find(function (arg) { return arg && arg.name && arg.name.value === 'reason'; });
|
100 | var deprecationReason = deprecatedArgument &&
|
101 | deprecatedArgument.value &&
|
102 | deprecatedArgument.value.value;
|
103 | result[node.name.value] = {
|
104 | type: resolveType(node.type, 'object'),
|
105 | args: makeValues(node.arguments),
|
106 | description: graphql_1.getDescription(node, backcompatOptions),
|
107 | deprecationReason: deprecationReason,
|
108 | };
|
109 | });
|
110 | return result;
|
111 | }
|
112 | function makeValues(nodes) {
|
113 | var result = {};
|
114 | nodes.forEach(function (node) {
|
115 | var type = resolveType(node.type, 'input');
|
116 | result[node.name.value] = {
|
117 | type: type,
|
118 | defaultValue: graphql_1.valueFromAST(node.defaultValue, type),
|
119 | description: graphql_1.getDescription(node, backcompatOptions),
|
120 | };
|
121 | });
|
122 | return result;
|
123 | }
|
124 | function resolveType(node, type) {
|
125 | switch (node.kind) {
|
126 | case graphql_1.Kind.LIST_TYPE:
|
127 | return new graphql_1.GraphQLList(resolveType(node.type, type));
|
128 | case graphql_1.Kind.NON_NULL_TYPE:
|
129 | return new graphql_1.GraphQLNonNull(resolveType(node.type, type));
|
130 | default:
|
131 | return createNamedStub(node.name.value, type);
|
132 | }
|
133 | }
|
134 | function createNamedStub(name, type) {
|
135 | var constructor;
|
136 | if (type === 'object') {
|
137 | constructor = graphql_1.GraphQLObjectType;
|
138 | }
|
139 | else if (type === 'interface') {
|
140 | constructor = graphql_1.GraphQLInterfaceType;
|
141 | }
|
142 | else {
|
143 | constructor = graphql_1.GraphQLInputObjectType;
|
144 | }
|
145 | return new constructor({
|
146 | name: name,
|
147 | fields: {
|
148 | __fake: {
|
149 | type: graphql_1.GraphQLString,
|
150 | },
|
151 | },
|
152 | });
|
153 | }
|
154 | function makeDirective(node) {
|
155 | var locations = [];
|
156 | node.locations.forEach(function (location) {
|
157 | if (location.value in graphql_1.DirectiveLocation) {
|
158 | locations.push(location.value);
|
159 | }
|
160 | });
|
161 | return new graphql_1.GraphQLDirective({
|
162 | name: node.name.value,
|
163 | description: node.description ? node.description.value : null,
|
164 | args: makeValues(node.arguments),
|
165 | locations: locations,
|
166 | });
|
167 | }
|
168 |
|
\ | No newline at end of file |