1.0.5 • Published 4 years ago
gql-bin v1.0.5
gql-bin
send and receive binary data in graphql
to be clear this package does not send or receive bin data via graphql it self but it gives you a way to turn around it
NOTE: YOU MUST include bodyParser.raw() for this module to work properly
Usage
we will start this this server:
import express = require("express");
import bodyParser from "body-parser";
import { gql, ApolloServer } from "apollo-server-express";
import { BuffGql, BufferInput, applyToExpressApp } from "gql-bin";
const typeDefs = gql`
# defining the Buffer type
${BuffGql}
# defining the BufferInput type
${BufferInput}
type Book {
# ...
text: Buffer!
# ...
}
input BookInput {
# ...
text: BufferInput
# ...
}
type Query {
book(id: ID): Book
}
type Mutation {
book(id: ID, newBook: BookInput): Book
}
`;
export const resolvers = {
Book: {
text() {
// code will add later in the readme
},
},
Query: {
book(_, args) {
// ...
},
},
Mutation: {
book(_, args) {
// code will add later in the readme
},
},
};
const apolloServer = new ApolloServer({ typeDefs, resolvers });
const app = express();
// NOTE: YOU MUST include bodyParser.raw()
app.use(bodyParser.raw());
// applies the gql-bin middle ware to the express app
// the second arg is the route for the middle ware
// to send and receive data
applyToExpressApp(app, "/graphql/storage");
app.use(apolloServer.getMiddleware({ path: "/graphql" }));
app.listen(4000);
receive Data
to receive bin data you first to convert the data in buffer to Buff in the server and send it
then you need to convert the Buff Objects you got to clientBuff
so you could call .request() on them an get your data
in your server:
import { toBuffObject } from "gql-bin";
// in resolvers
const resolvers = {
// ...
Book: {
// (Book and text are just resolvers for the property text)
text() {
// you just call toBuffObject to covert a normal buffer
// to a Buff Object
return toBuffObject(Buffer.from("hello world"));
},
},
// ...
};
in your client
import fetchBase = require('graphql-fetch');
import { BufferFragment, setEndPoint, addRequest } from "gql-bin";
// setting the endPoint to send and recive data
// (this is the default)
setEndPoint("/graphql/storage");
const fetch = fetchBase('/graphql');
const query = `
# defining the BufferFields fragment
${BufferFragment}
query q (id: String!) {
book(id: $id) {
text { ...BufferFields }
}
}
`
(async () => {
// addRequest gets all buff object and convert them to bufferClient
const data = addRequest((await fetch(query, { id: 'abcdef' }, {})).data);
const text = await data.text.request();
console.log(text); // => "hello world"
})()
sending data
to send you first need to convert your data to uploadBuff
then in your server you need to convert the uploadBuff to
a handler and wait for the data while it's being uploaded
in client
import fetchBase = require('graphql-fetch');
import { BufferFragment, setEndPoint, addRequest, createUploadVars } from "gql-bin";
// setting the endPoint to send and recive data
// (this is the default)
setEndPoint("/graphql/storage");
const fetch = fetchBase('/graphql');
const query = `
# defining the BufferFields fragment
${BufferFragment}
mutation q (id: String!, newBook: BookInput!) {
book(id: $id, newBook: $newBook) {
# ...
}
}
`
(async () => {
// createUploadVars converts Buffers Blobs and arrayBuffers
const vars = createUploadVars({
id: 'abcdef',
newBook: {
// this will convert the data to uploadBuff behind the scenes
text: new Blob(["hello world"])
}
});
await fetch(
query,
vars.obj
{}
);
// uploading binary data
await vars.upload();
})()
in server
const resolvers = {
Mutation: {
async book(_, args) {
// one way
if (args.newBook.text) {
// convert uploadBuff to a handler
const handler = toUploadHandler(args.newBook.text);
// waits until the data full upload
console.log((await handler.fullLoad()).toString());
// or you could
handler.on("data", console.log);
handler.on("close", () => console.log("data uploaded"));
}
// second way
// cnToUploadHandlers convert all uploadBuff to handlers on
// an object
args = cnToUploadHandlers(args);
const { text } = args.newBook;
if (text) console.log((await text.fullLoad()).toString());
// ...
},
},
};
Licence
copyrights (c) AliBasicCoder 2020