@schema-hub/zod-graphql-query-builder 0.0.13 → 0.0.14
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/package.json +16 -5
- package/readme.md +80 -2
- package/sbom.cdx.json +67 -0
- package/zod-graphql-query-builder/build-context.d.ts +4 -0
- package/zod-graphql-query-builder/build-context.d.ts.map +1 -0
- package/zod-graphql-query-builder/build-context.js +10 -0
- package/zod-graphql-query-builder/build-context.js.map +1 -0
- package/zod-graphql-query-builder/builder.d.ts +2 -13
- package/zod-graphql-query-builder/builder.d.ts.map +1 -1
- package/zod-graphql-query-builder/builder.js +19 -159
- package/zod-graphql-query-builder/builder.js.map +1 -1
- package/zod-graphql-query-builder/custom-scalar.d.ts +0 -2
- package/zod-graphql-query-builder/custom-scalar.d.ts.map +1 -1
- package/zod-graphql-query-builder/custom-scalar.js +1 -7
- package/zod-graphql-query-builder/custom-scalar.js.map +1 -1
- package/zod-graphql-query-builder/entry-point.d.ts +1 -4
- package/zod-graphql-query-builder/entry-point.d.ts.map +1 -1
- package/zod-graphql-query-builder/fragment-definitions.d.ts +1 -0
- package/zod-graphql-query-builder/fragment-definitions.d.ts.map +1 -0
- package/zod-graphql-query-builder/fragment-definitions.js +15 -0
- package/zod-graphql-query-builder/fragment-definitions.js.map +1 -0
- package/zod-graphql-query-builder/query-schema.d.ts +63 -27
- package/zod-graphql-query-builder/query-schema.d.ts.map +1 -1
- package/zod-graphql-query-builder/query-schema.js +8 -3
- package/zod-graphql-query-builder/query-schema.js.map +1 -1
- package/zod-graphql-query-builder/root-shape.js +7 -0
- package/zod-graphql-query-builder/root-shape.js.map +1 -0
- package/zod-graphql-query-builder/serializer.d.ts +5 -0
- package/zod-graphql-query-builder/serializer.d.ts.map +1 -0
- package/zod-graphql-query-builder/serializer.js +308 -0
- package/zod-graphql-query-builder/serializer.js.map +1 -0
- package/zod-graphql-query-builder/type-name.d.ts +8 -0
- package/zod-graphql-query-builder/type-name.d.ts.map +1 -0
- package/zod-graphql-query-builder/type-name.js +45 -0
- package/zod-graphql-query-builder/type-name.js.map +1 -0
- package/zod-graphql-query-builder/values/enum.d.ts +0 -2
- package/zod-graphql-query-builder/values/enum.d.ts.map +1 -1
- package/zod-graphql-query-builder/values/enum.js +1 -9
- package/zod-graphql-query-builder/values/enum.js.map +1 -1
- package/zod-graphql-query-builder/values/parameter-list.js +8 -2
- package/zod-graphql-query-builder/values/parameter-list.js.map +1 -1
- package/zod-graphql-query-builder/values/value.d.ts +0 -5
- package/zod-graphql-query-builder/values/value.d.ts.map +1 -1
- package/zod-graphql-query-builder/values/value.js +9 -3
- package/zod-graphql-query-builder/values/value.js.map +1 -1
- package/zod-graphql-query-builder/values/variable-placeholder.d.ts +0 -3
- package/zod-graphql-query-builder/values/variable-placeholder.d.ts.map +1 -1
- package/zod-graphql-query-builder/values/variable-placeholder.js +1 -9
- package/zod-graphql-query-builder/values/variable-placeholder.js.map +1 -1
- package/zod-graphql-query-builder/values/variable-set.js +4 -0
- package/zod-graphql-query-builder/values/variable-set.js.map +1 -0
- package/zod-graphql-query-builder/variable-definition.d.ts +0 -1
- package/zod-graphql-query-builder/variable-definition.d.ts.map +1 -1
- package/zod-graphql-query-builder/variable-definition.js +4 -1
- package/zod-graphql-query-builder/variable-definition.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Mathias Schreck <schreck.mathias@gmail.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"graphql": "16.
|
|
4
|
+
"graphql": "16.14.0"
|
|
5
5
|
},
|
|
6
6
|
"description": "Transforms Zod schemas into GraphQL queries",
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": "^
|
|
8
|
+
"node": "^24 || ^26"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./zod-graphql-query-builder/entry-point.js",
|
|
13
|
+
"types": "./zod-graphql-query-builder/entry-point.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./zod-graphql-query-builder/values/variable-placeholder.js": {
|
|
16
|
+
"import": "./zod-graphql-query-builder/values/variable-placeholder.js",
|
|
17
|
+
"types": "./zod-graphql-query-builder/values/variable-placeholder.d.ts"
|
|
18
|
+
}
|
|
9
19
|
},
|
|
10
20
|
"keywords": [
|
|
11
21
|
"graphql",
|
|
@@ -16,7 +26,6 @@
|
|
|
16
26
|
"zod-graphql"
|
|
17
27
|
],
|
|
18
28
|
"license": "MIT",
|
|
19
|
-
"main": "zod-graphql-query-builder/entry-point.js",
|
|
20
29
|
"name": "@schema-hub/zod-graphql-query-builder",
|
|
21
30
|
"peerDependencies": {
|
|
22
31
|
"zod": "^4.0.5"
|
|
@@ -25,7 +34,9 @@
|
|
|
25
34
|
"type": "git",
|
|
26
35
|
"url": "git+ssh://git@github.com/enormora/schema-hub.git"
|
|
27
36
|
},
|
|
37
|
+
"sideEffects": [
|
|
38
|
+
"./zod-graphql-query-builder/entry-point.js"
|
|
39
|
+
],
|
|
28
40
|
"type": "module",
|
|
29
|
-
"
|
|
30
|
-
"version": "0.0.13"
|
|
41
|
+
"version": "0.0.14"
|
|
31
42
|
}
|
package/readme.md
CHANGED
|
@@ -101,7 +101,7 @@ const query = buildGraphqlQuery(mySchema, { variableDefinitions: { $var: 'String
|
|
|
101
101
|
**Built query:**
|
|
102
102
|
|
|
103
103
|
```graphql
|
|
104
|
-
query ($var: String!) { foo(anyParameterAssignedToPlainValue: "plain-string",
|
|
104
|
+
query ($var: String!) { foo(anyParameterAssignedToEnumValue: foo, anyParameterAssignedToPlainValue: "plain-string", anyParameterReferencingAVariable: $var) }
|
|
105
105
|
```
|
|
106
106
|
|
|
107
107
|
### Defining Fragments
|
|
@@ -129,6 +129,84 @@ const query = buildGraphqlQuery(mySchema);
|
|
|
129
129
|
query { foo { ... on A { __typename, valueA }, ... on B { __typename, valueB } } }
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
+
### Named Fragments / Reusing Schemas
|
|
133
|
+
|
|
134
|
+
When the same object schema reference is used in more than one place in a single operation, the builder
|
|
135
|
+
automatically hoists its body into a named fragment to keep the produced query small. Two ways to opt in
|
|
136
|
+
to this behavior — both rely on the builder being able to resolve a GraphQL type name for the schema.
|
|
137
|
+
|
|
138
|
+
1. Register a `typeName` explicitly via `graphqlFieldOptions`, or
|
|
139
|
+
2. Include `__typename: z.literal('TheTypeName')` in the strict-object's shape.
|
|
140
|
+
|
|
141
|
+
If both are present and disagree the build throws. Schemas without a resolvable type name stay inlined at
|
|
142
|
+
every use site exactly as before, so this feature is purely additive.
|
|
143
|
+
|
|
144
|
+
**Input (explicit `typeName`):**
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { buildGraphqlQuery, graphqlFieldOptions } from '@schema-hub/zod-graphql-query-builder';
|
|
148
|
+
import { z } from 'zod';
|
|
149
|
+
|
|
150
|
+
const userSchema = graphqlFieldOptions(
|
|
151
|
+
z.strictObject({ id: z.string(), name: z.string() }),
|
|
152
|
+
{ typeName: 'User' }
|
|
153
|
+
);
|
|
154
|
+
const mySchema = z.strictObject({ me: userSchema, you: userSchema });
|
|
155
|
+
const query = buildGraphqlQuery(mySchema);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**Built query:**
|
|
159
|
+
|
|
160
|
+
```graphql
|
|
161
|
+
query { me { ...User_1 }, you { ...User_1 } } fragment User_1 on User { id, name }
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Input (`typeName` inferred from `__typename` literal):**
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
const userSchema = z.strictObject({
|
|
168
|
+
__typename: z.literal('User'),
|
|
169
|
+
id: z.string()
|
|
170
|
+
});
|
|
171
|
+
const mySchema = z.strictObject({ me: userSchema, you: userSchema });
|
|
172
|
+
const query = buildGraphqlQuery(mySchema);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Built query:**
|
|
176
|
+
|
|
177
|
+
```graphql
|
|
178
|
+
query { me { ...User_1 }, you { ...User_1 } } fragment User_1 on User { __typename, id }
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Fragment names follow the pattern `<TypeName>_<index>`, where the index is a counter scoped to one
|
|
182
|
+
`buildGraphqlQuery` / `buildGraphqlMutation` call. Two distinct schemas registered with the same `typeName`
|
|
183
|
+
are disambiguated by the counter (`User_1`, `User_2`).
|
|
184
|
+
|
|
185
|
+
**Cyclic schemas.** Self-referential schemas — which would otherwise be inexpressible as a finite inline
|
|
186
|
+
selection — work as long as a type name is resolvable for them. The cyclic reference is emitted as a
|
|
187
|
+
self-recursive fragment:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
type User = { id: string; friends: User[]; };
|
|
191
|
+
const userSchema: z.ZodMiniType<User> = graphqlFieldOptions(
|
|
192
|
+
z.strictObject({
|
|
193
|
+
id: z.string(),
|
|
194
|
+
friends: z.array(z.lazy(() => userSchema))
|
|
195
|
+
}),
|
|
196
|
+
{ typeName: 'User' }
|
|
197
|
+
);
|
|
198
|
+
const query = buildGraphqlQuery(z.strictObject({ me: userSchema }));
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**Built query:**
|
|
202
|
+
|
|
203
|
+
```graphql
|
|
204
|
+
query { me { ...User_1 } } fragment User_1 on User { friends { ...User_1 }, id }
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
A cyclic schema without a resolvable type name is rejected at build time with an explicit error so the
|
|
208
|
+
builder never enters an infinite recursion.
|
|
209
|
+
|
|
132
210
|
### Working with custom scalars
|
|
133
211
|
|
|
134
212
|
**Input:**
|
|
@@ -225,5 +303,5 @@ const mutation = buildGraphqlMutation(mySchema, { variableDefinitions: { $var: '
|
|
|
225
303
|
**Built mutation:**
|
|
226
304
|
|
|
227
305
|
```graphql
|
|
228
|
-
mutation ($var: String!) { foo(anyParameterAssignedToPlainValue: "plain-string",
|
|
306
|
+
mutation ($var: String!) { foo(anyParameterAssignedToEnumValue: foo, anyParameterAssignedToPlainValue: "plain-string", anyParameterReferencingAVariable: $var) }
|
|
229
307
|
```
|
package/sbom.cdx.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
|
|
3
|
+
"bomFormat": "CycloneDX",
|
|
4
|
+
"specVersion": "1.6",
|
|
5
|
+
"version": 1,
|
|
6
|
+
"metadata": {
|
|
7
|
+
"tools": {
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"type": "application",
|
|
11
|
+
"name": "packtory",
|
|
12
|
+
"version": "0.0.26"
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"component": {
|
|
17
|
+
"type": "library",
|
|
18
|
+
"name": "@schema-hub/zod-graphql-query-builder",
|
|
19
|
+
"version": "0.0.14",
|
|
20
|
+
"bom-ref": "pkg:npm/@schema-hub/zod-graphql-query-builder@0.0.14",
|
|
21
|
+
"purl": "pkg:npm/@schema-hub/zod-graphql-query-builder@0.0.14"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"components": [
|
|
25
|
+
{
|
|
26
|
+
"type": "library",
|
|
27
|
+
"name": "graphql",
|
|
28
|
+
"version": "16.14.0",
|
|
29
|
+
"bom-ref": "pkg:npm/graphql@16.14.0",
|
|
30
|
+
"scope": "required",
|
|
31
|
+
"licenses": [
|
|
32
|
+
{
|
|
33
|
+
"expression": "MIT"
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"purl": "pkg:npm/graphql@16.14.0"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"type": "library",
|
|
40
|
+
"name": "zod",
|
|
41
|
+
"version": "^4.0.5",
|
|
42
|
+
"bom-ref": "pkg:npm/zod@%5E4.0.5",
|
|
43
|
+
"scope": "optional",
|
|
44
|
+
"licenses": [
|
|
45
|
+
{
|
|
46
|
+
"expression": "MIT"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"purl": "pkg:npm/zod@%5E4.0.5"
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
"dependencies": [
|
|
53
|
+
{
|
|
54
|
+
"ref": "pkg:npm/@schema-hub/zod-graphql-query-builder@0.0.14",
|
|
55
|
+
"dependsOn": [
|
|
56
|
+
"pkg:npm/graphql@16.14.0",
|
|
57
|
+
"pkg:npm/zod@%5E4.0.5"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"ref": "pkg:npm/graphql@16.14.0"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"ref": "pkg:npm/zod@%5E4.0.5"
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-context.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/build-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B;AACnE,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB;AAChE,OAAO,EAA2B,KAAK,oBAAoB,EAAE,MAAM,iBAAiB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { collectSchemaReferences } from './serializer.js';
|
|
2
|
+
export function createBuildContext(registry, rootShape) {
|
|
3
|
+
return {
|
|
4
|
+
counts: collectSchemaReferences(registry, rootShape),
|
|
5
|
+
nameForSchema: new Map(),
|
|
6
|
+
counterPerTypeName: new Map(),
|
|
7
|
+
definitions: new Map()
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=build-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-context.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/build-context.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAA6B,MAAM,iBAAiB,CAAC;AAWrF,MAAM,UAAU,kBAAkB,CAC9B,QAA8B,EAC9B,SAAqB;IAErB,OAAO;QACH,MAAM,EAAE,uBAAuB,CAAC,QAAQ,EAAE,SAAS,CAAC;QACpD,aAAa,EAAE,IAAI,GAAG,EAAE;QACxB,kBAAkB,EAAE,IAAI,GAAG,EAAE;QAC7B,WAAW,EAAE,IAAI,GAAG,EAAE;KACzB,CAAC;AACN,CAAC"}
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type
|
|
1
|
+
import type { FieldSchema, QuerySchema } from './query-schema.js';
|
|
2
|
+
import { type GraphqlFieldOptions } from './serializer.js';
|
|
3
3
|
import { type VariableDefinitions } from './variable-definition.js';
|
|
4
|
-
type GraphqlFieldOptions = {
|
|
5
|
-
aliasFor?: string;
|
|
6
|
-
parameters?: Record<string, GraphqlValue>;
|
|
7
|
-
};
|
|
8
4
|
export type OperationOptions = {
|
|
9
5
|
operationName?: string | undefined;
|
|
10
6
|
variableDefinitions?: VariableDefinitions | undefined;
|
|
11
7
|
};
|
|
12
|
-
export type QueryBuilder = {
|
|
13
|
-
registerFieldOptions: <Schema extends FieldSchema>(schema: Schema, options: GraphqlFieldOptions) => Schema;
|
|
14
|
-
buildQuery: (schema: QuerySchema, options?: OperationOptions) => string;
|
|
15
|
-
buildMutation: (schema: QuerySchema, options?: OperationOptions) => string;
|
|
16
|
-
};
|
|
17
|
-
export declare function createQueryBuilder(): QueryBuilder;
|
|
18
|
-
export {};
|
|
19
8
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/builder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/builder.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB;AAEjE,OAAO,EAEH,KAAK,mBAAmB,EAE3B,MAAM,iBAAiB;AAGxB,OAAO,EAAgC,KAAK,mBAAmB,EAAE,MAAM,0BAA0B;AAEjG,MAAM,MAAM,gBAAgB,GAAG;IAC3B,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,mBAAmB,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAC;CACzD"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { createBuildContext } from './build-context.js';
|
|
2
|
+
import { formatFragmentDefinitions } from './fragment-definitions.js';
|
|
3
|
+
import { extractRootShape } from './root-shape.js';
|
|
4
|
+
import { serializeRootShape } from './serializer.js';
|
|
5
|
+
import { isValidGraphqlName } from './values/name.js';
|
|
6
6
|
import { ensureValidVariableCorrelations } from './variable-correlations.js';
|
|
7
7
|
import { serializeVariableDefinitions } from './variable-definition.js';
|
|
8
8
|
function withTrailingSpace(value) {
|
|
@@ -11,171 +11,31 @@ function withTrailingSpace(value) {
|
|
|
11
11
|
}
|
|
12
12
|
return `${value} `;
|
|
13
13
|
}
|
|
14
|
-
function
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// @ts-ignore - Type instantiation is not infinite
|
|
24
|
-
const { items } = schema._zod.def;
|
|
25
|
-
const [firstElementSchema] = items;
|
|
26
|
-
const unwrappedElementSchema = unwrapFieldSchema(firstElementSchema);
|
|
27
|
-
if (predicate(unwrappedElementSchema)) {
|
|
28
|
-
return unwrappedElementSchema;
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
function getObjectSchema(schema) {
|
|
33
|
-
if (isStrictObjectSchema(schema)) {
|
|
34
|
-
return schema;
|
|
35
|
-
}
|
|
36
|
-
if (schema instanceof $ZodArray) {
|
|
37
|
-
return unwrapFromArraySchema(isStrictObjectSchema, schema);
|
|
38
|
-
}
|
|
39
|
-
return unwrapFromTupleSchema(isStrictObjectSchema, schema);
|
|
40
|
-
}
|
|
41
|
-
function getUnionSchema(schema) {
|
|
42
|
-
if (schema instanceof $ZodDiscriminatedUnion) {
|
|
43
|
-
return schema;
|
|
44
|
-
}
|
|
45
|
-
if (schema instanceof $ZodArray) {
|
|
46
|
-
return unwrapFromArraySchema(isFragmentsSchema, schema);
|
|
47
|
-
}
|
|
48
|
-
return unwrapFromTupleSchema(isFragmentsSchema, schema);
|
|
14
|
+
function buildDocument(registry, documentType, schema, options) {
|
|
15
|
+
const { variableDefinitions = {}, operationName = '' } = options;
|
|
16
|
+
const shape = extractRootShape(schema);
|
|
17
|
+
const context = createBuildContext(registry, shape);
|
|
18
|
+
const { bodyEntries, referencedVariables } = serializeRootShape(registry, shape, context);
|
|
19
|
+
ensureValidVariableCorrelations(variableDefinitions, referencedVariables);
|
|
20
|
+
const operationNameAndParams = withTrailingSpace(`${operationName}${serializeVariableDefinitions(variableDefinitions)}`);
|
|
21
|
+
const operationOutput = `${documentType} ${operationNameAndParams}{ ${bodyEntries.join(', ')} }`;
|
|
22
|
+
return operationOutput + formatFragmentDefinitions(context.definitions);
|
|
49
23
|
}
|
|
50
24
|
export function createQueryBuilder() {
|
|
51
25
|
const fieldOptionsRegistry = new WeakMap();
|
|
52
|
-
function getFieldOptionsForSchema(schema) {
|
|
53
|
-
const unwrappingResult = unwrapFieldSchemaChain(schema);
|
|
54
|
-
const queue = [...unwrappingResult.wrapperElements, unwrappingResult.unwrappedSchema];
|
|
55
|
-
for (const currentSchema of queue) {
|
|
56
|
-
const fieldOptions = fieldOptionsRegistry.get(currentSchema);
|
|
57
|
-
if (fieldOptions !== undefined) {
|
|
58
|
-
return fieldOptions;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return {};
|
|
62
|
-
}
|
|
63
|
-
function serializedFieldSelector(fieldName, fieldSchema) {
|
|
64
|
-
const fieldOptions = getFieldOptionsForSchema(fieldSchema);
|
|
65
|
-
const { aliasFor, parameters } = fieldOptions;
|
|
66
|
-
const selectorName = aliasFor === undefined ? fieldName : `${fieldName}: ${aliasFor}`;
|
|
67
|
-
if (parameters !== undefined) {
|
|
68
|
-
const normalizedParameterList = normalizeParameterList(parameters);
|
|
69
|
-
return {
|
|
70
|
-
serializedValue: `${selectorName}${normalizedParameterList.serializedValue}`,
|
|
71
|
-
referencedVariables: normalizedParameterList.referencedVariables
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
return {
|
|
75
|
-
serializedValue: selectorName,
|
|
76
|
-
referencedVariables: new Set()
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
function serializeObjectSchema(objectSchema) {
|
|
80
|
-
const entries = Object.entries(objectSchema._zod.def.shape);
|
|
81
|
-
let referencedVariables = new Set();
|
|
82
|
-
const serializedEntries = [];
|
|
83
|
-
for (const [fieldName, fieldSchema] of entries) {
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define -- recursion
|
|
85
|
-
const serializedField = serializeFieldSchema(fieldName, fieldSchema);
|
|
86
|
-
referencedVariables = new Set([...referencedVariables, ...serializedField.referencedVariables]);
|
|
87
|
-
serializedEntries.push(serializedField.serializedValue);
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
serializedValue: ` { ${serializedEntries.join(', ')} }`,
|
|
91
|
-
referencedVariables
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
// eslint-disable-next-line max-statements -- no good idea how to refactor at the moment
|
|
95
|
-
function serializeFragments(discriminatorMap, unionOptions) {
|
|
96
|
-
let referencedVariables = new Set();
|
|
97
|
-
const serializedFragments = [];
|
|
98
|
-
const typeNameDiscriminator = discriminatorMap.__typename;
|
|
99
|
-
const fragmentNames = Array.from(typeNameDiscriminator ?? []);
|
|
100
|
-
for (const [index, fragmentSchema] of unionOptions.entries()) {
|
|
101
|
-
const fragmentName = fragmentNames[index];
|
|
102
|
-
if (fragmentName === undefined || fragmentName === null) {
|
|
103
|
-
throw new Error(`Fragment name for index ${index} is undefined`);
|
|
104
|
-
}
|
|
105
|
-
const serializedFragment = serializeObjectSchema(fragmentSchema);
|
|
106
|
-
referencedVariables = new Set([...referencedVariables, ...serializedFragment.referencedVariables]);
|
|
107
|
-
serializedFragments.push(`... on ${fragmentName.toString()}${serializedFragment.serializedValue}`);
|
|
108
|
-
}
|
|
109
|
-
return {
|
|
110
|
-
serializedValue: ` { ${serializedFragments.join(', ')} }`,
|
|
111
|
-
referencedVariables
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function combineFieldSelectorAndFieldBody(fieldSelector, fieldBody) {
|
|
115
|
-
return {
|
|
116
|
-
serializedValue: `${fieldSelector.serializedValue}${fieldBody.serializedValue}`,
|
|
117
|
-
referencedVariables: new Set([
|
|
118
|
-
...fieldSelector.referencedVariables,
|
|
119
|
-
...fieldBody.referencedVariables
|
|
120
|
-
])
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
// eslint-disable-next-line max-statements, complexity -- no good idea how to refactor at the moment
|
|
124
|
-
function serializeFieldSchema(fieldName, fieldSchema) {
|
|
125
|
-
const fieldSelector = serializedFieldSelector(fieldName, fieldSchema);
|
|
126
|
-
if (isCustomScalarSchema(fieldSchema)) {
|
|
127
|
-
return fieldSelector;
|
|
128
|
-
}
|
|
129
|
-
const unwrappedSchema = unwrapFieldSchema(fieldSchema);
|
|
130
|
-
if (unwrappedSchema instanceof $ZodUndefined) {
|
|
131
|
-
return { serializedValue: '', referencedVariables: new Set() };
|
|
132
|
-
}
|
|
133
|
-
if (isObjectOrListSchema(unwrappedSchema)) {
|
|
134
|
-
const objectSchema = getObjectSchema(unwrappedSchema);
|
|
135
|
-
if (objectSchema !== null) {
|
|
136
|
-
return combineFieldSelectorAndFieldBody(fieldSelector, serializeObjectSchema(objectSchema));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (isUnionOrListSchema(unwrappedSchema)) {
|
|
140
|
-
const unionSchema = getUnionSchema(unwrappedSchema);
|
|
141
|
-
if (unionSchema !== null) {
|
|
142
|
-
return combineFieldSelectorAndFieldBody(fieldSelector, serializeFragments(unionSchema._zod.propValues, unionSchema._zod.def.options));
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return fieldSelector;
|
|
146
|
-
}
|
|
147
|
-
// eslint-disable-next-line max-statements -- no idea right now to make this smaller
|
|
148
|
-
function buildDocument(documentType, schema, options) {
|
|
149
|
-
let referencedVariables = new Set();
|
|
150
|
-
const { variableDefinitions = {}, operationName = '' } = options;
|
|
151
|
-
const bodyEntries = [];
|
|
152
|
-
const shape = schema instanceof $ZodReadonly ?
|
|
153
|
-
schema._zod.def.innerType._zod.def.shape :
|
|
154
|
-
schema._zod.def.shape;
|
|
155
|
-
for (const [fieldName, fieldSchema] of Object.entries(shape)) {
|
|
156
|
-
const serializedField = serializeFieldSchema(fieldName, fieldSchema);
|
|
157
|
-
if (serializedField.serializedValue.length > 0) {
|
|
158
|
-
bodyEntries.push(serializedField.serializedValue);
|
|
159
|
-
}
|
|
160
|
-
referencedVariables = new Set([
|
|
161
|
-
...referencedVariables,
|
|
162
|
-
...serializedField.referencedVariables
|
|
163
|
-
]);
|
|
164
|
-
}
|
|
165
|
-
ensureValidVariableCorrelations(variableDefinitions, referencedVariables);
|
|
166
|
-
const operationNameAndParams = withTrailingSpace(`${operationName}${serializeVariableDefinitions(variableDefinitions)}`);
|
|
167
|
-
return `${documentType} ${operationNameAndParams}{ ${bodyEntries.join(', ')} }`;
|
|
168
|
-
}
|
|
169
26
|
return {
|
|
170
27
|
registerFieldOptions(schema, options) {
|
|
28
|
+
if (options.typeName !== undefined && !isValidGraphqlName(options.typeName)) {
|
|
29
|
+
throw new Error(`Invalid GraphQL type name: "${options.typeName}". A type name must match /^[A-Z_a-z]\\w*$/.`);
|
|
30
|
+
}
|
|
171
31
|
fieldOptionsRegistry.set(schema, options);
|
|
172
32
|
return schema;
|
|
173
33
|
},
|
|
174
34
|
buildQuery(schema, options = {}) {
|
|
175
|
-
return buildDocument('query', schema, options);
|
|
35
|
+
return buildDocument(fieldOptionsRegistry, 'query', schema, options);
|
|
176
36
|
},
|
|
177
37
|
buildMutation(schema, options = {}) {
|
|
178
|
-
return buildDocument('mutation', schema, options);
|
|
38
|
+
return buildDocument(fieldOptionsRegistry, 'mutation', schema, options);
|
|
179
39
|
}
|
|
180
40
|
};
|
|
181
41
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/builder.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAGH,kBAAkB,EACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,+BAA+B,EAAE,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAE,4BAA4B,EAA4B,MAAM,0BAA0B,CAAC;AAgBlG,SAAS,iBAAiB,CAAC,KAAa;IACpC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,OAAO,GAAG,KAAK,GAAG,CAAC;AACvB,CAAC;AAED,SAAS,aAAa,CAClB,QAA8B,EAC9B,YAAkC,EAClC,MAAmB,EACnB,OAAyB;IAEzB,MAAM,EAAE,mBAAmB,GAAG,EAAE,EAAE,aAAa,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IACjE,MAAM,KAAK,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,kBAAkB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACpD,MAAM,EAAE,WAAW,EAAE,mBAAmB,EAAE,GAAG,kBAAkB,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAE1F,+BAA+B,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,CAAC;IAC1E,MAAM,sBAAsB,GAAG,iBAAiB,CAC5C,GAAG,aAAa,GAAG,4BAA4B,CAAC,mBAAmB,CAAC,EAAE,CACzE,CAAC;IACF,MAAM,eAAe,GAAG,GAAG,YAAY,IAAI,sBAAsB,KAAK,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;IACjG,OAAO,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;AAC5E,CAAC;AAED,MAAM,UAAU,kBAAkB;IAC9B,MAAM,oBAAoB,GAAyB,IAAI,OAAO,EAAE,CAAC;IAEjE,OAAO;QACH,oBAAoB,CAChB,MAAc,EACd,OAA4B;YAE5B,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;gBAC1E,MAAM,IAAI,KAAK,CACX,+BAA+B,OAAO,CAAC,QAAQ,8CAA8C,CAChG,CAAC;YACN,CAAC;YACD,oBAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAC1C,OAAO,MAAM,CAAC;QAClB,CAAC;QAED,UAAU,CAAC,MAAM,EAAE,OAAO,GAAG,EAAE;YAC3B,OAAO,aAAa,CAAC,oBAAoB,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACzE,CAAC;QAED,aAAa,CAAC,MAAM,EAAE,OAAO,GAAG,EAAE;YAC9B,OAAO,aAAa,CAAC,oBAAoB,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAC5E,CAAC;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -3,7 +3,5 @@ declare const customScalarSymbol: unique symbol;
|
|
|
3
3
|
export type CustomScalarSchema<Schema extends $ZodType> = $ZodLazy<Schema> & {
|
|
4
4
|
[customScalarSymbol]: true;
|
|
5
5
|
};
|
|
6
|
-
export declare function isCustomScalarSchema(schema: $ZodType): schema is CustomScalarSchema<$ZodType>;
|
|
7
|
-
export declare function createCustomScalarSchema<Schema extends $ZodType>(schema: Schema): CustomScalarSchema<Schema>;
|
|
8
6
|
export {};
|
|
9
7
|
//# sourceMappingURL=custom-scalar.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-scalar.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/custom-scalar.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa
|
|
1
|
+
{"version":3,"file":"custom-scalar.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/custom-scalar.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa;AAErD,QAAA,MAAM,kBAAkB,EAAE,OAAO,MAAgC;AAEjE,MAAM,MAAM,kBAAkB,CAAC,MAAM,SAAS,QAAQ,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG;IACzE,CAAC,kBAAkB,CAAC,EAAE,IAAI,CAAC;CAC9B"}
|
|
@@ -3,11 +3,5 @@ const customScalarSymbol = Symbol('custom-scalar');
|
|
|
3
3
|
export function isCustomScalarSchema(schema) {
|
|
4
4
|
return Object.hasOwn(schema, customScalarSymbol);
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
const wrappedSchema = z.lazy(() => {
|
|
8
|
-
return schema;
|
|
9
|
-
});
|
|
10
|
-
wrappedSchema[customScalarSymbol] = true;
|
|
11
|
-
return wrappedSchema;
|
|
12
|
-
}
|
|
6
|
+
|
|
13
7
|
//# sourceMappingURL=custom-scalar.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-scalar.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/custom-scalar.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,aAAa
|
|
1
|
+
{"version":3,"file":"custom-scalar.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/custom-scalar.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,aAAa;AAG/B,MAAM,kBAAkB,GAAkB,MAAM,CAAC,eAAe,CAAC;AAMjE,MAAM,UAAU,oBAAoB,CAAC,MAAgB;IACjD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;AACrD"}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export declare const graphqlFieldOptions: <Schema extends import("./query-schema.js").FieldSchema>(schema: Schema, options:
|
|
2
|
-
aliasFor?: string;
|
|
3
|
-
parameters?: Record<string, import("./values/value.js").GraphqlValue>;
|
|
4
|
-
}) => Schema;
|
|
1
|
+
export declare const graphqlFieldOptions: <Schema extends import("./query-schema.js").FieldSchema>(schema: Schema, options: import("./type-name.js").GraphqlFieldOptions) => Schema;
|
|
5
2
|
export declare const buildGraphqlQuery: (schema: import("./query-schema.js").QuerySchema, options?: import("./builder.js").OperationOptions) => string;
|
|
6
3
|
export declare const buildGraphqlMutation: (schema: import("./query-schema.js").QuerySchema, options?: import("./builder.js").OperationOptions) => string;
|
|
7
4
|
export { createCustomScalarSchema as customScalar } from './custom-scalar.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry-point.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/entry-point.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,mBAAmB
|
|
1
|
+
{"version":3,"file":"entry-point.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/entry-point.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,mBAAmB,2IAA+B,CAAC;AAChE,eAAO,MAAM,iBAAiB,gHAAqB,CAAC;AACpD,eAAO,MAAM,oBAAoB,gHAAwB,CAAC;AAE1D,OAAO,EAAE,wBAAwB,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAEvE,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=fragment-definitions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragment-definitions.d.ts","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/fragment-definitions.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function formatFragmentDefinitions(definitions) {
|
|
2
|
+
if (definitions.size === 0) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
const sortedEntries = Array
|
|
6
|
+
.from(definitions.entries())
|
|
7
|
+
.toSorted(([nameA], [nameB]) => {
|
|
8
|
+
return nameA.localeCompare(nameB);
|
|
9
|
+
});
|
|
10
|
+
const formatted = sortedEntries.map(([name, definition]) => {
|
|
11
|
+
return `fragment ${name} on ${definition.typeName}${definition.body}`;
|
|
12
|
+
});
|
|
13
|
+
return ` ${formatted.join(' ')}`;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=fragment-definitions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragment-definitions.js","sourceRoot":"","sources":["../../../../source/zod-graphql-query-builder/fragment-definitions.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,yBAAyB,CAAC,WAA4C;IAClF,IAAI,WAAW,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,EAAE,CAAC;IACd,CAAC;IACD,MAAM,aAAa,GAAG,KAAK;SACtB,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;SAC3B,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE;QAC3B,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IACP,MAAM,SAAS,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,EAAE;QACvD,OAAO,YAAY,IAAI,OAAO,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC;IAC1E,CAAC,CAAC,CAAC;IACH,OAAO,IAAI,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;AACrC,CAAC"}
|