@gqloom/json 0.11.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 CHANGED
@@ -1,30 +1,5 @@
1
- //#region rolldown:runtime
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
- [__gqloom_core.SYMBOLS.WEAVER_CONFIG]: "gqloom.json"
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 = __gqloom_core.weaverContext.value?.getConfig("gqloom.json");
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" && !(__gqloom_core.SYMBOLS.GET_GRAPHQL_TYPE in schema)) Object.defineProperty(schema, __gqloom_core.SYMBOLS.GET_GRAPHQL_TYPE, {
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 __gqloom_core.weaverContext.useConfig(config, () => JSONWeaver.getGraphQLTypeBySelf.call(this));
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 Map();
41
+ static sourceTypeMap = /* @__PURE__ */ new WeakMap();
67
42
  static getGraphQLType(schema, { source } = {}) {
68
- if (source) JSONWeaver.sourceTypeMap.set(schema, source);
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 = __gqloom_core.weaverContext.getGraphQLType(schema) ?? (name ? __gqloom_core.weaverContext.getNamedType(name) : void 0);
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) __gqloom_core.weaverContext.memoNamedType(gqlType);
88
- return __gqloom_core.weaverContext.memoGraphQLType(schema, gqlType);
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 = (__gqloom_core.weaverContext.value?.getConfig("gqloom.json"))?.presetGraphQLType?.(schema);
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
- const interfaceObjectType = JSONWeaver.toMemoriedGraphQLType(interfaceSchema);
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,8 +121,7 @@ var JSONWeaver = class JSONWeaver {
140
121
  };
141
122
  return JSONWeaver.toGraphQLTypeInner(unwrappedSchema);
142
123
  }
143
- const name = schema.title ?? JSONWeaver.getCollectedName(schema);
144
- if (!name) throw new Error("Union type must have a name");
124
+ const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? _gqloom_core.AUTO_ALIASING;
145
125
  return new graphql.GraphQLUnionType({
146
126
  name,
147
127
  description: schema.description,
@@ -154,8 +134,7 @@ var JSONWeaver = class JSONWeaver {
154
134
  }
155
135
  const type = Array.isArray(schema.type) ? schema.type.find((t) => t !== "null") : schema.type;
156
136
  if (schema.enum) {
157
- const name = schema.title ?? JSONWeaver.getCollectedName(schema);
158
- if (!name) throw new Error("Enum type must have a name");
137
+ const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? _gqloom_core.AUTO_ALIASING;
159
138
  const values = {};
160
139
  for (const value of schema.enum) if (typeof value === "string" || typeof value === "number") {
161
140
  const key = String(value).replace(/[^_a-zA-Z0-9]/g, "_");
@@ -172,32 +151,27 @@ var JSONWeaver = class JSONWeaver {
172
151
  case "number": return graphql.GraphQLFloat;
173
152
  case "integer": return graphql.GraphQLInt;
174
153
  case "boolean": return graphql.GraphQLBoolean;
175
- case "array": {
154
+ case "array":
176
155
  if (!schema.items || typeof schema.items !== "object" || Array.isArray(schema.items)) throw new Error("Array schema must have a single object in 'items'");
177
- const itemType = JSONWeaver.toNullableGraphQLType(schema.items);
178
- return new graphql.GraphQLList(itemType);
179
- }
180
- case "object": {
181
- const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? JSONWeaver.getTypeName(schema) ?? __gqloom_core.LoomObjectType.AUTO_ALIASING;
182
- return new graphql.GraphQLObjectType({
183
- name,
184
- description: schema.description,
185
- fields: () => (0, __gqloom_core.mapValue)(schema.properties ?? {}, (propSchema, key) => {
186
- if (key.startsWith("__")) return __gqloom_core.mapValue.SKIP;
187
- const fieldSchema = propSchema;
188
- if (typeof fieldSchema === "boolean") throw new Error("Boolean JSON schemas are not supported in properties");
189
- let fieldType = JSONWeaver.toMemoriedGraphQLType(fieldSchema);
190
- if (schema.required?.includes(key)) {
191
- if (!(0, graphql.isNonNullType)(fieldType)) fieldType = new graphql.GraphQLNonNull(fieldType);
192
- } else if ((0, graphql.isNonNullType)(fieldType)) fieldType = fieldType.ofType;
193
- return {
194
- type: fieldType,
195
- description: fieldSchema.description,
196
- defaultValue: fieldSchema.default
197
- };
198
- })
199
- });
200
- }
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
+ });
201
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']");
202
176
  }
203
177
  throw new Error(`Unsupported JSON schema type: ${String(type)}`);
@@ -211,11 +185,11 @@ var JSONWeaver = class JSONWeaver {
211
185
  }
212
186
  static getCollectedName(schema) {
213
187
  if (typeof schema !== "object") return void 0;
214
- const name = __gqloom_core.weaverContext.names.get(schema);
188
+ const name = _gqloom_core.weaverContext.names.get(schema);
215
189
  if (name) return name;
216
190
  const source = JSONWeaver.sourceTypeMap.get(schema);
217
191
  if (source == null) return void 0;
218
- return __gqloom_core.weaverContext.names.get(source);
192
+ return _gqloom_core.weaverContext.names.get(source);
219
193
  }
220
194
  };
221
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: Map<JSONSchema, any>;
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?: any;
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: Map<JSONSchema, any>;
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?: any;
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
@@ -1,4 +1,4 @@
1
- import { LoomObjectType, SYMBOLS, ensureInterfaceType, mapValue, weaverContext } from "@gqloom/core";
1
+ import { AUTO_ALIASING, SYMBOLS, ensureInterfaceType, mapValue, weaverContext } from "@gqloom/core";
2
2
  import { GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString, GraphQLUnionType, isNonNullType, isObjectType } from "graphql";
3
3
 
4
4
  //#region src/index.ts
@@ -38,9 +38,16 @@ var JSONWeaver = class JSONWeaver {
38
38
  });
39
39
  return schema;
40
40
  }
41
- static sourceTypeMap = /* @__PURE__ */ new Map();
41
+ static sourceTypeMap = /* @__PURE__ */ new WeakMap();
42
42
  static getGraphQLType(schema, { source } = {}) {
43
- if (source) JSONWeaver.sourceTypeMap.set(schema, source);
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
- const interfaceObjectType = JSONWeaver.toMemoriedGraphQLType(interfaceSchema);
94
- return ensureInterfaceType(interfaceObjectType);
100
+ return ensureInterfaceType(JSONWeaver.toMemoriedGraphQLType(interfaceSchema));
95
101
  });
96
102
  return new GraphQLObjectType({
97
103
  ...objectType.toConfig(),
@@ -115,8 +121,7 @@ var JSONWeaver = class JSONWeaver {
115
121
  };
116
122
  return JSONWeaver.toGraphQLTypeInner(unwrappedSchema);
117
123
  }
118
- const name = schema.title ?? JSONWeaver.getCollectedName(schema);
119
- if (!name) throw new Error("Union type must have a name");
124
+ const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? AUTO_ALIASING;
120
125
  return new GraphQLUnionType({
121
126
  name,
122
127
  description: schema.description,
@@ -129,8 +134,7 @@ var JSONWeaver = class JSONWeaver {
129
134
  }
130
135
  const type = Array.isArray(schema.type) ? schema.type.find((t) => t !== "null") : schema.type;
131
136
  if (schema.enum) {
132
- const name = schema.title ?? JSONWeaver.getCollectedName(schema);
133
- if (!name) throw new Error("Enum type must have a name");
137
+ const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? AUTO_ALIASING;
134
138
  const values = {};
135
139
  for (const value of schema.enum) if (typeof value === "string" || typeof value === "number") {
136
140
  const key = String(value).replace(/[^_a-zA-Z0-9]/g, "_");
@@ -147,32 +151,27 @@ var JSONWeaver = class JSONWeaver {
147
151
  case "number": return GraphQLFloat;
148
152
  case "integer": return GraphQLInt;
149
153
  case "boolean": return GraphQLBoolean;
150
- case "array": {
154
+ case "array":
151
155
  if (!schema.items || typeof schema.items !== "object" || Array.isArray(schema.items)) throw new Error("Array schema must have a single object in 'items'");
152
- const itemType = JSONWeaver.toNullableGraphQLType(schema.items);
153
- return new GraphQLList(itemType);
154
- }
155
- case "object": {
156
- const name = schema.title ?? JSONWeaver.getCollectedName(schema) ?? JSONWeaver.getTypeName(schema) ?? LoomObjectType.AUTO_ALIASING;
157
- return new GraphQLObjectType({
158
- name,
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
- });
175
- }
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
+ });
176
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']");
177
176
  }
178
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.11.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.11.1",
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.22",
41
- "effect": "^3.17.13",
40
+ "arktype": "^2.1.29",
42
41
  "typebox": "^1.0.4",
43
- "@gqloom/core": "0.11.1"
42
+ "@gqloom/core": "0.15.0"
44
43
  },
45
44
  "homepage": "https://gqloom.dev/",
46
45
  "repository": {