@newmo/graphql-fake-server 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -1
- package/dist/esm/schema-scanner.d.ts.map +1 -1
- package/dist/esm/schema-scanner.js +72 -28
- package/dist/esm/schema-scanner.js.map +1 -1
- package/package.json +1 -1
- package/src/schema-scanner.ts +82 -31
package/README.md
CHANGED
|
@@ -11,7 +11,66 @@ GraphQL Fake Server.
|
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
1. Add `@exampleID`, `@exampleString`, `@exampleInt`, `@exampleFloat`, `@exampleBoolean` directive to your schema.
|
|
15
|
+
|
|
16
|
+
```graphql
|
|
17
|
+
"""
|
|
18
|
+
@exampleID directive specifies an example value for a ID field.
|
|
19
|
+
This example value is used in the fake data.
|
|
20
|
+
ID value will be unique between all ID fake data.
|
|
21
|
+
"""
|
|
22
|
+
directive @exampleID(
|
|
23
|
+
"""
|
|
24
|
+
The value of the ID field.
|
|
25
|
+
@exampleID(value: "id")
|
|
26
|
+
"""
|
|
27
|
+
value: ID!
|
|
28
|
+
) on FIELD_DEFINITION
|
|
29
|
+
"""
|
|
30
|
+
@exampleString directive specifies an example value for a String field.
|
|
31
|
+
This example value is used in the fake data.
|
|
32
|
+
"""
|
|
33
|
+
directive @exampleString(
|
|
34
|
+
"""
|
|
35
|
+
The value of the String field.
|
|
36
|
+
@exampleString(value: "example")
|
|
37
|
+
"""
|
|
38
|
+
value: String!
|
|
39
|
+
) on FIELD_DEFINITION
|
|
40
|
+
"""
|
|
41
|
+
@exampleInt directive specifies an example value for a Inf field.
|
|
42
|
+
This example value is used in the fake data.
|
|
43
|
+
"""
|
|
44
|
+
directive @exampleInt(
|
|
45
|
+
"""
|
|
46
|
+
The value of the Int field.
|
|
47
|
+
@exampleInt(value: 1)
|
|
48
|
+
"""
|
|
49
|
+
value: Int!
|
|
50
|
+
) on FIELD_DEFINITION
|
|
51
|
+
"""
|
|
52
|
+
@exampleFloat directive specifies an example value for a Float field.
|
|
53
|
+
This example value is used in the fake data.
|
|
54
|
+
"""
|
|
55
|
+
directive @exampleFloat(
|
|
56
|
+
"""
|
|
57
|
+
The value of the Float field.
|
|
58
|
+
@exampleFloat(value: 1.0)
|
|
59
|
+
"""
|
|
60
|
+
value: Float!
|
|
61
|
+
) on FIELD_DEFINITION
|
|
62
|
+
"""
|
|
63
|
+
@exampleBoolean directive specifies an example value for a Boolean field.
|
|
64
|
+
This example value is used in the fake data.
|
|
65
|
+
"""
|
|
66
|
+
directive @exampleBoolean(
|
|
67
|
+
"""
|
|
68
|
+
The value of the Boolean field.
|
|
69
|
+
@exampleBoolean(value: true)
|
|
70
|
+
"""
|
|
71
|
+
value: Boolean!
|
|
72
|
+
) on FIELD_DEFINITION
|
|
73
|
+
```
|
|
15
74
|
|
|
16
75
|
## Usage
|
|
17
76
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-scanner.d.ts","sourceRoot":"","sources":["../../src/schema-scanner.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,aAAa,EAWhB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"schema-scanner.d.ts","sourceRoot":"","sources":["../../src/schema-scanner.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,aAAa,EAWhB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AA8CrC,KAAK,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AACvD,KAAK,UAAU,GAAG,cAAc,EAAE,CAAC;AACnC,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,GAAG,UAAU,CAAC,CAAC;AAC/D,MAAM,MAAM,qBAAqB,GAAG;IAChC,KAAK,EAAE,cAAc,GAAG,UAAU,GAAG,WAAW,CAAA;CACnD,CAAC;AACF,MAAM,MAAM,yBAAyB,GAAG;IACpC,UAAU,EAAE,MAAM,CAAA;CACrB,CAAA;AACD,MAAM,MAAM,gBAAgB,GAAG,qBAAqB,GAAG,yBAAyB,CAAC;AA2LjF,KAAK,SAAS,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;CAC1C,CAAA;AACD,MAAM,MAAM,cAAc,GAAG;IACzB,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,EAAE,CAAC;CACvB,CAAC;AACF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAA;CACzC,CAAC;AACF,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,gBAAgB,CAAC;AAEzD,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,QAAQ,EAAE,CAsE9E"}
|
|
@@ -9,6 +9,14 @@ function convertName(node, config) {
|
|
|
9
9
|
convertedName += config.typesSuffix;
|
|
10
10
|
return convertedName;
|
|
11
11
|
}
|
|
12
|
+
const createIDFactory = () => {
|
|
13
|
+
const idMap = new Map();
|
|
14
|
+
return (name) => {
|
|
15
|
+
const count = idMap.get(name) ?? 0;
|
|
16
|
+
idMap.set(name, count + 1);
|
|
17
|
+
return `${name}${count}`;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
12
20
|
const parseTypeNodeStructure = (node) => {
|
|
13
21
|
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
14
22
|
return parseTypeNodeStructure(node.type);
|
|
@@ -78,7 +86,7 @@ function valueOf(value) {
|
|
|
78
86
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
79
87
|
throw new Error(`Unknown kind of value ${value}`);
|
|
80
88
|
}
|
|
81
|
-
const typeToFunction = (type, config) => {
|
|
89
|
+
const typeToFunction = (type, config, idFactory) => {
|
|
82
90
|
switch (type) {
|
|
83
91
|
case "String":
|
|
84
92
|
return `"${config.defaultValues.String}"`;
|
|
@@ -89,71 +97,101 @@ const typeToFunction = (type, config) => {
|
|
|
89
97
|
case "Boolean":
|
|
90
98
|
return `${config.defaultValues.Boolean ? "true" : "false"}`;
|
|
91
99
|
case "ID":
|
|
92
|
-
return `"${config.defaultValues.ID}"`;
|
|
100
|
+
return `"${idFactory(config.defaultValues.ID)}"`;
|
|
93
101
|
default:
|
|
94
102
|
// reference to the object
|
|
95
103
|
return `${type}`;
|
|
96
104
|
}
|
|
97
105
|
};
|
|
98
|
-
const typeToFunctionWithArray = (type, config) => {
|
|
99
|
-
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config)})`;
|
|
106
|
+
const typeToFunctionWithArray = (type, config, idFactory) => {
|
|
107
|
+
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config, idFactory)})`;
|
|
100
108
|
};
|
|
101
109
|
// NamedType/ListType handling
|
|
102
|
-
const nodeToExpression = ({ currentNode, isArray = false, config }) => {
|
|
110
|
+
const nodeToExpression = ({ currentNode, isArray = false, config, idFactory }) => {
|
|
103
111
|
if (currentNode.kind === "NonNullType") {
|
|
104
112
|
return nodeToExpression({
|
|
105
|
-
currentNode: currentNode.type,
|
|
113
|
+
currentNode: currentNode.type,
|
|
114
|
+
isArray,
|
|
115
|
+
config,
|
|
116
|
+
idFactory
|
|
106
117
|
});
|
|
107
118
|
}
|
|
108
119
|
else if (currentNode.kind === "NamedType") {
|
|
109
120
|
if (isArray) {
|
|
110
121
|
return {
|
|
111
|
-
expression: typeToFunctionWithArray(currentNode.name.value, config)
|
|
122
|
+
expression: typeToFunctionWithArray(currentNode.name.value, config, idFactory)
|
|
112
123
|
};
|
|
113
124
|
}
|
|
114
125
|
else {
|
|
115
126
|
return {
|
|
116
|
-
expression: typeToFunction(currentNode.name.value, config)
|
|
127
|
+
expression: typeToFunction(currentNode.name.value, config, idFactory)
|
|
117
128
|
};
|
|
118
129
|
}
|
|
119
130
|
}
|
|
120
131
|
else if (currentNode.kind === "ListType") {
|
|
121
|
-
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config });
|
|
132
|
+
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config, idFactory });
|
|
122
133
|
}
|
|
123
134
|
throw new Error("Unknown node kind");
|
|
124
135
|
};
|
|
125
|
-
|
|
126
|
-
|
|
136
|
+
const SUPPORTED_EXAMPLE_DIRECTIVES = ["exampleID", "exampleString", "exampleInt", "exampleFloat", "exampleBoolean"];
|
|
137
|
+
const isIdType = (node) => {
|
|
138
|
+
if (node.kind === "NonNullType") {
|
|
139
|
+
return isIdType(node.type);
|
|
140
|
+
}
|
|
141
|
+
else if (node.kind === "NamedType") {
|
|
142
|
+
return node.name.value === "ID";
|
|
143
|
+
}
|
|
144
|
+
else if (node.kind === "ListType") {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
};
|
|
149
|
+
function parseFieldOrInputValueDefinition({ node, convertedTypeName, config, idFactory }) {
|
|
150
|
+
const exampleDirective = node.directives?.find(d => {
|
|
151
|
+
return SUPPORTED_EXAMPLE_DIRECTIVES.includes(d.name.value);
|
|
152
|
+
});
|
|
127
153
|
// fake
|
|
128
154
|
// if @example directive is not found, return random value for the scalar type
|
|
129
155
|
if (!exampleDirective) {
|
|
130
156
|
return {
|
|
131
|
-
example: nodeToExpression({ currentNode: node.type, config })
|
|
157
|
+
example: nodeToExpression({ currentNode: node.type, config, idFactory })
|
|
132
158
|
};
|
|
133
159
|
}
|
|
134
160
|
if (!exampleDirective.arguments) {
|
|
135
161
|
throw new Error("@example directive must have arguments");
|
|
136
162
|
}
|
|
137
163
|
/**
|
|
138
|
-
* @
|
|
164
|
+
* @exampleID(value: "id")
|
|
165
|
+
* -> { value: "id1" }
|
|
166
|
+
* @exampleString(value: "value")
|
|
139
167
|
* -> { value: "value" }
|
|
168
|
+
* @exampleInt(value: 1)
|
|
169
|
+
* -> { value: 1 }
|
|
170
|
+
* @exampleFloat(value: 1.1)
|
|
171
|
+
* -> { value: 1.1 }
|
|
172
|
+
* @exampleBoolean(value: true)
|
|
173
|
+
* -> { value: true }
|
|
140
174
|
*/
|
|
141
175
|
const value = exampleDirective.arguments.find(a => a.name.value === "value");
|
|
142
|
-
if (value) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
if (!value) {
|
|
177
|
+
throw new Error(`@${exampleDirective.name.value} directive must have value argument.`);
|
|
178
|
+
}
|
|
179
|
+
const rawValue = valueOf(value.value);
|
|
180
|
+
// if node type is not equal to the value type, throw an error
|
|
181
|
+
const nodeType = parseTypeNodeStructure(node.type);
|
|
182
|
+
const fieldName = node.name.value;
|
|
183
|
+
// array, object, string, number, boolean, null
|
|
184
|
+
const rawValueType = Object.prototype.toString.call(rawValue).slice(8, -1).toLowerCase();
|
|
185
|
+
if (nodeType !== rawValueType) {
|
|
186
|
+
throw new Error(`${convertedTypeName}.${fieldName}: @${exampleDirective.name.value} directive value type must be ${nodeType}. Got ${rawValueType}`);
|
|
153
187
|
}
|
|
154
|
-
|
|
188
|
+
// if ID type, add idFactory() to the value
|
|
189
|
+
// e.g. @exampleID(value: "id") -> { value: "id1" }
|
|
190
|
+
const isExampleIdDirective = exampleDirective.name.value === "exampleID";
|
|
191
|
+
const exampleValue = isExampleIdDirective && typeof rawValue === "string" ? `${idFactory(rawValue)}` : rawValue;
|
|
192
|
+
return { example: { value: exampleValue } };
|
|
155
193
|
}
|
|
156
|
-
function parseObjectTypeOrInputObjectTypeDefinition(node, config) {
|
|
194
|
+
function parseObjectTypeOrInputObjectTypeDefinition({ node, config, idFactory }) {
|
|
157
195
|
const originalTypeName = node.name.value;
|
|
158
196
|
const convertedTypeName = convertName(originalTypeName, config);
|
|
159
197
|
return {
|
|
@@ -162,13 +200,19 @@ function parseObjectTypeOrInputObjectTypeDefinition(node, config) {
|
|
|
162
200
|
fields: [
|
|
163
201
|
...(node.fields ?? []).map((field) => ({
|
|
164
202
|
name: field.name.value,
|
|
165
|
-
...parseFieldOrInputValueDefinition(
|
|
203
|
+
...parseFieldOrInputValueDefinition({
|
|
204
|
+
node: field,
|
|
205
|
+
convertedTypeName,
|
|
206
|
+
config,
|
|
207
|
+
idFactory
|
|
208
|
+
}),
|
|
166
209
|
})),
|
|
167
210
|
],
|
|
168
211
|
};
|
|
169
212
|
}
|
|
170
213
|
export function getTypeInfos(config, schema) {
|
|
171
214
|
const types = Object.values(schema.getTypeMap());
|
|
215
|
+
const idFactory = createIDFactory();
|
|
172
216
|
const userDefinedTypeDefinitions = types
|
|
173
217
|
.map((type) => type.astNode)
|
|
174
218
|
.filter((node) => {
|
|
@@ -196,7 +240,7 @@ export function getTypeInfos(config, schema) {
|
|
|
196
240
|
})
|
|
197
241
|
.map((node) => {
|
|
198
242
|
if (node?.kind === Kind.OBJECT_TYPE_DEFINITION || node?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
|
|
199
|
-
return parseObjectTypeOrInputObjectTypeDefinition(node, config);
|
|
243
|
+
return parseObjectTypeOrInputObjectTypeDefinition({ node, config, idFactory });
|
|
200
244
|
}
|
|
201
245
|
else if (node?.kind === Kind.INTERFACE_TYPE_DEFINITION) {
|
|
202
246
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-scanner.js","sourceRoot":"","sources":["../../src/schema-scanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MA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|
|
27
27
|
return convertedName;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
const createIDFactory = () => {
|
|
31
|
+
const idMap = new Map<string, number>();
|
|
32
|
+
return (name: string) => {
|
|
33
|
+
const count = idMap.get(name) ?? 0;
|
|
34
|
+
idMap.set(name, count + 1);
|
|
35
|
+
return `${name}${count}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
30
39
|
const parseTypeNodeStructure = (node: TypeNode): string => {
|
|
31
40
|
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
32
41
|
return parseTypeNodeStructure(node.type);
|
|
@@ -106,7 +115,7 @@ function valueOf(value: ConstValueNode): ValuePrimitive | ValueArray | ValueObje
|
|
|
106
115
|
throw new Error(`Unknown kind of value ${value satisfies never}`)
|
|
107
116
|
}
|
|
108
117
|
|
|
109
|
-
const typeToFunction = (type: string, config: Config): string => {
|
|
118
|
+
const typeToFunction = (type: string, config: Config, idFactory: ReturnType<typeof createIDFactory>): string => {
|
|
110
119
|
switch (type) {
|
|
111
120
|
case "String":
|
|
112
121
|
return `"${config.defaultValues.String}"`
|
|
@@ -117,81 +126,117 @@ const typeToFunction = (type: string, config: Config): string => {
|
|
|
117
126
|
case "Boolean":
|
|
118
127
|
return `${config.defaultValues.Boolean ? "true" : "false"}`
|
|
119
128
|
case "ID":
|
|
120
|
-
return `"${config.defaultValues.ID}"`
|
|
129
|
+
return `"${idFactory(config.defaultValues.ID)}"`
|
|
121
130
|
default:
|
|
122
131
|
// reference to the object
|
|
123
132
|
return `${type}`;
|
|
124
133
|
}
|
|
125
134
|
}
|
|
126
|
-
const typeToFunctionWithArray = (type: string, config: Config): string => {
|
|
127
|
-
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config)})`
|
|
135
|
+
const typeToFunctionWithArray = (type: string, config: Config, idFactory: ReturnType<typeof createIDFactory>): string => {
|
|
136
|
+
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config, idFactory)})`
|
|
128
137
|
}
|
|
129
138
|
// NamedType/ListType handling
|
|
130
|
-
const nodeToExpression = ({ currentNode, isArray = false, config }: {
|
|
139
|
+
const nodeToExpression = ({ currentNode, isArray = false, config, idFactory }: {
|
|
131
140
|
currentNode: NonNullTypeNode | NamedTypeNode | ListTypeNode,
|
|
132
141
|
config: Config
|
|
133
142
|
isArray?: boolean,
|
|
143
|
+
idFactory: ReturnType<typeof createIDFactory>
|
|
134
144
|
}): ExampleDirectionExpresion => {
|
|
135
145
|
if (currentNode.kind === "NonNullType") {
|
|
136
146
|
return nodeToExpression({
|
|
137
147
|
currentNode:
|
|
138
|
-
currentNode.type,
|
|
148
|
+
currentNode.type,
|
|
149
|
+
isArray,
|
|
150
|
+
config,
|
|
151
|
+
idFactory
|
|
139
152
|
});
|
|
140
153
|
} else if (currentNode.kind === "NamedType") {
|
|
141
154
|
if (isArray) {
|
|
142
155
|
return {
|
|
143
|
-
expression: typeToFunctionWithArray(currentNode.name.value, config)
|
|
156
|
+
expression: typeToFunctionWithArray(currentNode.name.value, config, idFactory)
|
|
144
157
|
}
|
|
145
158
|
} else {
|
|
146
159
|
return {
|
|
147
|
-
expression: typeToFunction(currentNode.name.value, config)
|
|
160
|
+
expression: typeToFunction(currentNode.name.value, config, idFactory)
|
|
148
161
|
}
|
|
149
162
|
}
|
|
150
163
|
} else if (currentNode.kind === "ListType") {
|
|
151
|
-
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config })
|
|
164
|
+
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config, idFactory })
|
|
152
165
|
}
|
|
153
166
|
throw new Error("Unknown node kind")
|
|
154
167
|
}
|
|
155
168
|
|
|
169
|
+
const SUPPORTED_EXAMPLE_DIRECTIVES = ["exampleID", "exampleString", "exampleInt", "exampleFloat", "exampleBoolean"];
|
|
170
|
+
const isIdType = (node: NonNullTypeNode | NamedTypeNode | ListTypeNode): boolean => {
|
|
171
|
+
if (node.kind === "NonNullType") {
|
|
172
|
+
return isIdType(node.type)
|
|
173
|
+
} else if (node.kind === "NamedType") {
|
|
174
|
+
return node.name.value === "ID"
|
|
175
|
+
} else if (node.kind === "ListType") {
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
return false
|
|
179
|
+
}
|
|
180
|
+
|
|
156
181
|
function parseFieldOrInputValueDefinition(
|
|
157
|
-
node
|
|
158
|
-
|
|
159
|
-
|
|
182
|
+
{ node, convertedTypeName, config, idFactory }: {
|
|
183
|
+
node: FieldDefinitionNode | InputValueDefinitionNode,
|
|
184
|
+
convertedTypeName: string,
|
|
185
|
+
config: Config;
|
|
186
|
+
idFactory: ReturnType<typeof createIDFactory>
|
|
187
|
+
},
|
|
160
188
|
): { example?: ExampleDirective | undefined } {
|
|
161
|
-
const exampleDirective = node.directives?.find(d =>
|
|
189
|
+
const exampleDirective = node.directives?.find(d => {
|
|
190
|
+
return SUPPORTED_EXAMPLE_DIRECTIVES.includes(d.name.value)
|
|
191
|
+
});
|
|
162
192
|
// fake
|
|
163
193
|
// if @example directive is not found, return random value for the scalar type
|
|
164
194
|
if (!exampleDirective) {
|
|
165
195
|
return {
|
|
166
|
-
example: nodeToExpression({ currentNode: node.type, config })
|
|
196
|
+
example: nodeToExpression({ currentNode: node.type, config, idFactory })
|
|
167
197
|
};
|
|
168
198
|
}
|
|
169
199
|
if (!exampleDirective.arguments) {
|
|
170
200
|
throw new Error("@example directive must have arguments")
|
|
171
201
|
}
|
|
172
202
|
/**
|
|
173
|
-
* @
|
|
203
|
+
* @exampleID(value: "id")
|
|
204
|
+
* -> { value: "id1" }
|
|
205
|
+
* @exampleString(value: "value")
|
|
174
206
|
* -> { value: "value" }
|
|
207
|
+
* @exampleInt(value: 1)
|
|
208
|
+
* -> { value: 1 }
|
|
209
|
+
* @exampleFloat(value: 1.1)
|
|
210
|
+
* -> { value: 1.1 }
|
|
211
|
+
* @exampleBoolean(value: true)
|
|
212
|
+
* -> { value: true }
|
|
175
213
|
*/
|
|
176
214
|
const value = exampleDirective.arguments.find(a => a.name.value === "value");
|
|
177
|
-
if (value) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
215
|
+
if (!value) {
|
|
216
|
+
throw new Error(`@${exampleDirective.name.value} directive must have value argument.`)
|
|
217
|
+
}
|
|
218
|
+
const rawValue = valueOf(value.value);
|
|
219
|
+
// if node type is not equal to the value type, throw an error
|
|
220
|
+
const nodeType = parseTypeNodeStructure(node.type);
|
|
221
|
+
const fieldName = node.name.value;
|
|
222
|
+
// array, object, string, number, boolean, null
|
|
223
|
+
const rawValueType = Object.prototype.toString.call(rawValue).slice(8, -1).toLowerCase();
|
|
224
|
+
if (nodeType !== rawValueType) {
|
|
225
|
+
throw new Error(`${convertedTypeName}.${fieldName}: @${exampleDirective.name.value} directive value type must be ${nodeType}. Got ${rawValueType}`)
|
|
188
226
|
}
|
|
189
|
-
|
|
227
|
+
// if ID type, add idFactory() to the value
|
|
228
|
+
// e.g. @exampleID(value: "id") -> { value: "id1" }
|
|
229
|
+
const isExampleIdDirective = exampleDirective.name.value === "exampleID";
|
|
230
|
+
const exampleValue = isExampleIdDirective && typeof rawValue === "string" ? `${idFactory(rawValue)}` : rawValue;
|
|
231
|
+
return { example: { value: exampleValue } }
|
|
190
232
|
}
|
|
191
233
|
|
|
192
234
|
function parseObjectTypeOrInputObjectTypeDefinition(
|
|
193
|
-
node
|
|
194
|
-
|
|
235
|
+
{ node, config, idFactory }: {
|
|
236
|
+
node: ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode,
|
|
237
|
+
config: Config;
|
|
238
|
+
idFactory: ReturnType<typeof createIDFactory>
|
|
239
|
+
},
|
|
195
240
|
): ObjectTypeInfo {
|
|
196
241
|
const originalTypeName = node.name.value;
|
|
197
242
|
const convertedTypeName = convertName(originalTypeName, config);
|
|
@@ -201,7 +246,12 @@ function parseObjectTypeOrInputObjectTypeDefinition(
|
|
|
201
246
|
fields: [
|
|
202
247
|
...(node.fields ?? []).map((field) => ({
|
|
203
248
|
name: field.name.value,
|
|
204
|
-
...parseFieldOrInputValueDefinition(
|
|
249
|
+
...parseFieldOrInputValueDefinition({
|
|
250
|
+
node: field,
|
|
251
|
+
convertedTypeName,
|
|
252
|
+
config,
|
|
253
|
+
idFactory
|
|
254
|
+
}),
|
|
205
255
|
})),
|
|
206
256
|
],
|
|
207
257
|
};
|
|
@@ -228,6 +278,7 @@ export type TypeInfo = ObjectTypeInfo | AbstractTypeInfo;
|
|
|
228
278
|
export function getTypeInfos(config: Config, schema: GraphQLSchema): TypeInfo[] {
|
|
229
279
|
const types = Object.values(schema.getTypeMap());
|
|
230
280
|
|
|
281
|
+
const idFactory = createIDFactory();
|
|
231
282
|
const userDefinedTypeDefinitions = types
|
|
232
283
|
.map((type) => type.astNode)
|
|
233
284
|
.filter(
|
|
@@ -273,7 +324,7 @@ export function getTypeInfos(config: Config, schema: GraphQLSchema): TypeInfo[]
|
|
|
273
324
|
)
|
|
274
325
|
.map((node) => {
|
|
275
326
|
if (node?.kind === Kind.OBJECT_TYPE_DEFINITION || node?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
|
|
276
|
-
return parseObjectTypeOrInputObjectTypeDefinition(node, config);
|
|
327
|
+
return parseObjectTypeOrInputObjectTypeDefinition({ node, config, idFactory });
|
|
277
328
|
} else if (node?.kind === Kind.INTERFACE_TYPE_DEFINITION) {
|
|
278
329
|
return {
|
|
279
330
|
type: 'abstract',
|