From 0e6618f74feda253356c94d5dc1171c5f87da1b8 Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Tue, 24 Dec 2024 14:51:02 -0500 Subject: [PATCH] fix(extension/upload): non-upload requests still work (#1293) --- .github/actions/setup/action.yaml | 2 +- bundle-sizes/config.js | 1 - dprint.json | 3 +- eslint.config.js | 1 + src/extensions/Upload/Upload.test.ts | 48 ++++++++++++++++------------ src/extensions/Upload/Upload.ts | 12 +++---- tests/_/schemas/upload/schema.ts | 6 +++- 7 files changed, 43 insertions(+), 30 deletions(-) diff --git a/.github/actions/setup/action.yaml b/.github/actions/setup/action.yaml index 1eec9e825..4c48d6379 100644 --- a/.github/actions/setup/action.yaml +++ b/.github/actions/setup/action.yaml @@ -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 diff --git a/bundle-sizes/config.js b/bundle-sizes/config.js index 364077631..f1c483018 100644 --- a/bundle-sizes/config.js +++ b/bundle-sizes/config.js @@ -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' diff --git a/dprint.json b/dprint.json index 24c493f7c..11d331f97 100644 --- a/dprint.json +++ b/dprint.json @@ -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", diff --git a/eslint.config.js b/eslint.config.js index 8870ad62a..b1d98becb 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -18,6 +18,7 @@ export default tsEslint.config({ 'legacy/**/*', '**/build/**/*', 'website/**/*', + 'bundle-sizes/**/*.{js,json}', ], extends: configPrisma, languageOptions: { diff --git a/src/extensions/Upload/Upload.test.ts b/src/extensions/Upload/Upload.test.ts index d2056aeaa..ea2f6b527 100644 --- a/src/extensions/Upload/Upload.test.ts +++ b/src/extensions/Upload/Upload.test.ts @@ -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({ + 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) @@ -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 diff --git a/src/extensions/Upload/Upload.ts b/src/extensions/Upload/Upload.ts index fa6963103..ca6bb36bd 100644 --- a/src/extensions/Upload/Upload.ts +++ b/src/extensions/Upload/Upload.ts @@ -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 @@ -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`) { @@ -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`) { @@ -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) } diff --git a/tests/_/schemas/upload/schema.ts b/tests/_/schemas/upload/schema.ts index a764932c9..241e3b65a 100644 --- a/tests/_/schemas/upload/schema.ts +++ b/tests/_/schemas/upload/schema.ts @@ -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 } } }>({}) @@ -12,7 +16,7 @@ builder.scalarType(`Upload`, { builder.queryType({ fields: t => ({ - greetings: t.string({ resolve: () => `Hello World` }), + greetings: t.string({ resolve: () => data.greetings }), }), })