Graphql-yoga: AWS Lambda support

Created on 29 Nov 2017  路  11Comments  路  Source: dotansimha/graphql-yoga

I've been prototyping an API to use with AWS Lambda and this is a basic example of what it could look like:

import { GraphQLLambda } from 'graphql-yoga';

const typeDefs = `
    type Query {
        hello(name: String): String!
    }
`;

const resolvers = {
    Query: {
        hello: (_, { name}) => `Hello ${name || 'World'}`,
    },
}

const handler = new GraphQLLambda({ typeDefs, resolvers });

export const graphql = handler.graphql;
export const playground = handler.playground;

The implementation would be simple to put together for basic use cases, but subscriptions and file-upload would be missing. Binary file-upload is possible already with AWS lambda through API gateway, but if you're using serverless framework (which I imagine most people are), it doesn't currently support it as a configuration option. I'll likely open an issue with serverless to add binary pass-through as an option. Following that, apollo-upload-server would also need lambda support, but that is a trivial addition that I can do.

Subscriptions are the one feature that cannot be entirely handled in lambda. What works instead is having a separate subscription server using express graphql-yoga or some other http server with websocket support and then use remoteSchemaStitching to then have the subscription server resolve queries through the lambda interface.

Looking for some feedback on this approach before anything gets implemented. Thanks!

All 11 comments

Good news on the serverless front for file uploads. There is already a serverless plugin for allowing binary types maciejtreder/serverless-apigw-binary. There is already an issue in serverless/serverless#2797, for enabling it in core, but a plugin works for now. This leaves apollo-upload-server the only blocker for adding file upload support in lambda.

@jtmthf Do you have a branch where we can look at the code in your GraphQLLambda approach?

The suggested API looks really nice and clean. I like it @jtmthf!
I think it's fine that for now file-upload & subscriptions don't work yet. This can be added later!

Would be great if you could create a PR for this? 馃憤

@chrisl777 @schickling I don't have an implementation yet, but I will be putting it together this weekend with a PR.

I did run into one issue that would be problematic with the current API. As Lambda requires the handler to be provided synchronously as a module export, the schema cannot be created asynchronously. One common case of this is introspecting a graph.cool backend. To resolve this in a prior project of mine, I did this in the entry:

import {
  graphqlLambda,
  LambdaGraphQLOptionsFunction
} from "apollo-server-lambda";

import { schema } from './schema';

const optionsFunction: LambdaGraphQLOptionsFunction = async () => {
  return {
    schema: await schema.getSchema()
  };
};

export const graphql = graphqlLambda(optionsFunction);

Where schema is a singleton class instance that caches the schema to prevent re-instrospecting graph.cool on each invocation while the function is warm.

I can leave out this concern in an initial PR, but I think there should be some consideration towards having built-in support for async generated schemas, especially for simple remote schemas.

Would it be an option in that case to read the schema from a file instead? (This is what we're seeing as a emerging best practice.)

That would work, or would it be even better to have explicit support for graphql-config? That would be a separate PR of course.

Initially, I think in the case of Lambda, it would be great to have support for using a schema string in a local variable, or reading from a local .graphql file. For a Lambda, it is probably better to cache the schema locally, since you get billed for execution time.

Then, later on, you could add support for graphql-config, for the more intensive cases that people may have.

One of the additional issues with Lambda is the websockets support. Lambda is not designed for long-lived connections, so as much as I'd like to see yoga deployed to AWS Lambda, I don't know if it's a good fit altogether...

Lambda support is released in v0.9.0.

@kbrandwijk how is websocket going to work on a lambda?

Unfortunately websockets are not yet supported via AWS Lambda but I encourage you to look into now as an easy alternative. If you want to stick with AWS you can also host Yoga using containers for example via ECS or Elastic Beanstalk.

Was this page helpful?
0 / 5 - 0 ratings