Skip to content

Commit

Permalink
feat(perf): graphql.web for smaller bundle (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Dec 7, 2024
1 parent 8609b84 commit c586f28
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
1 change: 0 additions & 1 deletion examples/55_document-builder/document-builder_directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* This example shows how to use special fields to write GraphQL document directives.
*/

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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"release:pr": "dripip pr"
},
"dependencies": {
"@0no-co/graphql.web": "^1.0.11",
"@graphql-typed-document-node/core": "^3.2.0",
"@molt/command": "^0.9.0",
"es-toolkit": "^1.29.0",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/documentBuilder/SelectGraphQLMapper/nodes/5_Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ export const toGraphQLField: GraphQLPostOperationMapper<
}
}

// Empty array does not work with graphql.web
// @see https://github.com/0no-co/graphql.web/issues/45
const selectionSet = selections.length === 0
? undefined
: Nodes.SelectionSet({
selections,
})

return Nodes.Field({
name: Nodes.Name({
value: field.name,
}),
alias,
arguments: arguments_,
directives,
selectionSet: Nodes.SelectionSet({
selections,
}),
selectionSet,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/documentBuilder/requestMethods/requestMethods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe(`query batch`, () => {

describe(`query root field`, () => {
test(`scalar`, async ({ kitchenSink, kitchenSinkData: db }) => {
await expect(kitchenSink.query.id()).resolves.toEqual(db.id1)
expect(await kitchenSink.query.id()).toEqual(db.id1)
})
test(`argument`, async ({ kitchenSink }) => {
await expect(kitchenSink.query.stringWithArgs({ $: { id: `x` } })).resolves.toEqual(`{"id":"x"}`)
Expand Down
10 changes: 9 additions & 1 deletion src/extensions/SchemaErrors/injectTypenameOnRootResultFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ const injectTypenameOnRootResultFields_ = (
switch (selection.kind) {
case Nodes.Kind.FIELD: {
const isResultField = Boolean(sddm.operations[operationType]?.f[selection.name.value]?.r)

if (isResultField) {
if (selection.selectionSet === undefined) {
// @ts-expect-error selections is typed as readonly
// @see https://github.com/graphql/graphql-js/discussions/4212
selection.selectionSet = Nodes.SelectionSet({
selections: [],
})
}
// @ts-expect-error selections is typed as readonly
// @see https://github.com/graphql/graphql-js/discussions/4212
selection.selectionSet?.selections.push(Nodes.Field({ name: Nodes.Name({ value: `__typename` }) }))
selection.selectionSet.selections.push(Nodes.Field({ name: Nodes.Name({ value: `__typename` }) }))
}
continue
}
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/SchemaErrors/tests/SchemaErrors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from 'graphql'
import { parse } from '@0no-co/graphql.web'
import { describe, expect, test } from 'vitest'
import { db } from '../../../../tests/_/schemas/db.js'
import { schema } from '../../../../tests/_/schemas/kitchen-sink/schema.js'
Expand Down Expand Up @@ -27,7 +27,7 @@ describe(`document`, () => {
})
test(`__typename is dynamically added at runtime if missing`, async () => {
const result = (await graffle
.document({ query: { x: { resultNonNull: { $: { $case: `ErrorOne` } } } } })
.document({ query: { x: { resultNonNull: { $: { $case: `ErrorOne` }, ___on_Object1: { id: true } } } } })
.run()) as Errors.ContextualAggregateError
expect(result.errors[0]).toMatchInlineSnapshot(`[Error: Failure on field resultNonNull: ErrorOne]`)
})
Expand Down
5 changes: 5 additions & 0 deletions src/lib/dump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { inspect } from 'node:util'

export const dump = (value: unknown) => {
console.log(inspect(value, { depth: null }))
}
5 changes: 2 additions & 3 deletions src/lib/grafaid/document.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse, print as graphqlWebPrint } from '@0no-co/graphql.web'
import {
type ArgumentNode,
type BooleanValueNode,
Expand All @@ -16,8 +17,6 @@ import {
type ObjectFieldNode,
type ObjectValueNode,
type OperationDefinitionNode,
parse,
print as graphqlPrint,
type SelectionSetNode,
type StringValueNode,
type TypeNode,
Expand Down Expand Up @@ -270,7 +269,7 @@ export const OperationTypeToAccessKind = {

export const print = (document: TypedDocument.TypedDocumentLike): string => {
const documentUntyped = TypedDocument.unType(document)
return isString(documentUntyped) ? documentUntyped : graphqlPrint(documentUntyped)
return isString(documentUntyped) ? documentUntyped : graphqlWebPrint(documentUntyped)
}

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

0 comments on commit c586f28

Please sign in to comment.