1 | "use strict";
|
2 | var definition_1 = require("graphql/type/definition");
|
3 | var utils_1 = require("./utils");
|
4 | var pascalCase = require("pascal-case");
|
5 | var ignoredScalars = ['Boolean', 'Float', 'String', 'ID', 'Int'];
|
6 | exports.buildArgumentsType = function (primitivesMap, fieldName, typeName, argumentsArr) {
|
7 | if (argumentsArr === void 0) { argumentsArr = []; }
|
8 | var argsModel = {
|
9 | imports: [],
|
10 | name: pascalCase(fieldName) + typeName,
|
11 | fields: [],
|
12 | isEnum: false,
|
13 | isObject: true,
|
14 | isArgumentsType: true,
|
15 | isCustomScalar: false
|
16 | };
|
17 | argsModel.fields = argumentsArr.map(function (argDefinition) {
|
18 | var type = utils_1.getTypeName(primitivesMap, argDefinition.type);
|
19 | return {
|
20 | name: argDefinition.name,
|
21 | type: type,
|
22 | isArray: utils_1.isArray(argDefinition.type),
|
23 | isRequired: utils_1.isRequired(argDefinition.type)
|
24 | };
|
25 | });
|
26 | return argsModel;
|
27 | };
|
28 | exports.handleType = function (schema, primitivesMap, type) {
|
29 | var typeName = type['name'];
|
30 | var resultArr = [];
|
31 | var currentType = {
|
32 | imports: [],
|
33 | name: typeName,
|
34 | fields: [],
|
35 | isEnum: false,
|
36 | isObject: false,
|
37 | isCustomScalar: false
|
38 | };
|
39 | resultArr.push(currentType);
|
40 | if (!utils_1.shouldSkip(typeName)) {
|
41 | if (type instanceof definition_1.GraphQLEnumType) {
|
42 | currentType.isEnum = true;
|
43 | currentType.enumValues = type.getValues().map(function (enumItem) {
|
44 | return {
|
45 | name: enumItem.name,
|
46 | description: enumItem.description,
|
47 | value: enumItem.value
|
48 | };
|
49 | });
|
50 | }
|
51 | else if (type instanceof definition_1.GraphQLObjectType || type instanceof definition_1.GraphQLInputObjectType || type instanceof definition_1.GraphQLInterfaceType) {
|
52 | currentType.isInput = type instanceof definition_1.GraphQLInputObjectType;
|
53 | currentType.isObject = true;
|
54 | var fields_1 = type.getFields();
|
55 | if (type instanceof definition_1.GraphQLObjectType) {
|
56 | currentType.implementedInterfaces = type.getInterfaces().map(function (interf) {
|
57 | return interf.name;
|
58 | });
|
59 | currentType.hasImplementedInterfaces = currentType.implementedInterfaces.length > 0;
|
60 | }
|
61 | currentType.fields = Object
|
62 | .keys(fields_1)
|
63 | .map(function (fieldName) { return fields_1[fieldName]; })
|
64 | .map(function (field) {
|
65 | var type = utils_1.getTypeName(primitivesMap, field.type);
|
66 | var fieldArguments = field.args || [];
|
67 | if (fieldArguments.length > 0) {
|
68 | resultArr.push(exports.buildArgumentsType(primitivesMap, field.name, typeName, fieldArguments));
|
69 | }
|
70 | if (!utils_1.isPrimitive(primitivesMap, type)) {
|
71 | currentType.imports.push(type);
|
72 | }
|
73 | return {
|
74 | name: field.name,
|
75 | type: type,
|
76 | isArray: utils_1.isArray(field.type),
|
77 | isRequired: utils_1.isRequired(field.type)
|
78 | };
|
79 | });
|
80 | }
|
81 | else if (type instanceof definition_1.GraphQLUnionType) {
|
82 | currentType.name = type.name || typeName;
|
83 | currentType.isUnion = true;
|
84 | currentType.isObject = false;
|
85 | currentType.unionTypes = type.getTypes().map(function (type) { return type.name; });
|
86 | currentType.hasUnionTypes = currentType.unionTypes.length > 0;
|
87 | }
|
88 | else if (type instanceof definition_1.GraphQLList || type instanceof definition_1.GraphQLNonNull) {
|
89 | return exports.handleType(schema, primitivesMap, definition_1.getNamedType(type));
|
90 | }
|
91 | else if (type instanceof definition_1.GraphQLScalarType && ignoredScalars.indexOf(currentType.name) === -1) {
|
92 | currentType.isCustomScalar = true;
|
93 | }
|
94 | return resultArr;
|
95 | }
|
96 | else {
|
97 | return null;
|
98 | }
|
99 | };
|
100 |
|
\ | No newline at end of file |