Skip to content

Commit

Permalink
fix(extension/upload): non-upload requests still work (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Dec 24, 2024
1 parent 3a17b33 commit 0e6618f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ runs:
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: '9.12.2'
version: '9.15.1'
run_install: false
# Cannot use corepack because it doesn't permit global package installs.
# @see https://github.com/pnpm/action-setup/issues/105#issuecomment-2468689290
Expand Down
1 change: 0 additions & 1 deletion bundle-sizes/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable */
import { nodeResolve } from '@rollup/plugin-node-resolve'
import terser from '@rollup/plugin-terser'
import typescript from '@rollup/plugin-typescript'
Expand Down
3 changes: 2 additions & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"**/*-lock.json",
"website/pokemon",
"website/graffle",
"website/.vitepress/dist"
"website/.vitepress/dist",
"bundle-sizes/**/*.{js,json}"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.93.0.wasm",
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default tsEslint.config({
'legacy/**/*',
'**/build/**/*',
'website/**/*',
'bundle-sizes/**/*.{js,json}',
],
extends: configPrisma,
languageOptions: {
Expand Down
48 changes: 28 additions & 20 deletions src/extensions/Upload/Upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
// todo in order to test jsdom, we need to boot the server in a separate process
// @vitest-environment node

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

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

let schemaServer: SchemaService

let graffle: GraffleMinimal.Client

beforeAll(async () => {
schemaServer = await serveSchema({ schema })
})

beforeEach(() => {
const graffle_ = Graffle.create().transport({ url: schemaServer.url }).use(Upload())
graffle = graffle_ as any
})

afterAll(async () => {
await schemaServer.stop()
interface Context {
graffle: GraffleMinimal.Client.With<{ checkPreflight: false }>
schemaServer: SchemaService
}

const test = testBase.extend<Context>({
schemaServer: async ({}, use) => { // eslint-disable-line
const schemaServer = await serveSchema({ schema: UploadSchema.schema })
await use(schemaServer)
await schemaServer.stop()
},
graffle: async ({ schemaServer }, use) => {
const graffle = Graffle.create({ checkPreflight: false }).transport({ url: schemaServer.url }).use(Upload())
await use(graffle as any)
},
})

test(`upload`, async () => {
// @ts-expect-error fixme
test(`upload`, async ({ graffle }) => {
const data = await graffle.gql`
mutation ($blob: Upload!) {
readTextFile(blob: $blob)
Expand All @@ -42,7 +41,16 @@ test(`upload`, async () => {
`)
})

// todo test that non-upload requests work
test(`client with upload extension making non-upload request`, async ({ graffle }) => {
const data = await graffle.gql`
query {
greetings
}
`.send()
expect(data).toEqual({
greetings: UploadSchema.data.greetings,
})
})

// todo test with non-raw
// ^ for this to work we need to generate documents that use variables
12 changes: 6 additions & 6 deletions src/extensions/Upload/Upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Extension } from '../../entrypoints/extensionkit.js'
import type { Variables } from '../../lib/grafaid/graphql.js'
import type { RequestAnalyzedInput } from '../../lib/grafaid/graphql.js'
import { createBody } from './createBody.js'

// todo
Expand All @@ -13,6 +13,8 @@ export const Upload = Extension.create({
create() {
return {
async onRequest({ pack }) {
if (!isUploadRequest(pack.input.request)) return pack()

// TODO we can probably get file upload working for in-memory schemas too :)
// @ts-expect-error fixme
if (pack.input.transportType !== `http`) {
Expand All @@ -27,9 +29,6 @@ export const Upload = Extension.create({
using: {
// @ts-expect-error fixme
body: (input) => {
const hasUploadScalarVariable = input.variables && isUsingUploadScalar(input.variables)
if (!hasUploadScalarVariable) return

// TODO we can probably get file upload working for in-memory schemas too :)
// @ts-expect-error fixme
if (pack.input.transportType !== `http`) {
Expand All @@ -55,6 +54,7 @@ export const Upload = Extension.create({
},
})

const isUsingUploadScalar = (_variables: Variables) => {
return Object.values(_variables).some(_ => _ instanceof Blob)
const isUploadRequest = (request: RequestAnalyzedInput) => {
if (!request.variables) return false
return Object.values(request.variables).some(_ => _ instanceof Blob)
}
6 changes: 5 additions & 1 deletion tests/_/schemas/upload/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import SchemaBuilder from '@pothos/core'

export const data = {
greetings: `Hello World`,
}

const builder = new SchemaBuilder<{
Scalars: { Upload: { Input: Blob; Output: never } }
}>({})
Expand All @@ -12,7 +16,7 @@ builder.scalarType(`Upload`, {

builder.queryType({
fields: t => ({
greetings: t.string({ resolve: () => `Hello World` }),
greetings: t.string({ resolve: () => data.greetings }),
}),
})

Expand Down

0 comments on commit 0e6618f

Please sign in to comment.