Skip to content

Commit 0e6618f

Browse files
authored
fix(extension/upload): non-upload requests still work (#1293)
1 parent 3a17b33 commit 0e6618f

File tree

7 files changed

+43
-30
lines changed

7 files changed

+43
-30
lines changed

.github/actions/setup/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ runs:
1919
- name: Install pnpm
2020
uses: pnpm/action-setup@v4
2121
with:
22-
version: '9.12.2'
22+
version: '9.15.1'
2323
run_install: false
2424
# Cannot use corepack because it doesn't permit global package installs.
2525
# @see https://github.com/pnpm/action-setup/issues/105#issuecomment-2468689290

bundle-sizes/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable */
21
import { nodeResolve } from '@rollup/plugin-node-resolve'
32
import terser from '@rollup/plugin-terser'
43
import typescript from '@rollup/plugin-typescript'

dprint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"**/*-lock.json",
1313
"website/pokemon",
1414
"website/graffle",
15-
"website/.vitepress/dist"
15+
"website/.vitepress/dist",
16+
"bundle-sizes/**/*.{js,json}"
1617
],
1718
"plugins": [
1819
"https://plugins.dprint.dev/typescript-0.93.0.wasm",

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default tsEslint.config({
1818
'legacy/**/*',
1919
'**/build/**/*',
2020
'website/**/*',
21+
'bundle-sizes/**/*.{js,json}',
2122
],
2223
extends: configPrisma,
2324
languageOptions: {

src/extensions/Upload/Upload.test.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
// todo in order to test jsdom, we need to boot the server in a separate process
22
// @vitest-environment node
33

4-
import { afterAll, beforeAll, beforeEach, expect, test } from 'vitest'
5-
import { schema } from '../../../tests/_/schemas/upload/schema.js'
4+
import { expect, test as testBase } from 'vitest'
5+
import * as UploadSchema from '../../../tests/_/schemas/upload/schema.js'
66
import { Graffle } from '../../entrypoints/main.js'
77
import { Upload } from './Upload.js'
88

99
import { type SchemaService, serveSchema } from '../../../tests/_/lib/serveSchema.js'
1010
import type { GraffleMinimal } from '../../entrypoints/presets/minimal.js'
1111

12-
let schemaServer: SchemaService
13-
14-
let graffle: GraffleMinimal.Client
15-
16-
beforeAll(async () => {
17-
schemaServer = await serveSchema({ schema })
18-
})
19-
20-
beforeEach(() => {
21-
const graffle_ = Graffle.create().transport({ url: schemaServer.url }).use(Upload())
22-
graffle = graffle_ as any
23-
})
24-
25-
afterAll(async () => {
26-
await schemaServer.stop()
12+
interface Context {
13+
graffle: GraffleMinimal.Client.With<{ checkPreflight: false }>
14+
schemaServer: SchemaService
15+
}
16+
17+
const test = testBase.extend<Context>({
18+
schemaServer: async ({}, use) => { // eslint-disable-line
19+
const schemaServer = await serveSchema({ schema: UploadSchema.schema })
20+
await use(schemaServer)
21+
await schemaServer.stop()
22+
},
23+
graffle: async ({ schemaServer }, use) => {
24+
const graffle = Graffle.create({ checkPreflight: false }).transport({ url: schemaServer.url }).use(Upload())
25+
await use(graffle as any)
26+
},
2727
})
2828

29-
test(`upload`, async () => {
30-
// @ts-expect-error fixme
29+
test(`upload`, async ({ graffle }) => {
3130
const data = await graffle.gql`
3231
mutation ($blob: Upload!) {
3332
readTextFile(blob: $blob)
@@ -42,7 +41,16 @@ test(`upload`, async () => {
4241
`)
4342
})
4443

45-
// todo test that non-upload requests work
44+
test(`client with upload extension making non-upload request`, async ({ graffle }) => {
45+
const data = await graffle.gql`
46+
query {
47+
greetings
48+
}
49+
`.send()
50+
expect(data).toEqual({
51+
greetings: UploadSchema.data.greetings,
52+
})
53+
})
4654

4755
// todo test with non-raw
4856
// ^ for this to work we need to generate documents that use variables

src/extensions/Upload/Upload.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Extension } from '../../entrypoints/extensionkit.js'
2-
import type { Variables } from '../../lib/grafaid/graphql.js'
2+
import type { RequestAnalyzedInput } from '../../lib/grafaid/graphql.js'
33
import { createBody } from './createBody.js'
44

55
// todo
@@ -13,6 +13,8 @@ export const Upload = Extension.create({
1313
create() {
1414
return {
1515
async onRequest({ pack }) {
16+
if (!isUploadRequest(pack.input.request)) return pack()
17+
1618
// TODO we can probably get file upload working for in-memory schemas too :)
1719
// @ts-expect-error fixme
1820
if (pack.input.transportType !== `http`) {
@@ -27,9 +29,6 @@ export const Upload = Extension.create({
2729
using: {
2830
// @ts-expect-error fixme
2931
body: (input) => {
30-
const hasUploadScalarVariable = input.variables && isUsingUploadScalar(input.variables)
31-
if (!hasUploadScalarVariable) return
32-
3332
// TODO we can probably get file upload working for in-memory schemas too :)
3433
// @ts-expect-error fixme
3534
if (pack.input.transportType !== `http`) {
@@ -55,6 +54,7 @@ export const Upload = Extension.create({
5554
},
5655
})
5756

58-
const isUsingUploadScalar = (_variables: Variables) => {
59-
return Object.values(_variables).some(_ => _ instanceof Blob)
57+
const isUploadRequest = (request: RequestAnalyzedInput) => {
58+
if (!request.variables) return false
59+
return Object.values(request.variables).some(_ => _ instanceof Blob)
6060
}

tests/_/schemas/upload/schema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import SchemaBuilder from '@pothos/core'
22

3+
export const data = {
4+
greetings: `Hello World`,
5+
}
6+
37
const builder = new SchemaBuilder<{
48
Scalars: { Upload: { Input: Blob; Output: never } }
59
}>({})
@@ -12,7 +16,7 @@ builder.scalarType(`Upload`, {
1216

1317
builder.queryType({
1418
fields: t => ({
15-
greetings: t.string({ resolve: () => `Hello World` }),
19+
greetings: t.string({ resolve: () => data.greetings }),
1620
}),
1721
})
1822

0 commit comments

Comments
 (0)