Détail du package

genql-upload

stackables152MIT1.3.0

Graphql Upload support for genql

genql, graphql, upload, stackables

readme

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