Skip to content

Commit 02a5522

Browse files
authored
feat!: remove file uploads feature (#501)
1 parent 5ce990a commit 02a5522

File tree

6 files changed

+79
-202
lines changed

6 files changed

+79
-202
lines changed

README.md

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ Minimal GraphQL client supporting Node and browsers for scripts or simple apps
2828
- [Cookie support for `node`](#cookie-support-for-node)
2929
- [Using a custom `fetch` method](#using-a-custom-fetch-method)
3030
- [Receiving a raw response](#receiving-a-raw-response)
31-
- [File Upload](#file-upload)
32-
- [Browser](#browser)
33-
- [Node](#node)
3431
- [Batching](#batching)
3532
- [Cancellation](#cancellation)
3633
- [Middleware](#middleware)
@@ -39,6 +36,7 @@ Minimal GraphQL client supporting Node and browsers for scripts or simple apps
3936
- [Ignore](#ignore)
4037
- [All](#all)
4138
- [Knowledge Base](#knowledge-base)
39+
- [Why was the file upload feature taken away? Will it return?](#why-was-the-file-upload-feature-taken-away-will-it-return)
4240
- [Why do I have to install `graphql`?](#why-do-i-have-to-install-graphql)
4341
- [Do I need to wrap my GraphQL documents inside the `gql` template exported by `graphql-request`?](#do-i-need-to-wrap-my-graphql-documents-inside-the-gql-template-exported-by-graphql-request)
4442
- [What's the difference between `graphql-request`, Apollo and Relay?](#whats-the-difference-between-graphql-request-apollo-and-relay)
@@ -565,45 +563,6 @@ async function main() {
565563
main().catch((error) => console.error(error))
566564
```
567565

568-
### File Upload
569-
570-
#### Browser
571-
572-
```js
573-
import { request } from 'graphql-request'
574-
575-
const UploadUserAvatar = gql`
576-
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
577-
updateUser(id: $userId, input: { avatar: $file })
578-
}
579-
`
580-
581-
request('/api/graphql', UploadUserAvatar, {
582-
userId: 1,
583-
file: document.querySelector('input#avatar').files[0],
584-
})
585-
```
586-
587-
#### Node
588-
589-
```js
590-
import { createReadStream } from 'fs'
591-
import { request } from 'graphql-request'
592-
593-
const UploadUserAvatar = gql`
594-
mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
595-
updateUser(id: $userId, input: { avatar: $file })
596-
}
597-
`
598-
599-
request('/api/graphql', UploadUserAvatar, {
600-
userId: 1,
601-
file: createReadStream('./avatar.img'),
602-
})
603-
```
604-
605-
[TypeScript Source](examples/receiving-a-raw-response.ts)
606-
607566
### Batching
608567

609568
It is possible with `graphql-request` to use [batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md) via the `batchRequests()` function. Example available at [examples/batching-requests.ts](examples/batching-requests.ts)
@@ -745,6 +704,10 @@ Return both the errors and data, only works with `rawRequest`.
745704

746705
## Knowledge Base
747706

707+
#### Why was the file upload feature taken away? Will it return?
708+
709+
In [this issue](https://github.com/jasonkuhrt/graphql-request/issues/500) we decided to make this library more stable and maintainable. In principal the feature is still in scope of this library and will make a return when we find time to do the feature right.
710+
748711
#### Why do I have to install `graphql`?
749712

750713
`graphql-request` uses methods exposed by the `graphql` package to handle some internal logic. On top of that, for TypeScript users, some types are used from the `graphql` package to provide better typings.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@
6262
},
6363
"dependencies": {
6464
"@graphql-typed-document-node/core": "^3.2.0",
65-
"cross-fetch": "^3.1.5",
66-
"extract-files": "^9.0.0",
67-
"form-data": "^3.0.0"
65+
"cross-fetch": "^3.1.5"
6866
},
6967
"peerDependencies": {
7068
"graphql": "14 - 16"
@@ -75,7 +73,6 @@
7573
"@tsconfig/node16": "^1.0.3",
7674
"@types/body-parser": "^1.19.2",
7775
"@types/express": "^4.17.17",
78-
"@types/extract-files": "^8.1.1",
7976
"@types/node": "^18.15.11",
8077
"@types/ws": "^8.5.4",
8178
"@typescript-eslint/eslint-plugin": "^5.57.1",

pnpm-lock.yaml

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

src/createRequestBody.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import createRequestBody from './createRequestBody.js'
21
import { defaultJsonSerializer } from './defaultJsonSerializer.js'
32
import { HeadersInstanceToPlainObject, uppercase } from './helpers.js'
43
import {
@@ -597,7 +596,32 @@ const parseBatchRequestsArgsExtended = (args: BatchRequestsArgs): BatchRequestsE
597596
}
598597
}
599598

600-
export default request
599+
const createRequestBody = (
600+
query: string | string[],
601+
variables?: Variables | Variables[],
602+
operationName?: string,
603+
jsonSerializer?: JsonSerializer
604+
): string => {
605+
const jsonSerializer_ = jsonSerializer ?? defaultJsonSerializer
606+
if (!Array.isArray(query)) {
607+
return jsonSerializer_.stringify({ query, variables, operationName })
608+
}
609+
610+
if (typeof variables !== `undefined` && !Array.isArray(variables)) {
611+
throw new Error(`Cannot create request body with given variable type, array expected`)
612+
}
613+
614+
// Batch support
615+
const payload = query.reduce<{ query: string; variables: Variables | undefined }[]>(
616+
(acc, currentQuery, index) => {
617+
acc.push({ query: currentQuery, variables: variables ? variables[index] : undefined })
618+
return acc
619+
},
620+
[]
621+
)
622+
623+
return jsonSerializer_.stringify(payload)
624+
}
601625

602626
const getResult = async (
603627
response: Response,
@@ -655,3 +679,4 @@ export const gql = (chunks: TemplateStringsArray, ...variables: unknown[]): stri
655679

656680
export { GraphQLWebSocketClient } from './graphql-ws.js'
657681
export { resolveRequestDocument } from './resolveRequestDocument.js'
682+
export default request

0 commit comments

Comments
 (0)