Custom fetcher for Genql graphql client that supports Graphql Multipart Request for file uploads in Graphql.
npm install genql-uploadFirst generate your typed client and connect a custom fetcher as shown below.
# Given this schema
scalar Upload
type Mutation {
singleUpload(file: Upload!): String
}import { createClient } from "./generated_dir";
import { createFetcher } from "genql-upload";
const client = createClient({
fetcher: createFetcher({
url: "http://localhost:4000/graphql",
headers: {
// ...
},
}),
});In order to use the library in nodejs you need to use a custom FileUpload class to wrap any readable stream.
Example is assuming a demo server as described at https://www.apollographql.com/docs/apollo-server/data/file-uploads/
import { FileUpload } from "genql-upload";
import fs from "fs";
async function upload() {
// read stream is valid file input
const file1 = new FileUpload(fs.createReadStream("./README.md"));
// but file can also be Buffer
const file2 = new FileUpload(Buffer.from(/* ... */), "filename.txt");
const response = await client.mutation({
singleUpload: {
__args: {
file: file1 // file2
}
},
});
}See the basic test for full usage including the server setup.
Generate the test sdk by running genql --schema ./test/schema.graphql --output ./test/generated or use the npm script npm run test:generate.
npm install
npm run test:generate
npm test... happy coding :)