hoangvvo / graphyne Goto Github PK
View Code? Open in Web Editor NEW⚡ Lightning-fast JavaScript GraphQL Server ⚡
License: MIT License
⚡ Lightning-fast JavaScript GraphQL Server ⚡
License: MIT License
Graphyne does not have File upload feature built in. To enable file upload, you can use the graphql-upload library (which is also used by other GraphQL servers like Apollo).
graphql-upload
via npm/yarn:npm i graphql-upload
// or
yarn add graphql-upload
When constructing your schema, include Upload
scaler and its resolver.
const { makeExecutableSchema } = require('graphql-tools')
const { GraphQLUpload } = require('graphql-upload')
const typeDefs = `
scalar Upload
`
const resolvers = {
Upload: GraphQLUpload
}
const schema = makeExecutableSchema({ typeDefs, resolvers });
graphql-upload
middleware.const express = require('express')
const { graphqlUploadExpress } = require('graphql-upload')
app.use(
graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
httpHandler(graphyne)
);
You can use processRequest
in this case and save it to req.body
. httpHandler
creates a function that handles req
and res
so can be used like below:
const { processRequest } = require('graphql-upload')
const gqlHandle = httpHandler(graphyne)
async function handler(req, res) {
if (req.headers['content-type'].includes('multipart/form-data')) {
req.body = await processRequest(req, res);
}
gqlHandle(req, res)
}
// Micro
module.exports = handler;
// Node HTTP Server
http.createServer(handler).listen(PORT)
If you have any issue, feel free to tell me.
After several months of experimenting with graphyne-server
, I'm sadly to announce that the approach does not work like I thought.
The goal of graphyne-server
is to create a truly agnostic http transport for GraphQL. However, in order to support multiple frameworks, graphyne-server
makes multiple compromise on reliability, performance, and developer experience. Therefore, there is a need to move away from the agnostic approach.
Therefore, I have decided to split up the packages again to several framework-specific:
graphyne-core
This will stay and still be used by other packages
graphyne-server
onRequest
, onResponse
)context
is no longer in the constructor of GraphyneServer
, it will be moved to createHandler
createHandler
is no longer a method in the GraphyneServer
class. Instead, it will become an export. It will be a function that accepts context
, which is a function that is the same as the framework handler one. The new function will have the somewhat like the following signature:httpHandler(graphyne, options)
Express.js
, micro
and Node HTTP Server
still use this package because their handle functions are compatible
graphyne-fastify
To support fastify
.
createHandler
will be the same.createPlugin
/fp
function that create a fastify-plugin
compatible plugin.graphyne-ws
I'm thinking of making this usable with or without graphyne-server
. We will do so by having a new startSubscriptionServer
function signature:
function wsHandler(graphyne, wss, options)
So, the first argument can either be GraphyneServer
instance or simply GraphQL options like schema
, formatError
, etc.
The second argument will include context
, our custom super-long-named option onGraphyneWebSocketConnection
More detail expected.
DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
It is the solution to GraphQL N+1 issue (such as making database queries when it is not needed). The way it works can be illustrated in this article.
One way to integrate the dataloader package into Graphyne is by using context
since this allows the loader to be valid per request only and avoid stale data.
dataloader
via npm/yarn:npm i dataloader
// or
yarn add dataloader
const DataLoader = require('dataloader')
const graphyne = new Graphyne({ schema });
httpHandler(graphyne, {
context(req) {
// This is your business logic
const userLoader = new DataLoader((keys) => db.findUsersByIds(keys));
// Maybe add even more loaders
return {
userLoader,
};
},
});
// In a resolver far, far away
const resolvers = {
getUser(obj, args, context) {
return context.userLoader.load(args.id);
},
};
This is helpful when you want to approach "Fetching data at a field-level" as recommended by the Paypal Engineering team without worrying about over-fetching.
Sometimes context would lose its value.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
It should maintain subscription context
This issue is for me to keep track of my plan to move graphyne
toward 1.0. Feel free to chime in!
Even though, it is said that graphyne-ws
follows GraphQL over WebSocket Protocol, we have several differences:
GQL_ERROR
Server sends this message upon a failing operation, before the GraphQL execution, usually due to GraphQL validation errors (resolver errors are part of GQL_DATA message, and will be added as errors array)
graphyne-ws
sends this as GQL_DATA
instead of GQL_ERROR
. However, weird enough, even subscriptions-transports-ws
implementation does the same: https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/server.ts#L350-L354 -- and does not follow the section of the protocol either.
I will need to look into some GraphQL client implementation to see how it handles GQL_ERROR
.
The only times graphyne-ws
sends GQL_ERROR
are:
This issue will be updated if there is any additional deviations.
Describe the bug
When using with koa
, POST request always result in 404 Not Found. My guess is that the function must return a promise or resolve immediately.
To Reproduce
https://github.com/hoangvvo/graphyne/tree/master/examples/with-koa
Expected behavior
It should execute GraphQL.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.