aqwert
aqwert

Reputation: 10789

Apollo Server Graphql within AWS Lambda

I have a local instance running fine with the Apollo Graphql libraries but I want to use the same schema and resolvers within the AWS Lambda.

My current code is:

var makeExecutableSchema = require('graphql-tools').makeExecutableSchema;

var resolvers = require('./resolvers/root').root;
var schema = require('./schema/graphSchema').schema;

import graphqlHTTP from 'express-graphql';

import express from 'express';

//graphql express
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';

import path from 'path';

const config = require('./config/main.json');
const port = (!global.process.env.PORT) ? 1234 : global.process.env.PORT;
const server = global.server = express();

const allowCrossDomain = function (req, res, next) {
    //slow down the requests to mimic latency
    setTimeout(() => {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
        res.header('Access-Control-Allow-Headers', 'Connection, Host, Origin, Referer, Access-Control-Request-Method, Access-Control-Request-Headers, User-Agent, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, Accept-Encoding, Accept-Language');

        if ('OPTIONS' == req.method) {

            res.sendStatus(200);
        } else {
            next();
        }
    }, 1000);
}
var executableSchema = makeExecutableSchema({
    typeDefs: schema,
    resolvers: resolvers,
});

exports.executableSchema = executableSchema;

server.set('port', port);
server.use(express.static(path.join(__dirname, 'public')));
server.use(allowCrossDomain);

server.use('/graphql',
    bodyParser.json(),
    graphqlExpress({
        schema: executableSchema
    }));
server.use('/graphiql',
    graphiqlExpress({
        endpointURL: '/graphql'
    }));

server.listen(server.get('port'), () => {
    console.log('The server is running at http://localhost:' + server.get('port'));
});

I use graphql-tools to join the schema and resolvers rather than using graphql makeSchema method.

I am not sure how to convert the old graphql code that worked into the same working example using apollo tools and server...

in the lambda handler I have this but need it to use apollo express server.

var graphql = require('graphql').graphql;
var resolvers = require('./src/resolvers/root').root;
var schema = require('./src/schema/graphSchema').schema;
module.exports.graphql = (event, context, callback) => {
    graphql(schema, event.body.query, resolvers, {}, event.body.variables)
         .then((response) => callback(null, response))
         .catch((error) => callback(error));
};

What can be used here within the lambda function to use graphqlExpress instead?

Upvotes: 2

Views: 2270

Answers (1)

JakubKnejzlik
JakubKnejzlik

Reputation: 6483

I've been struggling with this as well. I found that the safest solution is to combine aws-serverless-express with express-graphql. It's not the most straightforward solution, so it led me to create a module for it: https://www.npmjs.com/package/lambda-graphql

Upvotes: 1

Related Questions