Skip to content

Commit c586f28

Browse files
authored
feat(perf): graphql.web for smaller bundle (#1277)
1 parent 8609b84 commit c586f28

File tree

9 files changed

+43
-11
lines changed

9 files changed

+43
-11
lines changed

examples/55_document-builder/document-builder_directive.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* This example shows how to use special fields to write GraphQL document directives.
33
*/
44

5-
// import { parse, print } from 'graphql'
65
import { Graffle } from '../../tests/_/schemas/pokemon/graffle/__.js'
76
import { showJson } from '../$/helpers.js'
87

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"release:pr": "dripip pr"
120120
},
121121
"dependencies": {
122+
"@0no-co/graphql.web": "^1.0.11",
122123
"@graphql-typed-document-node/core": "^3.2.0",
123124
"@molt/command": "^0.9.0",
124125
"es-toolkit": "^1.29.0",

pnpm-lock.yaml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/documentBuilder/SelectGraphQLMapper/nodes/5_Field.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,22 @@ export const toGraphQLField: GraphQLPostOperationMapper<
6767
}
6868
}
6969

70+
// Empty array does not work with graphql.web
71+
// @see https://github.com/0no-co/graphql.web/issues/45
72+
const selectionSet = selections.length === 0
73+
? undefined
74+
: Nodes.SelectionSet({
75+
selections,
76+
})
77+
7078
return Nodes.Field({
7179
name: Nodes.Name({
7280
value: field.name,
7381
}),
7482
alias,
7583
arguments: arguments_,
7684
directives,
77-
selectionSet: Nodes.SelectionSet({
78-
selections,
79-
}),
85+
selectionSet,
8086
})
8187
}
8288

src/documentBuilder/requestMethods/requestMethods.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe(`query batch`, () => {
1313

1414
describe(`query root field`, () => {
1515
test(`scalar`, async ({ kitchenSink, kitchenSinkData: db }) => {
16-
await expect(kitchenSink.query.id()).resolves.toEqual(db.id1)
16+
expect(await kitchenSink.query.id()).toEqual(db.id1)
1717
})
1818
test(`argument`, async ({ kitchenSink }) => {
1919
await expect(kitchenSink.query.stringWithArgs({ $: { id: `x` } })).resolves.toEqual(`{"id":"x"}`)

src/extensions/SchemaErrors/injectTypenameOnRootResultFields.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ const injectTypenameOnRootResultFields_ = (
2626
switch (selection.kind) {
2727
case Nodes.Kind.FIELD: {
2828
const isResultField = Boolean(sddm.operations[operationType]?.f[selection.name.value]?.r)
29+
2930
if (isResultField) {
31+
if (selection.selectionSet === undefined) {
32+
// @ts-expect-error selections is typed as readonly
33+
// @see https://github.com/graphql/graphql-js/discussions/4212
34+
selection.selectionSet = Nodes.SelectionSet({
35+
selections: [],
36+
})
37+
}
3038
// @ts-expect-error selections is typed as readonly
3139
// @see https://github.com/graphql/graphql-js/discussions/4212
32-
selection.selectionSet?.selections.push(Nodes.Field({ name: Nodes.Name({ value: `__typename` }) }))
40+
selection.selectionSet.selections.push(Nodes.Field({ name: Nodes.Name({ value: `__typename` }) }))
3341
}
3442
continue
3543
}

src/extensions/SchemaErrors/tests/SchemaErrors.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse } from 'graphql'
1+
import { parse } from '@0no-co/graphql.web'
22
import { describe, expect, test } from 'vitest'
33
import { db } from '../../../../tests/_/schemas/db.js'
44
import { schema } from '../../../../tests/_/schemas/kitchen-sink/schema.js'
@@ -27,7 +27,7 @@ describe(`document`, () => {
2727
})
2828
test(`__typename is dynamically added at runtime if missing`, async () => {
2929
const result = (await graffle
30-
.document({ query: { x: { resultNonNull: { $: { $case: `ErrorOne` } } } } })
30+
.document({ query: { x: { resultNonNull: { $: { $case: `ErrorOne` }, ___on_Object1: { id: true } } } } })
3131
.run()) as Errors.ContextualAggregateError
3232
expect(result.errors[0]).toMatchInlineSnapshot(`[Error: Failure on field resultNonNull: ErrorOne]`)
3333
})

src/lib/dump.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { inspect } from 'node:util'
2+
3+
export const dump = (value: unknown) => {
4+
console.log(inspect(value, { depth: null }))
5+
}

src/lib/grafaid/document.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parse, print as graphqlWebPrint } from '@0no-co/graphql.web'
12
import {
23
type ArgumentNode,
34
type BooleanValueNode,
@@ -16,8 +17,6 @@ import {
1617
type ObjectFieldNode,
1718
type ObjectValueNode,
1819
type OperationDefinitionNode,
19-
parse,
20-
print as graphqlPrint,
2120
type SelectionSetNode,
2221
type StringValueNode,
2322
type TypeNode,
@@ -270,7 +269,7 @@ export const OperationTypeToAccessKind = {
270269

271270
export const print = (document: TypedDocument.TypedDocumentLike): string => {
272271
const documentUntyped = TypedDocument.unType(document)
273-
return isString(documentUntyped) ? documentUntyped : graphqlPrint(documentUntyped)
272+
return isString(documentUntyped) ? documentUntyped : graphqlWebPrint(documentUntyped)
274273
}
275274

276275
export const getNamedType = (type: TypeNode): NamedTypeNode => {

0 commit comments

Comments
 (0)