1.3.0 • Published 3 months ago

genql-upload v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

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.

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.chain.mutation
    .singleUpload({
      file: file1, // file2
    })
    .get({
      filename: true,
      mimetype: true,
      headers: true,
    });
}

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 :)

1.3.0

3 months ago

1.2.0

2 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago