UNPKG

2.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getBuiltInForStub = exports.isNamedStub = exports.createStub = exports.createNamedStub = void 0;
4const graphql_1 = require("graphql");
5function createNamedStub(name, type) {
6 let constructor;
7 if (type === 'object') {
8 constructor = graphql_1.GraphQLObjectType;
9 }
10 else if (type === 'interface') {
11 constructor = graphql_1.GraphQLInterfaceType;
12 }
13 else {
14 constructor = graphql_1.GraphQLInputObjectType;
15 }
16 return new constructor({
17 name,
18 fields: {
19 _fake: {
20 type: graphql_1.GraphQLString,
21 },
22 },
23 });
24}
25exports.createNamedStub = createNamedStub;
26function createStub(node, type) {
27 switch (node.kind) {
28 case graphql_1.Kind.LIST_TYPE:
29 return new graphql_1.GraphQLList(createStub(node.type, type));
30 case graphql_1.Kind.NON_NULL_TYPE:
31 return new graphql_1.GraphQLNonNull(createStub(node.type, type));
32 default:
33 if (type === 'output') {
34 return createNamedStub(node.name.value, 'object');
35 }
36 return createNamedStub(node.name.value, 'input');
37 }
38}
39exports.createStub = createStub;
40function isNamedStub(type) {
41 if ('getFields' in type) {
42 const fields = type.getFields();
43 // eslint-disable-next-line no-unreachable-loop
44 for (const fieldName in fields) {
45 const field = fields[fieldName];
46 return field.name === '_fake';
47 }
48 }
49 return false;
50}
51exports.isNamedStub = isNamedStub;
52function getBuiltInForStub(type) {
53 switch (type.name) {
54 case graphql_1.GraphQLInt.name:
55 return graphql_1.GraphQLInt;
56 case graphql_1.GraphQLFloat.name:
57 return graphql_1.GraphQLFloat;
58 case graphql_1.GraphQLString.name:
59 return graphql_1.GraphQLString;
60 case graphql_1.GraphQLBoolean.name:
61 return graphql_1.GraphQLBoolean;
62 case graphql_1.GraphQLID.name:
63 return graphql_1.GraphQLID;
64 default:
65 return type;
66 }
67}
68exports.getBuiltInForStub = getBuiltInForStub;