@kubun/graphql 0.2.2 → 0.2.4
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/lib/schema.d.ts +11 -4
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +141 -75
- package/package.json +2 -2
package/lib/schema.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { AttachmentID } from '@kubun/id';
|
|
1
|
+
import { AttachmentID, DocumentModelID } from '@kubun/id';
|
|
2
2
|
import { type ArrayModel, type DocumentData, type DocumentModel, type DocumentModelSchema, type DocumentModelsRecord, type ObjectModel, type ReferencesRecordModel, type ScalarModel, type StringCustomModel, type StringEnumModel, type StringFormatModel, type StringModel, type TypeModel } from '@kubun/protocol';
|
|
3
3
|
import { GraphQLEnumType, type GraphQLFieldConfig, type GraphQLFieldConfigMap, GraphQLInputObjectType, type GraphQLInputType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, type GraphQLOutputType, type GraphQLScalarType, GraphQLSchema } from 'graphql';
|
|
4
4
|
import { type Connection, type ConnectionArguments } from 'graphql-relay';
|
|
5
|
+
export declare function toDocumentModelID(id: DocumentModelID | string): DocumentModelID;
|
|
6
|
+
export declare function getGlobalID(id: DocumentModelID | string, globalID: DocumentModelID | string): DocumentModelID;
|
|
5
7
|
export declare function toList(type: GraphQLInputType): GraphQLList<GraphQLNonNull<GraphQLInputType>>;
|
|
8
|
+
export declare function toOuputList(type: GraphQLOutputType): GraphQLList<GraphQLNonNull<GraphQLOutputType>>;
|
|
6
9
|
export declare const PatchOperation: GraphQLInputObjectType;
|
|
7
10
|
export type DocumentType = {
|
|
8
11
|
id: string;
|
|
@@ -30,13 +33,16 @@ export type SchemaBuilderParams = {
|
|
|
30
33
|
export declare class SchemaBuilder<Document extends DocumentType = DocumentType, Context extends ContextType = ContextType, Definitions extends SharedDefinitions<Document, Context> = SharedDefinitions<Document, Context>> {
|
|
31
34
|
_aliases: Record<string, string>;
|
|
32
35
|
_definitions: Definitions | undefined;
|
|
36
|
+
_implemented: Record<string, Array<string>>;
|
|
37
|
+
_inputObjects: Record<string, GraphQLInputObjectType>;
|
|
38
|
+
_mutations: Record<string, GraphQLFieldConfig<unknown, Context>>;
|
|
39
|
+
_nodeModelIDs: Array<string>;
|
|
33
40
|
_record: DocumentModelsRecord;
|
|
34
41
|
_references: ReferencesRecordModel;
|
|
35
42
|
_relationsTo: Record<string, Record<string, string>>;
|
|
36
43
|
_types: Record<string, GraphQLOutputType>;
|
|
37
|
-
_inputObjects: Record<string, GraphQLInputObjectType>;
|
|
38
|
-
_mutations: Record<string, GraphQLFieldConfig<unknown, Context>>;
|
|
39
44
|
constructor(params: SchemaBuilderParams);
|
|
45
|
+
getModelIDs(id: string | null): Array<string>;
|
|
40
46
|
getViewer(ctx: Context): string | null;
|
|
41
47
|
loadDocument(id: string, ctx: Context): Promise<Document | null>;
|
|
42
48
|
resolveConnection(model: string | null, args: ConnectionArguments, ctx: Context): Promise<Connection<Document>>;
|
|
@@ -45,9 +51,10 @@ export declare class SchemaBuilder<Document extends DocumentType = DocumentType,
|
|
|
45
51
|
build(): GraphQLSchema;
|
|
46
52
|
_getDefinitions(): Definitions;
|
|
47
53
|
_getReference(path: string, parentName?: string): GraphQLOutputType;
|
|
54
|
+
_getReferenceSchema(id: string): TypeModel;
|
|
48
55
|
_getReferenceInput(path: string, isPartial: boolean, parentName?: string): GraphQLInputType;
|
|
49
56
|
_buildSharedDefinitions(): Definitions;
|
|
50
|
-
_buildDocument(id: string): GraphQLObjectType<Document, Context
|
|
57
|
+
_buildDocument(id: string): GraphQLObjectType<Document, Context> | GraphQLInterfaceType;
|
|
51
58
|
_buildDocumentInput(id: string, model: DocumentModel): void;
|
|
52
59
|
_buildReferenceFilterInput(path: string, parentName?: string): GraphQLInputType;
|
|
53
60
|
_buildReferenceOrderByInput(path: string, parentName?: string): GraphQLInputType | null;
|
package/lib/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAc,eAAe,EAAY,MAAM,WAAW,CAAA;AAC/E,OAAO,EACL,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAEhB,KAAK,qBAAqB,EAC1B,KAAK,WAAW,EAEhB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAEhB,KAAK,SAAS,EACf,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAEL,eAAe,EAEf,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAI1B,sBAAsB,EACtB,KAAK,gBAAgB,EAErB,oBAAoB,EACpB,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,aAAa,EAKd,MAAM,SAAS,CAAA;AAChB,OAAO,EACL,KAAK,UAAU,EACf,KAAK,mBAAmB,EAIzB,MAAM,eAAe,CAAA;AAwDtB,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,eAAe,GAAG,MAAM,GAAG,eAAe,CAE/E;AAED,wBAAgB,WAAW,CACzB,EAAE,EAAE,eAAe,GAAG,MAAM,EAC5B,QAAQ,EAAE,eAAe,GAAG,MAAM,GACjC,eAAe,CAGjB;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,gBAAgB,iDAE5C;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,iBAAiB,kDAElD;AAyFD,eAAO,MAAM,cAAc,wBAUzB,CAAA;AAUF,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,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AAM,EAAE,UAAU,EAAE,UAAU,SAAK,GAAG,WAAW,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;IAOhG,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,SAAK,GAAG,iBAAiB;IAKjF,kBAAkB,CAChB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,mBAAmB,GAAG,WAAW,EACzC,KAAK,CAAC,EAAE,aAAa,GACpB,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IA2FxD,YAAY,CACV,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,OAAO,GACf,eAAe,GAAG,iBAAiB,GAAG,iBAAiB;IAa1D,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,OAAO;IAqB9D,gBAAgB,CAAC,MAAM,EAAE,eAAe;IAQxC,kBAAkB,CAAC,MAAM,EAAE,iBAAiB;CAO7C"}
|
package/lib/schema.js
CHANGED
|
@@ -19,13 +19,33 @@ const STRING_TITLES = {
|
|
|
19
19
|
docid: GraphQLDocID,
|
|
20
20
|
jsonobject: GraphQLJSONObject
|
|
21
21
|
};
|
|
22
|
+
function getReferenceID(path) {
|
|
23
|
+
return path.slice(REFERENCE_PREFIX.length);
|
|
24
|
+
}
|
|
22
25
|
function isList(type) {
|
|
23
26
|
return isListType(type) || isNonNullType(type) && isListType(type.ofType);
|
|
24
27
|
}
|
|
28
|
+
function collectAllModelIDs(collected, record, interfaceID) {
|
|
29
|
+
const { isInterface, implementedBy } = record[interfaceID];
|
|
30
|
+
for (const id of implementedBy){
|
|
31
|
+
if (isInterface) {
|
|
32
|
+
collectAllModelIDs(collected, record, id);
|
|
33
|
+
} else {
|
|
34
|
+
collected.add(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function toDocumentModelID(id) {
|
|
39
|
+
return id instanceof DocumentModelID ? id : DocumentModelID.fromString(id);
|
|
40
|
+
}
|
|
41
|
+
export function getGlobalID(id, globalID) {
|
|
42
|
+
const docModelID = toDocumentModelID(id);
|
|
43
|
+
return docModelID.isLocal ? docModelID.toGlobal(toDocumentModelID(globalID)) : docModelID;
|
|
44
|
+
}
|
|
25
45
|
export function toList(type) {
|
|
26
46
|
return new GraphQLList(new GraphQLNonNull(type));
|
|
27
47
|
}
|
|
28
|
-
function toOuputList(type) {
|
|
48
|
+
export function toOuputList(type) {
|
|
29
49
|
return new GraphQLList(new GraphQLNonNull(type));
|
|
30
50
|
}
|
|
31
51
|
function createEnumValueFilterInput(type) {
|
|
@@ -194,34 +214,60 @@ export class SchemaBuilder {
|
|
|
194
214
|
// Mapping of model or references IDs to their GraphQL object names
|
|
195
215
|
_aliases;
|
|
196
216
|
_definitions;
|
|
217
|
+
_implemented = {};
|
|
218
|
+
_inputObjects = {
|
|
219
|
+
...VALUE_FILTER_INPUTS,
|
|
220
|
+
PatchOperation
|
|
221
|
+
};
|
|
222
|
+
_mutations = {};
|
|
223
|
+
_nodeModelIDs;
|
|
197
224
|
_record;
|
|
198
225
|
_references = {};
|
|
199
226
|
_relationsTo = {};
|
|
200
227
|
_types = {
|
|
201
228
|
OrderByDirection
|
|
202
229
|
};
|
|
203
|
-
_inputObjects = {
|
|
204
|
-
...VALUE_FILTER_INPUTS,
|
|
205
|
-
PatchOperation
|
|
206
|
-
};
|
|
207
|
-
_mutations = {};
|
|
208
230
|
constructor(params){
|
|
209
231
|
this._aliases = params.aliases ?? {};
|
|
210
232
|
this._record = params.record;
|
|
211
|
-
|
|
212
|
-
|
|
233
|
+
const referencedInterfaces = {};
|
|
234
|
+
for (const [modelIDstring, model] of Object.entries(params.record)){
|
|
235
|
+
const modelID = DocumentModelID.fromString(modelIDstring);
|
|
213
236
|
Object.assign(this._references, model.schema.$defs ?? {});
|
|
237
|
+
// Extract interfaces
|
|
238
|
+
for (const iid of model.interfaces){
|
|
239
|
+
const interfaceID = getGlobalID(iid, modelID).toString();
|
|
240
|
+
referencedInterfaces[interfaceID] ??= {
|
|
241
|
+
isInterface: model.behavior === 'interface',
|
|
242
|
+
implementedBy: new Set()
|
|
243
|
+
};
|
|
244
|
+
referencedInterfaces[interfaceID].implementedBy.add(modelIDstring);
|
|
245
|
+
}
|
|
214
246
|
// Extract inverse relations
|
|
215
247
|
for (const [fieldName, meta] of Object.entries(model.fieldsMeta)){
|
|
216
248
|
if (meta.relationModel != null) {
|
|
217
|
-
const
|
|
218
|
-
const globalID = relationModelID.isLocal ? relationModelID.toGlobal(modelID) : relationModelID;
|
|
219
|
-
const relationID = globalID.toString();
|
|
249
|
+
const relationID = getGlobalID(meta.relationModel, modelID).toString();
|
|
220
250
|
this._relationsTo[relationID] ??= {};
|
|
221
|
-
this._relationsTo[relationID][fieldName] =
|
|
251
|
+
this._relationsTo[relationID][fieldName] = modelIDstring;
|
|
222
252
|
}
|
|
223
253
|
}
|
|
224
254
|
}
|
|
255
|
+
// Map interface IDs to all the models implementing them
|
|
256
|
+
const nodeModelIDs = new Set();
|
|
257
|
+
for (const id of Object.keys(referencedInterfaces)){
|
|
258
|
+
const implementedBy = new Set();
|
|
259
|
+
collectAllModelIDs(implementedBy, referencedInterfaces, id);
|
|
260
|
+
this._implemented[id] = Array.from(implementedBy);
|
|
261
|
+
for (const modelID of implementedBy){
|
|
262
|
+
nodeModelIDs.add(modelID);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
this._nodeModelIDs = Array.from(nodeModelIDs);
|
|
266
|
+
}
|
|
267
|
+
getModelIDs(id) {
|
|
268
|
+
return id === null ? this._nodeModelIDs : this._implemented[id] ?? [
|
|
269
|
+
id
|
|
270
|
+
];
|
|
225
271
|
}
|
|
226
272
|
getViewer(ctx) {
|
|
227
273
|
throw new Error('Must be implemented');
|
|
@@ -284,23 +330,24 @@ export class SchemaBuilder {
|
|
|
284
330
|
return this._definitions;
|
|
285
331
|
}
|
|
286
332
|
_getReference(path, parentName) {
|
|
287
|
-
const id = path
|
|
333
|
+
const id = getReferenceID(path);
|
|
288
334
|
const type = this._types[id];
|
|
289
335
|
if (type != null) {
|
|
290
336
|
return type;
|
|
291
337
|
}
|
|
292
|
-
const schema = this.
|
|
293
|
-
if (schema == null) {
|
|
294
|
-
throw new Error(`Could not find reference: ${id}`);
|
|
295
|
-
}
|
|
338
|
+
const schema = this._getReferenceSchema(id);
|
|
296
339
|
return this._buildReference(id, schema, parentName);
|
|
297
340
|
}
|
|
298
|
-
|
|
299
|
-
const id = path.slice(REFERENCE_PREFIX.length);
|
|
341
|
+
_getReferenceSchema(id) {
|
|
300
342
|
const schema = this._references[id];
|
|
301
343
|
if (schema == null) {
|
|
302
344
|
throw new Error(`Could not find reference: ${id}`);
|
|
303
345
|
}
|
|
346
|
+
return schema;
|
|
347
|
+
}
|
|
348
|
+
_getReferenceInput(path, isPartial, parentName = '') {
|
|
349
|
+
const id = getReferenceID(path);
|
|
350
|
+
const schema = this._getReferenceSchema(id);
|
|
304
351
|
if (schema.type === 'array') {
|
|
305
352
|
const itemType = this._getReferenceInput(schema.items.$ref, false, schema.title);
|
|
306
353
|
return new GraphQLList(new GraphQLNonNull(itemType));
|
|
@@ -484,16 +531,21 @@ export class SchemaBuilder {
|
|
|
484
531
|
}
|
|
485
532
|
const definitions = this._getDefinitions();
|
|
486
533
|
this._aliases[id] ??= pascalCase(model.name);
|
|
487
|
-
const
|
|
534
|
+
const config = {
|
|
488
535
|
name: this._aliases[id],
|
|
489
|
-
interfaces:
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
536
|
+
interfaces: ()=>{
|
|
537
|
+
return [
|
|
538
|
+
definitions.documentInterface,
|
|
539
|
+
definitions.nodeInterface
|
|
540
|
+
].concat(model.interfaces.map((interfaceID)=>{
|
|
541
|
+
return this._types[getGlobalID(interfaceID, id).toString()];
|
|
542
|
+
}));
|
|
543
|
+
},
|
|
544
|
+
fields: ()=>this._buildObjectFields(id, model.schema, model),
|
|
545
|
+
isTypeOf: (doc)=>DocumentID.fromString(doc.id).model.toString() === id
|
|
546
|
+
};
|
|
547
|
+
const isInterface = model.behavior === 'interface';
|
|
548
|
+
const nodeType = isInterface ? new GraphQLInterfaceType(config) : new GraphQLObjectType(config);
|
|
497
549
|
const { connectionType, edgeType } = connectionDefinitions({
|
|
498
550
|
nodeType
|
|
499
551
|
});
|
|
@@ -513,13 +565,18 @@ export class SchemaBuilder {
|
|
|
513
565
|
const setFields = {};
|
|
514
566
|
const updateFields = {};
|
|
515
567
|
for (const [key, value] of Object.entries(model.schema.properties)){
|
|
516
|
-
const
|
|
568
|
+
const path = value.$ref;
|
|
569
|
+
const setType = this._getReferenceInput(path, false);
|
|
517
570
|
setFields[key] = {
|
|
518
571
|
type: model.schema.required.includes(key) ? new GraphQLNonNull(setType) : setType
|
|
519
572
|
};
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
573
|
+
const id = getReferenceID(path);
|
|
574
|
+
const schema = this._getReferenceSchema(id);
|
|
575
|
+
if (schema.const == null) {
|
|
576
|
+
updateFields[key] = {
|
|
577
|
+
type: this._getReferenceInput(path, true)
|
|
578
|
+
};
|
|
579
|
+
}
|
|
523
580
|
}
|
|
524
581
|
const name = this._aliases[id];
|
|
525
582
|
this._inputObjects[id] = new GraphQLInputObjectType({
|
|
@@ -532,11 +589,8 @@ export class SchemaBuilder {
|
|
|
532
589
|
});
|
|
533
590
|
}
|
|
534
591
|
_buildReferenceFilterInput(path, parentName = '') {
|
|
535
|
-
const id = path
|
|
536
|
-
const schema = this.
|
|
537
|
-
if (schema == null) {
|
|
538
|
-
throw new Error(`Could not find reference: ${id}`);
|
|
539
|
-
}
|
|
592
|
+
const id = getReferenceID(path);
|
|
593
|
+
const schema = this._getReferenceSchema(id);
|
|
540
594
|
if (schema.type === 'object') {
|
|
541
595
|
return this._buildObjectFilterInput(id, schema, parentName);
|
|
542
596
|
}
|
|
@@ -571,11 +625,8 @@ export class SchemaBuilder {
|
|
|
571
625
|
return this._inputObjects[filter.name];
|
|
572
626
|
}
|
|
573
627
|
_buildReferenceOrderByInput(path, parentName = '') {
|
|
574
|
-
const id = path
|
|
575
|
-
const schema = this.
|
|
576
|
-
if (schema == null) {
|
|
577
|
-
throw new Error(`Could not find reference: ${id}`);
|
|
578
|
-
}
|
|
628
|
+
const id = getReferenceID(path);
|
|
629
|
+
const schema = this._getReferenceSchema(id);
|
|
579
630
|
switch(schema.type){
|
|
580
631
|
case 'array':
|
|
581
632
|
return null;
|
|
@@ -752,40 +803,43 @@ export class SchemaBuilder {
|
|
|
752
803
|
fields[key] = {
|
|
753
804
|
type: required.includes(key) ? new GraphQLNonNull(type) : type
|
|
754
805
|
};
|
|
755
|
-
if (model
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
}
|
|
786
|
-
}
|
|
806
|
+
if (model == null) {
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
fields[key].resolve = (doc)=>doc.data?.[key];
|
|
810
|
+
const relationModel = model.fieldsMeta[key]?.relationModel;
|
|
811
|
+
if (typeof relationModel !== 'undefined') {
|
|
812
|
+
let relationModelID;
|
|
813
|
+
let relationType;
|
|
814
|
+
if (relationModel === null) {
|
|
815
|
+
relationModelID = null;
|
|
816
|
+
relationType = definitions.documentInterface;
|
|
817
|
+
} else {
|
|
818
|
+
relationModelID = getGlobalID(relationModel, id).toString();
|
|
819
|
+
relationType = this._buildDocument(relationModelID);
|
|
820
|
+
}
|
|
821
|
+
if (isList(type)) {
|
|
822
|
+
// TODO: add suport for filters?
|
|
823
|
+
fields[`_${key}_documents`] = {
|
|
824
|
+
type: new GraphQLNonNull(toOuputList(relationType)),
|
|
825
|
+
resolve: async (doc, _args, ctx)=>{
|
|
826
|
+
const relationIDs = doc.data?.[key] ?? [];
|
|
827
|
+
return relationIDs.length ? await this.resolveList(relationModelID, relationIDs, ctx) : [];
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
} else {
|
|
831
|
+
fields[`_${key}_document`] = {
|
|
832
|
+
type: relationType,
|
|
833
|
+
resolve: async (doc, _args, ctx)=>{
|
|
834
|
+
const relationID = doc.data?.[key];
|
|
835
|
+
return relationID ? await this.loadDocument(relationID, ctx) : null;
|
|
836
|
+
}
|
|
837
|
+
};
|
|
787
838
|
}
|
|
788
|
-
|
|
839
|
+
}
|
|
840
|
+
for (const iid of model.interfaces ?? []){
|
|
841
|
+
const interfaceID = getGlobalID(iid, id).toString();
|
|
842
|
+
for (const [fieldName, relationID] of Object.entries(this._relationsTo[interfaceID] ?? {})){
|
|
789
843
|
const relationName = this._aliases[relationID];
|
|
790
844
|
fields[`_${fieldName}_of_${relationName}_list`] = {
|
|
791
845
|
type: new GraphQLNonNull(this._types[`${relationID}-connection`])
|
|
@@ -795,10 +849,22 @@ export class SchemaBuilder {
|
|
|
795
849
|
};
|
|
796
850
|
}
|
|
797
851
|
}
|
|
852
|
+
for (const [fieldName, relationID] of Object.entries(this._relationsTo[id] ?? {})){
|
|
853
|
+
const relationName = this._aliases[relationID];
|
|
854
|
+
fields[`_${fieldName}_of_${relationName}_list`] = {
|
|
855
|
+
type: new GraphQLNonNull(this._types[`${relationID}-connection`])
|
|
856
|
+
};
|
|
857
|
+
fields[`_${fieldName}_of_${relationName}_count`] = {
|
|
858
|
+
type: new GraphQLNonNull(GraphQLInt)
|
|
859
|
+
};
|
|
860
|
+
}
|
|
798
861
|
}
|
|
799
862
|
return fields;
|
|
800
863
|
}
|
|
801
864
|
_buildString(schema, asInput) {
|
|
865
|
+
if (schema.const != null) {
|
|
866
|
+
return GraphQLString;
|
|
867
|
+
}
|
|
802
868
|
if (schema.enum != null) {
|
|
803
869
|
return this._buildEnumString(schema);
|
|
804
870
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/graphql",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"graphql-scalars": "^1.24.2",
|
|
23
23
|
"multiformats": "^13.3.2",
|
|
24
24
|
"@kubun/id": "^0.2.0",
|
|
25
|
-
"@kubun/protocol": "^0.2.
|
|
25
|
+
"@kubun/protocol": "^0.2.4"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"json-schema-typed": "^8.0.1"
|