@gleanql/codegen 0.1.1 → 0.1.2
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.mjs +10 -3
- package/package.json +2 -2
- package/src/graph.ts +7 -1
- package/src/types.ts +8 -2
package/dist/index.mjs
CHANGED
|
@@ -168,12 +168,18 @@ function renderInputObject(type, ctx) {
|
|
|
168
168
|
const members = (type.inputFields ?? []).map((f) => " " + renderInputMember(f, ctx));
|
|
169
169
|
return `export interface ${type.name} {\n${members.join("\n")}\n}`;
|
|
170
170
|
}
|
|
171
|
-
/**
|
|
171
|
+
/**
|
|
172
|
+
* A field is callable when it declares arguments; otherwise it is a property.
|
|
173
|
+
* (The runtime proxy makes the same split — args-fields return a function.)
|
|
174
|
+
* The args object itself is optional when every argument is, so the common
|
|
175
|
+
* no-args call reads `image.url()` rather than `image.url({})`.
|
|
176
|
+
*/
|
|
172
177
|
function renderFieldMember(name, args, type, ctx) {
|
|
173
178
|
const ret = renderTs(type, ctx.scalars);
|
|
174
179
|
if (args.length > 0) {
|
|
175
180
|
const argList = args.map((a) => renderInputMember(a, ctx)).join(" ");
|
|
176
|
-
|
|
181
|
+
const allOptional = args.every((a) => a.type.kind !== "NON_NULL");
|
|
182
|
+
return `${propKey(name)}(args${allOptional ? "?" : ""}: { ${argList} }): ${ret};`;
|
|
177
183
|
}
|
|
178
184
|
return `${propKey(name)}: ${ret};`;
|
|
179
185
|
}
|
|
@@ -218,7 +224,8 @@ function generateGraph(schema, options = {}) {
|
|
|
218
224
|
}
|
|
219
225
|
function renderRoot(field, scalars) {
|
|
220
226
|
const ret = renderTs(field.type, scalars);
|
|
221
|
-
const
|
|
227
|
+
const allOptional = field.args.every((a) => a.type.kind !== "NON_NULL");
|
|
228
|
+
const params = field.args.length > 0 ? `args${allOptional ? "?" : ""}: { ${field.args.map((a) => renderArg(a.name, a.type, scalars)).join(" ")} }` : "";
|
|
222
229
|
return [
|
|
223
230
|
`${propKey(field.name)}(${params}): ${ret} {`,
|
|
224
231
|
` return undefined as unknown as ${ret};`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gleanql/codegen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Glean's schema codegen: introspection to schema model, branded types and graph accessors",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexander Liljengard",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@gleanql/core": "0.1.
|
|
29
|
+
"@gleanql/core": "0.1.2"
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://gleanql.com",
|
|
32
32
|
"bugs": "https://github.com/gleanql/gleanql/issues",
|
package/src/graph.ts
CHANGED
|
@@ -64,7 +64,13 @@ export function generateGraph(schema: IntrospectionSchema, options: GenerateGrap
|
|
|
64
64
|
|
|
65
65
|
function renderRoot(field: IntrospectionField, scalars: Record<string, string>): string {
|
|
66
66
|
const ret = renderTs(field.type, scalars);
|
|
67
|
-
|
|
67
|
+
// The args object is optional when every argument is — `glean.productsCount()`
|
|
68
|
+
// should not demand an empty `{}`.
|
|
69
|
+
const allOptional = field.args.every((a) => a.type.kind !== "NON_NULL");
|
|
70
|
+
const params =
|
|
71
|
+
field.args.length > 0
|
|
72
|
+
? `args${allOptional ? "?" : ""}: { ${field.args.map((a) => renderArg(a.name, a.type, scalars)).join(" ")} }`
|
|
73
|
+
: "";
|
|
68
74
|
return [
|
|
69
75
|
`${propKey(field.name)}(${params}): ${ret} {`,
|
|
70
76
|
` return undefined as unknown as ${ret};`,
|
package/src/types.ts
CHANGED
|
@@ -93,7 +93,12 @@ function renderInputObject(type: IntrospectionType, ctx: Ctx): string {
|
|
|
93
93
|
return `export interface ${type.name} {\n${members.join("\n")}\n}`;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* A field is callable when it declares arguments; otherwise it is a property.
|
|
98
|
+
* (The runtime proxy makes the same split — args-fields return a function.)
|
|
99
|
+
* The args object itself is optional when every argument is, so the common
|
|
100
|
+
* no-args call reads `image.url()` rather than `image.url({})`.
|
|
101
|
+
*/
|
|
97
102
|
function renderFieldMember(
|
|
98
103
|
name: string,
|
|
99
104
|
args: readonly IntrospectionInputValue[],
|
|
@@ -103,7 +108,8 @@ function renderFieldMember(
|
|
|
103
108
|
const ret = renderTs(type, ctx.scalars);
|
|
104
109
|
if (args.length > 0) {
|
|
105
110
|
const argList = args.map((a) => renderInputMember(a, ctx)).join(" ");
|
|
106
|
-
|
|
111
|
+
const allOptional = args.every((a) => a.type.kind !== "NON_NULL");
|
|
112
|
+
return `${propKey(name)}(args${allOptional ? "?" : ""}: { ${argList} }): ${ret};`;
|
|
107
113
|
}
|
|
108
114
|
return `${propKey(name)}: ${ret};`;
|
|
109
115
|
}
|