Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 2.11 KB

File metadata and controls

81 lines (58 loc) · 2.11 KB

npm codecov

Graphql Upload support for genql

Custom fetcher for Genql graphql client that supports Graphql Multipart Request for file uploads in Graphql.

Install

npm install genql-upload

Usage

First 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.

Running locally

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

Thats it ...

... happy coding :)