Giter Site home page Giter Site logo

graphyne's People

Contributors

actions-user avatar dependabot-preview[bot] avatar hoangvvo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

graphyne's Issues

Guide: File Upload

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

How-to

Install 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 });

Use graphql-upload middleware.

Express

const express = require('express')
const { graphqlUploadExpress } = require('graphql-upload')

app.use(
  graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
  httpHandler(graphyne)
);

Other framework

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.

New approaches for graphyne

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

  • The package will stop supporting different frameworks' handler signatures (removing 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.
    A new 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.

Guide: Dataloader

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.

How to

Install dataloader via npm/yarn:

npm i dataloader
// or
yarn add dataloader

Add Dataloader instance to context

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.

graphyne-ws: Resolver context is undefined

Sometimes context would lose its value.

To Reproduce
Steps to reproduce the behavior:

  1. Create a context
  2. Subscribe to an event
  3. Do the mutation/query that triggers the event several times
  4. See error

Expected behavior

It should maintain subscription context

Graphyne Server 1.0 Roadmap

This issue is for me to keep track of my plan to move graphyne toward 1.0. Feel free to chime in!

  • Subscription support
  • Dataloader integration (1 + N Problems)
  • Persisted queries
  • File Upload (inbuilt or optional)
  • Caching Header API for CDN
  • Testing

graphyne-ws: Deviation from 'GraphQL over WebSocket Protocol'

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:

  • No query string provided
  • Malformed message
  • Internal, non GraphQL error

This issue will be updated if there is any additional deviations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.