@gqloom/json 0.12.0 → 0.15.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/dist/index.cjs +43 -67
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +30 -29
- package/package.json +4 -5
package/dist/index.cjs
CHANGED
|
@@ -1,30 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
let __gqloom_core = require("@gqloom/core");
|
|
25
|
-
__gqloom_core = __toESM(__gqloom_core);
|
|
1
|
+
let _gqloom_core = require("@gqloom/core");
|
|
26
2
|
let graphql = require("graphql");
|
|
27
|
-
graphql = __toESM(graphql);
|
|
28
3
|
|
|
29
4
|
//#region src/index.ts
|
|
30
5
|
var JSONWeaver = class JSONWeaver {
|
|
@@ -37,7 +12,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
37
12
|
static config = function(config) {
|
|
38
13
|
return {
|
|
39
14
|
...config,
|
|
40
|
-
[
|
|
15
|
+
[_gqloom_core.SYMBOLS.WEAVER_CONFIG]: "gqloom.json"
|
|
41
16
|
};
|
|
42
17
|
};
|
|
43
18
|
/**
|
|
@@ -46,7 +21,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
46
21
|
* @returns GraphQL Silk Like JSON Schema
|
|
47
22
|
*/
|
|
48
23
|
static unravel(schema) {
|
|
49
|
-
const config =
|
|
24
|
+
const config = _gqloom_core.weaverContext.value?.getConfig("gqloom.json");
|
|
50
25
|
if (typeof schema === "object" && !("~standard" in schema)) Object.defineProperty(schema, "~standard", {
|
|
51
26
|
value: {
|
|
52
27
|
version: 1,
|
|
@@ -55,17 +30,24 @@ var JSONWeaver = class JSONWeaver {
|
|
|
55
30
|
},
|
|
56
31
|
enumerable: false
|
|
57
32
|
});
|
|
58
|
-
if (typeof schema === "object" && !(
|
|
33
|
+
if (typeof schema === "object" && !(_gqloom_core.SYMBOLS.GET_GRAPHQL_TYPE in schema)) Object.defineProperty(schema, _gqloom_core.SYMBOLS.GET_GRAPHQL_TYPE, {
|
|
59
34
|
value: config ? function() {
|
|
60
|
-
return
|
|
35
|
+
return _gqloom_core.weaverContext.useConfig(config, () => JSONWeaver.getGraphQLTypeBySelf.call(this));
|
|
61
36
|
} : JSONWeaver.getGraphQLTypeBySelf,
|
|
62
37
|
enumerable: false
|
|
63
38
|
});
|
|
64
39
|
return schema;
|
|
65
40
|
}
|
|
66
|
-
static sourceTypeMap = /* @__PURE__ */ new
|
|
41
|
+
static sourceTypeMap = /* @__PURE__ */ new WeakMap();
|
|
67
42
|
static getGraphQLType(schema, { source } = {}) {
|
|
68
|
-
if (
|
|
43
|
+
if ((typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema) {
|
|
44
|
+
const standard = schema["~standard"];
|
|
45
|
+
if ("jsonSchema" in standard) {
|
|
46
|
+
const rawSchema = standard.jsonSchema.output({ target: "draft-07" });
|
|
47
|
+
return JSONWeaver.getGraphQLType(rawSchema, { source: source ?? schema });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (source && typeof schema === "object" && schema !== null) JSONWeaver.sourceTypeMap.set(schema, source);
|
|
69
51
|
return JSONWeaver.toNullableGraphQLType(schema);
|
|
70
52
|
}
|
|
71
53
|
static getGraphQLTypeBySelf() {
|
|
@@ -81,15 +63,15 @@ var JSONWeaver = class JSONWeaver {
|
|
|
81
63
|
static toMemoriedGraphQLType(schema) {
|
|
82
64
|
if (typeof schema === "boolean") throw new Error("Boolean JSON schemas are not supported");
|
|
83
65
|
const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? JSONWeaver.getTypeName(schema);
|
|
84
|
-
const existing =
|
|
66
|
+
const existing = _gqloom_core.weaverContext.getGraphQLType(schema) ?? (name ? _gqloom_core.weaverContext.getNamedType(name) : void 0);
|
|
85
67
|
if (existing) return existing;
|
|
86
68
|
const gqlType = JSONWeaver.toGraphQLTypeInner(schema);
|
|
87
|
-
if (name)
|
|
88
|
-
return
|
|
69
|
+
if (name) _gqloom_core.weaverContext.memoNamedType(gqlType);
|
|
70
|
+
return _gqloom_core.weaverContext.memoGraphQLType(schema, gqlType);
|
|
89
71
|
}
|
|
90
72
|
static toGraphQLTypeInner(schema) {
|
|
91
73
|
if (typeof schema === "boolean") throw new Error("Boolean JSON schemas are not supported");
|
|
92
|
-
const presetType = (
|
|
74
|
+
const presetType = (_gqloom_core.weaverContext.value?.getConfig("gqloom.json"))?.presetGraphQLType?.(schema);
|
|
93
75
|
if (presetType) return presetType;
|
|
94
76
|
if (schema.allOf) {
|
|
95
77
|
const interfaceSchemas = schema.allOf.filter((s) => {
|
|
@@ -115,8 +97,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
115
97
|
const objectType = JSONWeaver.toGraphQLTypeInner(mergedSchema);
|
|
116
98
|
if (interfaceSchemas.length > 0) {
|
|
117
99
|
const interfaceTypes = interfaceSchemas.map((interfaceSchema) => {
|
|
118
|
-
|
|
119
|
-
return (0, __gqloom_core.ensureInterfaceType)(interfaceObjectType);
|
|
100
|
+
return (0, _gqloom_core.ensureInterfaceType)(JSONWeaver.toMemoriedGraphQLType(interfaceSchema));
|
|
120
101
|
});
|
|
121
102
|
return new graphql.GraphQLObjectType({
|
|
122
103
|
...objectType.toConfig(),
|
|
@@ -140,7 +121,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
140
121
|
};
|
|
141
122
|
return JSONWeaver.toGraphQLTypeInner(unwrappedSchema);
|
|
142
123
|
}
|
|
143
|
-
const name = schema.title ?? JSONWeaver.getCollectedName(schema) ??
|
|
124
|
+
const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? _gqloom_core.AUTO_ALIASING;
|
|
144
125
|
return new graphql.GraphQLUnionType({
|
|
145
126
|
name,
|
|
146
127
|
description: schema.description,
|
|
@@ -153,7 +134,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
153
134
|
}
|
|
154
135
|
const type = Array.isArray(schema.type) ? schema.type.find((t) => t !== "null") : schema.type;
|
|
155
136
|
if (schema.enum) {
|
|
156
|
-
const name = schema.title ?? JSONWeaver.getCollectedName(schema) ??
|
|
137
|
+
const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? _gqloom_core.AUTO_ALIASING;
|
|
157
138
|
const values = {};
|
|
158
139
|
for (const value of schema.enum) if (typeof value === "string" || typeof value === "number") {
|
|
159
140
|
const key = String(value).replace(/[^_a-zA-Z0-9]/g, "_");
|
|
@@ -170,32 +151,27 @@ var JSONWeaver = class JSONWeaver {
|
|
|
170
151
|
case "number": return graphql.GraphQLFloat;
|
|
171
152
|
case "integer": return graphql.GraphQLInt;
|
|
172
153
|
case "boolean": return graphql.GraphQLBoolean;
|
|
173
|
-
case "array":
|
|
154
|
+
case "array":
|
|
174
155
|
if (!schema.items || typeof schema.items !== "object" || Array.isArray(schema.items)) throw new Error("Array schema must have a single object in 'items'");
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
defaultValue: fieldSchema.default
|
|
195
|
-
};
|
|
196
|
-
})
|
|
197
|
-
});
|
|
198
|
-
}
|
|
156
|
+
return new graphql.GraphQLList(JSONWeaver.toNullableGraphQLType(schema.items));
|
|
157
|
+
case "object": return new graphql.GraphQLObjectType({
|
|
158
|
+
name: schema.title ?? JSONWeaver.getCollectedName(schema) ?? JSONWeaver.getTypeName(schema) ?? _gqloom_core.AUTO_ALIASING,
|
|
159
|
+
description: schema.description,
|
|
160
|
+
fields: () => (0, _gqloom_core.mapValue)(schema.properties ?? {}, (propSchema, key) => {
|
|
161
|
+
if (key.startsWith("__")) return _gqloom_core.mapValue.SKIP;
|
|
162
|
+
const fieldSchema = propSchema;
|
|
163
|
+
if (typeof fieldSchema === "boolean") throw new Error("Boolean JSON schemas are not supported in properties");
|
|
164
|
+
let fieldType = JSONWeaver.toMemoriedGraphQLType(fieldSchema);
|
|
165
|
+
if (schema.required?.includes(key)) {
|
|
166
|
+
if (!(0, graphql.isNonNullType)(fieldType)) fieldType = new graphql.GraphQLNonNull(fieldType);
|
|
167
|
+
} else if ((0, graphql.isNonNullType)(fieldType)) fieldType = fieldType.ofType;
|
|
168
|
+
return {
|
|
169
|
+
type: fieldType,
|
|
170
|
+
description: fieldSchema.description,
|
|
171
|
+
defaultValue: fieldSchema.default
|
|
172
|
+
};
|
|
173
|
+
})
|
|
174
|
+
});
|
|
199
175
|
case "null": throw new Error("Standalone 'null' type is not supported in GraphQL. It can only be used in a union type, like ['string', 'null']");
|
|
200
176
|
}
|
|
201
177
|
throw new Error(`Unsupported JSON schema type: ${String(type)}`);
|
|
@@ -209,11 +185,11 @@ var JSONWeaver = class JSONWeaver {
|
|
|
209
185
|
}
|
|
210
186
|
static getCollectedName(schema) {
|
|
211
187
|
if (typeof schema !== "object") return void 0;
|
|
212
|
-
const name =
|
|
188
|
+
const name = _gqloom_core.weaverContext.names.get(schema);
|
|
213
189
|
if (name) return name;
|
|
214
190
|
const source = JSONWeaver.sourceTypeMap.get(schema);
|
|
215
191
|
if (source == null) return void 0;
|
|
216
|
-
return
|
|
192
|
+
return _gqloom_core.weaverContext.names.get(source);
|
|
217
193
|
}
|
|
218
194
|
};
|
|
219
195
|
function jsonSilk(schema) {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GraphQLSilk, SYMBOLS, WeaverConfig } from "@gqloom/core";
|
|
1
|
+
import { GraphQLSilk, SYMBOLS, StandardJSONSchemaV1, WeaverConfig } from "@gqloom/core";
|
|
2
2
|
import { GraphQLOutputType } from "graphql";
|
|
3
3
|
import { FromSchema, JSONSchema, JSONSchema as JSONSchema$1 } from "json-schema-to-ts";
|
|
4
4
|
|
|
@@ -25,11 +25,11 @@ declare class JSONWeaver {
|
|
|
25
25
|
* @returns GraphQL Silk Like JSON Schema
|
|
26
26
|
*/
|
|
27
27
|
static unravel<const TSchema extends JSONSchema, TData = FromSchema<TSchema>>(schema: TSchema): JSONSilk<TSchema, TData>;
|
|
28
|
-
protected static sourceTypeMap:
|
|
29
|
-
static getGraphQLType(schema: JSONSchema, {
|
|
28
|
+
protected static sourceTypeMap: WeakMap<object, unknown>;
|
|
29
|
+
static getGraphQLType(schema: JSONSchema | StandardJSONSchemaV1, {
|
|
30
30
|
source
|
|
31
31
|
}?: {
|
|
32
|
-
source?:
|
|
32
|
+
source?: unknown;
|
|
33
33
|
}): GraphQLOutputType;
|
|
34
34
|
protected static getGraphQLTypeBySelf(this: JSONSchema): GraphQLOutputType;
|
|
35
35
|
protected static toNullableGraphQLType(schema: JSONSchema): GraphQLOutputType;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GraphQLSilk, SYMBOLS, WeaverConfig } from "@gqloom/core";
|
|
1
|
+
import { GraphQLSilk, SYMBOLS, StandardJSONSchemaV1, WeaverConfig } from "@gqloom/core";
|
|
2
2
|
import { GraphQLOutputType } from "graphql";
|
|
3
3
|
import { FromSchema, JSONSchema, JSONSchema as JSONSchema$1 } from "json-schema-to-ts";
|
|
4
4
|
|
|
@@ -25,11 +25,11 @@ declare class JSONWeaver {
|
|
|
25
25
|
* @returns GraphQL Silk Like JSON Schema
|
|
26
26
|
*/
|
|
27
27
|
static unravel<const TSchema extends JSONSchema, TData = FromSchema<TSchema>>(schema: TSchema): JSONSilk<TSchema, TData>;
|
|
28
|
-
protected static sourceTypeMap:
|
|
29
|
-
static getGraphQLType(schema: JSONSchema, {
|
|
28
|
+
protected static sourceTypeMap: WeakMap<object, unknown>;
|
|
29
|
+
static getGraphQLType(schema: JSONSchema | StandardJSONSchemaV1, {
|
|
30
30
|
source
|
|
31
31
|
}?: {
|
|
32
|
-
source?:
|
|
32
|
+
source?: unknown;
|
|
33
33
|
}): GraphQLOutputType;
|
|
34
34
|
protected static getGraphQLTypeBySelf(this: JSONSchema): GraphQLOutputType;
|
|
35
35
|
protected static toNullableGraphQLType(schema: JSONSchema): GraphQLOutputType;
|
package/dist/index.js
CHANGED
|
@@ -38,9 +38,16 @@ var JSONWeaver = class JSONWeaver {
|
|
|
38
38
|
});
|
|
39
39
|
return schema;
|
|
40
40
|
}
|
|
41
|
-
static sourceTypeMap = /* @__PURE__ */ new
|
|
41
|
+
static sourceTypeMap = /* @__PURE__ */ new WeakMap();
|
|
42
42
|
static getGraphQLType(schema, { source } = {}) {
|
|
43
|
-
if (
|
|
43
|
+
if ((typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema) {
|
|
44
|
+
const standard = schema["~standard"];
|
|
45
|
+
if ("jsonSchema" in standard) {
|
|
46
|
+
const rawSchema = standard.jsonSchema.output({ target: "draft-07" });
|
|
47
|
+
return JSONWeaver.getGraphQLType(rawSchema, { source: source ?? schema });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (source && typeof schema === "object" && schema !== null) JSONWeaver.sourceTypeMap.set(schema, source);
|
|
44
51
|
return JSONWeaver.toNullableGraphQLType(schema);
|
|
45
52
|
}
|
|
46
53
|
static getGraphQLTypeBySelf() {
|
|
@@ -90,8 +97,7 @@ var JSONWeaver = class JSONWeaver {
|
|
|
90
97
|
const objectType = JSONWeaver.toGraphQLTypeInner(mergedSchema);
|
|
91
98
|
if (interfaceSchemas.length > 0) {
|
|
92
99
|
const interfaceTypes = interfaceSchemas.map((interfaceSchema) => {
|
|
93
|
-
|
|
94
|
-
return ensureInterfaceType(interfaceObjectType);
|
|
100
|
+
return ensureInterfaceType(JSONWeaver.toMemoriedGraphQLType(interfaceSchema));
|
|
95
101
|
});
|
|
96
102
|
return new GraphQLObjectType({
|
|
97
103
|
...objectType.toConfig(),
|
|
@@ -145,32 +151,27 @@ var JSONWeaver = class JSONWeaver {
|
|
|
145
151
|
case "number": return GraphQLFloat;
|
|
146
152
|
case "integer": return GraphQLInt;
|
|
147
153
|
case "boolean": return GraphQLBoolean;
|
|
148
|
-
case "array":
|
|
154
|
+
case "array":
|
|
149
155
|
if (!schema.items || typeof schema.items !== "object" || Array.isArray(schema.items)) throw new Error("Array schema must have a single object in 'items'");
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
defaultValue: fieldSchema.default
|
|
170
|
-
};
|
|
171
|
-
})
|
|
172
|
-
});
|
|
173
|
-
}
|
|
156
|
+
return new GraphQLList(JSONWeaver.toNullableGraphQLType(schema.items));
|
|
157
|
+
case "object": return new GraphQLObjectType({
|
|
158
|
+
name: schema.title ?? JSONWeaver.getCollectedName(schema) ?? JSONWeaver.getTypeName(schema) ?? AUTO_ALIASING,
|
|
159
|
+
description: schema.description,
|
|
160
|
+
fields: () => mapValue(schema.properties ?? {}, (propSchema, key) => {
|
|
161
|
+
if (key.startsWith("__")) return mapValue.SKIP;
|
|
162
|
+
const fieldSchema = propSchema;
|
|
163
|
+
if (typeof fieldSchema === "boolean") throw new Error("Boolean JSON schemas are not supported in properties");
|
|
164
|
+
let fieldType = JSONWeaver.toMemoriedGraphQLType(fieldSchema);
|
|
165
|
+
if (schema.required?.includes(key)) {
|
|
166
|
+
if (!isNonNullType(fieldType)) fieldType = new GraphQLNonNull(fieldType);
|
|
167
|
+
} else if (isNonNullType(fieldType)) fieldType = fieldType.ofType;
|
|
168
|
+
return {
|
|
169
|
+
type: fieldType,
|
|
170
|
+
description: fieldSchema.description,
|
|
171
|
+
defaultValue: fieldSchema.default
|
|
172
|
+
};
|
|
173
|
+
})
|
|
174
|
+
});
|
|
174
175
|
case "null": throw new Error("Standalone 'null' type is not supported in GraphQL. It can only be used in a union type, like ['string', 'null']");
|
|
175
176
|
}
|
|
176
177
|
throw new Error(`Unsupported JSON schema type: ${String(type)}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gqloom/json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Create GraphQL schema and resolvers easily using using JSON Schema!",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -32,15 +32,14 @@
|
|
|
32
32
|
"author": "xcfox",
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@gqloom/core": ">= 0.
|
|
35
|
+
"@gqloom/core": ">= 0.15.0",
|
|
36
36
|
"graphql": ">= 16.8.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"ajv": "^8.17.1",
|
|
40
|
-
"arktype": "^2.1.
|
|
41
|
-
"effect": "^3.17.13",
|
|
40
|
+
"arktype": "^2.1.29",
|
|
42
41
|
"typebox": "^1.0.4",
|
|
43
|
-
"@gqloom/core": "0.
|
|
42
|
+
"@gqloom/core": "0.15.0"
|
|
44
43
|
},
|
|
45
44
|
"homepage": "https://gqloom.dev/",
|
|
46
45
|
"repository": {
|