buildgql 0.1.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/LICENSE +21 -0
- package/README.md +375 -0
- package/dist/adapters/apollo.cjs +23 -0
- package/dist/adapters/apollo.d.cts +28 -0
- package/dist/adapters/apollo.d.ts +28 -0
- package/dist/adapters/apollo.js +23 -0
- package/dist/adapters/urql.cjs +16 -0
- package/dist/adapters/urql.d.cts +26 -0
- package/dist/adapters/urql.d.ts +26 -0
- package/dist/adapters/urql.js +16 -0
- package/dist/chunk-2Z2AKLA3.cjs +267 -0
- package/dist/chunk-BM7L4MCF.cjs +27 -0
- package/dist/chunk-RSDTCILU.js +267 -0
- package/dist/chunk-SNFCCROD.js +27 -0
- package/dist/cli/bin.js +842 -0
- package/dist/cli/config.cjs +272 -0
- package/dist/cli/config.d.cts +73 -0
- package/dist/cli/config.d.ts +73 -0
- package/dist/cli/config.js +245 -0
- package/dist/client/index.cjs +16 -0
- package/dist/client/index.d.cts +93 -0
- package/dist/client/index.d.ts +93 -0
- package/dist/client/index.js +16 -0
- package/dist/document-BZ9RdxXB.d.cts +16 -0
- package/dist/document-BZ9RdxXB.d.ts +16 -0
- package/dist/index.cjs +313 -0
- package/dist/index.d.cts +85 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +313 -0
- package/dist/operation-DxbEION8.d.cts +200 -0
- package/dist/operation-DxbEION8.d.ts +200 -0
- package/package.json +148 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/** Phantom carrier for a selection's *result key* (field name or alias). */
|
|
2
|
+
declare const KEY: unique symbol;
|
|
3
|
+
/** Phantom carrier for a selection's inferred result type. */
|
|
4
|
+
declare const RESULT: unique symbol;
|
|
5
|
+
/** Phantom carrier for the variables a selection contributes. */
|
|
6
|
+
declare const VARS: unique symbol;
|
|
7
|
+
|
|
8
|
+
/** A variable reference recorded at build time, used to emit `$name: Type!`. */
|
|
9
|
+
interface VarRef {
|
|
10
|
+
readonly varName: string;
|
|
11
|
+
readonly gqlType: string;
|
|
12
|
+
}
|
|
13
|
+
/** An `@include`/`@skip` directive attached to a field selection. */
|
|
14
|
+
interface Directive {
|
|
15
|
+
readonly name: 'include' | 'skip';
|
|
16
|
+
/** Either a literal boolean or a variable reference. */
|
|
17
|
+
readonly if: boolean | VarRef;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A selected field.
|
|
21
|
+
* `N` is the key it lands under in the result. It is carried by the phantom
|
|
22
|
+
* `[KEY]` property — it MUST appear structurally, or `Extract`/`Exclude` cannot
|
|
23
|
+
* tell two selections apart and the whole remap collapses.
|
|
24
|
+
* `O` marks the field optional in the result (set by `@include`/`@skip`).
|
|
25
|
+
*/
|
|
26
|
+
interface FieldSelection<N extends string, R, V = {}, O extends boolean = false> {
|
|
27
|
+
readonly kind: 'field';
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly alias?: string;
|
|
30
|
+
readonly args?: Record<string, unknown>;
|
|
31
|
+
readonly sels?: readonly SelectionNode[];
|
|
32
|
+
readonly directives?: readonly Directive[];
|
|
33
|
+
readonly varRefs?: readonly VarRef[];
|
|
34
|
+
readonly [KEY]?: N;
|
|
35
|
+
readonly [RESULT]?: R;
|
|
36
|
+
readonly [VARS]?: V;
|
|
37
|
+
readonly __optional?: O;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The part of a fragment a spread needs to carry: enough to emit the definition
|
|
41
|
+
* and to walk into it for variables. Declared here rather than in
|
|
42
|
+
* `src/runtime/fragment.ts` so `print.ts` and `fragment.ts` share one contract
|
|
43
|
+
* instead of each re-declaring the shape behind a cast.
|
|
44
|
+
*/
|
|
45
|
+
interface FragmentDefinition {
|
|
46
|
+
readonly name: string;
|
|
47
|
+
readonly typeCondition: string;
|
|
48
|
+
readonly sels: readonly SelectionNode[];
|
|
49
|
+
}
|
|
50
|
+
/** A fragment spread. Required `kind` discriminant makes Extract/Exclude work. */
|
|
51
|
+
interface FragmentSpread<R, V = {}> {
|
|
52
|
+
readonly kind: 'spread';
|
|
53
|
+
/**
|
|
54
|
+
* The fragment being spread. Consumed by `collectFragments` and the variable walk
|
|
55
|
+
* in `print.ts`. Its `name` is the single source of truth for the fragment's printed
|
|
56
|
+
* name — there is deliberately no separate `fragmentName` field to keep in sync.
|
|
57
|
+
*/
|
|
58
|
+
readonly fragment: FragmentDefinition;
|
|
59
|
+
readonly [RESULT]?: R;
|
|
60
|
+
readonly [VARS]?: V;
|
|
61
|
+
}
|
|
62
|
+
/** An inline fragment (`... on Dog { ... }`). */
|
|
63
|
+
interface InlineFragment<TN extends string, R, V = {}> {
|
|
64
|
+
readonly kind: 'on';
|
|
65
|
+
readonly typename: string;
|
|
66
|
+
readonly sels: readonly SelectionNode[];
|
|
67
|
+
readonly [KEY]?: TN;
|
|
68
|
+
readonly [RESULT]?: R;
|
|
69
|
+
readonly [VARS]?: V;
|
|
70
|
+
}
|
|
71
|
+
type AnyFieldSelection = FieldSelection<string, unknown, unknown, boolean>;
|
|
72
|
+
type SelectionNode = AnyFieldSelection | FragmentSpread<unknown, unknown> | InlineFragment<string, unknown, unknown>;
|
|
73
|
+
|
|
74
|
+
/** Flattens intersections so hovers and errors show one object, not `A & B & C`. */
|
|
75
|
+
type Simplify<T> = {
|
|
76
|
+
[K in keyof T]: T[K];
|
|
77
|
+
} & {};
|
|
78
|
+
/**
|
|
79
|
+
* Intersects a union. The `[U] extends [never]` guard is load-bearing: the bare
|
|
80
|
+
* idiom maps `never` to `unknown`, which silently leaks into every derived type
|
|
81
|
+
* built on it (`VarsIn<[]>`, `VarsOf<{}, Spec>`, `SpreadFields<[]>`) and violates
|
|
82
|
+
* the "no derived type resolves to unknown" constraint. Empty union -> `{}`.
|
|
83
|
+
*/
|
|
84
|
+
type UnionToIntersection<U> = [U] extends [never] ? {} : (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
85
|
+
/** Strips `null` from a union. Distributes, so `NonNull<A | null>` is `A`. */
|
|
86
|
+
type NonNull<T> = T extends null ? never : T;
|
|
87
|
+
/** Keys of `V` that are not optional. */
|
|
88
|
+
type RequiredKeys<V> = {
|
|
89
|
+
[K in keyof V]-?: {} extends Pick<V, K> ? never : K;
|
|
90
|
+
}[keyof V];
|
|
91
|
+
|
|
92
|
+
type KeyOf<E> = E extends FieldSelection<infer N, unknown, unknown, boolean> ? N : never;
|
|
93
|
+
type ResOf<E> = E extends FieldSelection<string, infer R, unknown, boolean> ? R : never;
|
|
94
|
+
type RequiredSels<S extends readonly SelectionNode[]> = Extract<S[number], FieldSelection<string, unknown, unknown, false>>;
|
|
95
|
+
type OptionalSels<S extends readonly SelectionNode[]> = Extract<S[number], FieldSelection<string, unknown, unknown, true>>;
|
|
96
|
+
type PlainFields<S extends readonly SelectionNode[]> = {
|
|
97
|
+
[E in RequiredSels<S> as KeyOf<E>]: ResOf<E>;
|
|
98
|
+
} & {
|
|
99
|
+
[E in OptionalSels<S> as KeyOf<E>]?: ResOf<E>;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Fragment spreads merge into the parent object.
|
|
103
|
+
* Written in the distributive `S[number] extends infer E` form on purpose:
|
|
104
|
+
* `Extract<S[number], FragmentSpread<unknown, unknown>> extends FragmentSpread<infer R, unknown>` is NOT a
|
|
105
|
+
* naked type parameter, so it collapses the union instead of mapping over it.
|
|
106
|
+
*/
|
|
107
|
+
type SpreadFields<S extends readonly SelectionNode[]> = UnionToIntersection<S[number] extends infer E ? (E extends FragmentSpread<infer R, unknown> ? R : {}) : never>;
|
|
108
|
+
/** The union of inline-fragment branches, or `never` when there are none. */
|
|
109
|
+
type OnBranches<S extends readonly SelectionNode[]> = S[number] extends infer E ? E extends InlineFragment<string, infer R, unknown> ? R : never : never;
|
|
110
|
+
/**
|
|
111
|
+
* Assemble a selection tuple into its result type.
|
|
112
|
+
* With no inline fragments this is just the field map. With inline fragments the
|
|
113
|
+
* common fields are distributed across every branch, producing a discriminated
|
|
114
|
+
* union that narrows on `__typename`.
|
|
115
|
+
*/
|
|
116
|
+
type Selected<S extends readonly SelectionNode[]> = [OnBranches<S>] extends [never] ? Simplify<PlainFields<S> & SpreadFields<S>> : Simplify<PlainFields<S> & SpreadFields<S>> extends infer Common ? OnBranches<S> extends infer B ? B extends unknown ? Simplify<Common & B> : never : never : never;
|
|
117
|
+
/**
|
|
118
|
+
* Union of every variable map contributed anywhere in a selection tuple.
|
|
119
|
+
*/
|
|
120
|
+
type VarsIn<S extends readonly SelectionNode[]> = UnionToIntersection<{
|
|
121
|
+
[I in keyof S]: S[I] extends {
|
|
122
|
+
readonly [VARS]?: infer V;
|
|
123
|
+
} ? (V extends undefined ? {} : V) : {};
|
|
124
|
+
}[number]>;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A placeholder standing in for a GraphQL variable.
|
|
128
|
+
* `N` is only populated by the explicit `v('name')` helper; the `$` proxy
|
|
129
|
+
* produces `VarMarker<string>` because a mapped type over `string` cannot
|
|
130
|
+
* preserve the accessed key.
|
|
131
|
+
*/
|
|
132
|
+
interface VarMarker<N extends string = string> {
|
|
133
|
+
readonly __var: N;
|
|
134
|
+
}
|
|
135
|
+
/** Any argument slot accepts a literal value or a variable placeholder. */
|
|
136
|
+
type Arg<T> = T | VarMarker;
|
|
137
|
+
type ArgsInput<Spec> = {
|
|
138
|
+
[K in keyof Spec]: Arg<Spec[K]>;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Carries a field's argument types at the type level and their GraphQL type
|
|
142
|
+
* strings at runtime (needed to print `$name: String!`).
|
|
143
|
+
*/
|
|
144
|
+
interface ArgSpec<T> {
|
|
145
|
+
readonly gql: Readonly<Record<string, string>>;
|
|
146
|
+
/** Arg keys whose base type is an enum — their literals must print unquoted. */
|
|
147
|
+
readonly enums?: readonly string[];
|
|
148
|
+
readonly __t?: T;
|
|
149
|
+
}
|
|
150
|
+
type IsOpt<Spec, K> = K extends keyof Spec ? ({} extends Pick<Spec, K> ? true : false) : false;
|
|
151
|
+
type VarEntry<N extends string, T, O extends boolean> = O extends true ? {
|
|
152
|
+
[P in N]?: T;
|
|
153
|
+
} : {
|
|
154
|
+
[P in N]: T;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Derive the variable map from the args the user wrote.
|
|
158
|
+
* Name comes from the arg key (or the explicit `v()` name); type comes from the
|
|
159
|
+
* codegen-supplied `Spec`. Optional args yield optional variables.
|
|
160
|
+
*/
|
|
161
|
+
type VarsOf<A, Spec> = UnionToIntersection<{
|
|
162
|
+
[K in keyof A]: A[K] extends VarMarker<infer N> ? string extends N ? VarEntry<K & string, K extends keyof Spec ? Spec[K] : never, IsOpt<Spec, K>> : VarEntry<N, K extends keyof Spec ? Spec[K] : never, IsOpt<Spec, K>> : {};
|
|
163
|
+
}[keyof A]>;
|
|
164
|
+
/**
|
|
165
|
+
* The `$` object handed to operation builders.
|
|
166
|
+
* Deliberately NOT callable: a callable type would resolve `$.name`, `$.length`,
|
|
167
|
+
* `$.call`, `$.apply` and `$.bind` to `Function`'s own members, silently turning
|
|
168
|
+
* those variables into plain strings. Explicit naming lives in `v()` instead.
|
|
169
|
+
*/
|
|
170
|
+
type VarProxy = {
|
|
171
|
+
readonly [K in string]: VarMarker;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* True when the operation declared at least one REQUIRED variable — an all-optional
|
|
175
|
+
* variable map (e.g. `{ after?: string }`) must not force a positional `vars` argument.
|
|
176
|
+
*/
|
|
177
|
+
type HasVars<V> = RequiredKeys<V> extends never ? false : true;
|
|
178
|
+
/**
|
|
179
|
+
* The trailing parameter list carrying an operation's variables, and nothing else.
|
|
180
|
+
* `NoInfer` stops `V` being re-inferred from the argument, which would otherwise let a
|
|
181
|
+
* pre-declared object with missing keys through silently.
|
|
182
|
+
*
|
|
183
|
+
* `client.execute`/`client.subscribe` need a second `ExecuteOptions` slot as well, so they
|
|
184
|
+
* build their own tuple from `HasVars` rather than using this alias — see `create-client.ts`.
|
|
185
|
+
*/
|
|
186
|
+
type VarsArg<V> = HasVars<V> extends true ? [vars: NoInfer<V>] : [vars?: NoInfer<V>];
|
|
187
|
+
|
|
188
|
+
interface Operation<R, V> {
|
|
189
|
+
readonly kind: 'query' | 'mutation' | 'subscription';
|
|
190
|
+
readonly name: string;
|
|
191
|
+
readonly document: string;
|
|
192
|
+
readonly sels: readonly SelectionNode[];
|
|
193
|
+
readonly [RESULT]?: R;
|
|
194
|
+
readonly [VARS]?: V;
|
|
195
|
+
}
|
|
196
|
+
declare const makeQuery: <Root>(root: Root) => <S extends readonly SelectionNode[]>(name: string, build: (v: VarProxy, r: Root) => readonly [...S]) => Operation<Selected<S>, Simplify<VarsIn<S>>>;
|
|
197
|
+
declare const makeMutation: <Root>(root: Root) => <S extends readonly SelectionNode[]>(name: string, build: (v: VarProxy, r: Root) => readonly [...S]) => Operation<Selected<S>, Simplify<VarsIn<S>>>;
|
|
198
|
+
declare const makeSubscription: <Root>(root: Root) => <S extends readonly SelectionNode[]>(name: string, build: (v: VarProxy, r: Root) => readonly [...S]) => Operation<Selected<S>, Simplify<VarsIn<S>>>;
|
|
199
|
+
|
|
200
|
+
export { type ArgSpec as A, type Directive as D, type FieldSelection as F, type HasVars as H, type InlineFragment as I, KEY as K, type NonNull as N, type Operation as O, RESULT as R, type SelectionNode as S, type UnionToIntersection as U, type VarsArg as V, type ArgsInput as a, type VarsOf as b, type Selected as c, type VarsIn as d, type VarMarker as e, type FragmentDefinition as f, VARS as g, type FragmentSpread as h, type Simplify as i, type VarProxy as j, type AnyFieldSelection as k, type Arg as l, type VarRef as m, makeMutation as n, makeQuery as o, makeSubscription as p };
|
package/package.json
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "buildgql",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Type-safe GraphQL query builder generated from introspection",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"graphql",
|
|
9
|
+
"typescript",
|
|
10
|
+
"codegen",
|
|
11
|
+
"query-builder",
|
|
12
|
+
"type-safe"
|
|
13
|
+
],
|
|
14
|
+
"author": "Robert Ozimek",
|
|
15
|
+
"homepage": "https://github.com/robertozimek/buildgql#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/robertozimek/buildgql/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/robertozimek/buildgql.git"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": [
|
|
27
|
+
"./dist/cli/bin.js"
|
|
28
|
+
],
|
|
29
|
+
"bin": {
|
|
30
|
+
"buildgql": "./dist/cli/bin.js"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./client": {
|
|
47
|
+
"import": {
|
|
48
|
+
"types": "./dist/client/index.d.ts",
|
|
49
|
+
"default": "./dist/client/index.js"
|
|
50
|
+
},
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/client/index.d.cts",
|
|
53
|
+
"default": "./dist/client/index.cjs"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"./config": {
|
|
57
|
+
"import": {
|
|
58
|
+
"types": "./dist/cli/config.d.ts",
|
|
59
|
+
"default": "./dist/cli/config.js"
|
|
60
|
+
},
|
|
61
|
+
"require": {
|
|
62
|
+
"types": "./dist/cli/config.d.cts",
|
|
63
|
+
"default": "./dist/cli/config.cjs"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"./adapters/apollo": {
|
|
67
|
+
"import": {
|
|
68
|
+
"types": "./dist/adapters/apollo.d.ts",
|
|
69
|
+
"default": "./dist/adapters/apollo.js"
|
|
70
|
+
},
|
|
71
|
+
"require": {
|
|
72
|
+
"types": "./dist/adapters/apollo.d.cts",
|
|
73
|
+
"default": "./dist/adapters/apollo.cjs"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"./adapters/urql": {
|
|
77
|
+
"import": {
|
|
78
|
+
"types": "./dist/adapters/urql.d.ts",
|
|
79
|
+
"default": "./dist/adapters/urql.js"
|
|
80
|
+
},
|
|
81
|
+
"require": {
|
|
82
|
+
"types": "./dist/adapters/urql.d.cts",
|
|
83
|
+
"default": "./dist/adapters/urql.cjs"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"./package.json": "./package.json"
|
|
87
|
+
},
|
|
88
|
+
"typesVersions": {
|
|
89
|
+
"*": {
|
|
90
|
+
"client": [
|
|
91
|
+
"./dist/client/index.d.ts"
|
|
92
|
+
],
|
|
93
|
+
"config": [
|
|
94
|
+
"./dist/cli/config.d.ts"
|
|
95
|
+
],
|
|
96
|
+
"adapters/apollo": [
|
|
97
|
+
"./dist/adapters/apollo.d.ts"
|
|
98
|
+
],
|
|
99
|
+
"adapters/urql": [
|
|
100
|
+
"./dist/adapters/urql.d.ts"
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"files": [
|
|
105
|
+
"dist"
|
|
106
|
+
],
|
|
107
|
+
"scripts": {
|
|
108
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|
|
109
|
+
"prepack": "npm run build",
|
|
110
|
+
"lint": "eslint . --max-warnings 0",
|
|
111
|
+
"format": "prettier --write .",
|
|
112
|
+
"format:check": "prettier --check .",
|
|
113
|
+
"test": "vitest run",
|
|
114
|
+
"test:types": "tsc -p tsconfig.typetest.json && tsc -p test/types/nuia/tsconfig.json",
|
|
115
|
+
"test:perf": "node scripts/typeperf.mjs",
|
|
116
|
+
"check": "npm run lint && npm run format:check && npm run test:types && npm run build && npm run test && npm run test:perf"
|
|
117
|
+
},
|
|
118
|
+
"peerDependencies": {
|
|
119
|
+
"graphql": ">=16",
|
|
120
|
+
"typescript": ">=5.4"
|
|
121
|
+
},
|
|
122
|
+
"peerDependenciesMeta": {
|
|
123
|
+
"graphql": {
|
|
124
|
+
"optional": true
|
|
125
|
+
},
|
|
126
|
+
"typescript": {
|
|
127
|
+
"optional": true
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
"engines": {
|
|
131
|
+
"node": ">=18"
|
|
132
|
+
},
|
|
133
|
+
"devDependencies": {
|
|
134
|
+
"@eslint/js": "^9.39.5",
|
|
135
|
+
"@graphql-typed-document-node/core": "^3.2.0",
|
|
136
|
+
"@types/node": "^20.19.43",
|
|
137
|
+
"eslint": "^9.39.5",
|
|
138
|
+
"eslint-config-prettier": "^9.1.2",
|
|
139
|
+
"eslint-plugin-unicorn": "^56.0.1",
|
|
140
|
+
"graphql": "^16.14.2",
|
|
141
|
+
"graphql-yoga": "^5.21.2",
|
|
142
|
+
"prettier": "^3.9.6",
|
|
143
|
+
"tsup": "^8.5.1",
|
|
144
|
+
"typescript": "^5.9.3",
|
|
145
|
+
"typescript-eslint": "^8.66.0",
|
|
146
|
+
"vitest": "^2.1.9"
|
|
147
|
+
}
|
|
148
|
+
}
|