Skip to content

Instantly share code, notes, and snippets.

@onelittlenightmusic
Created December 24, 2018 22:48
Show Gist options
  • Save onelittlenightmusic/5b1c7d34aab1a9a6fb4e83ee702c7587 to your computer and use it in GitHub Desktop.
Save onelittlenightmusic/5b1c7d34aab1a9a6fb4e83ee702c7587 to your computer and use it in GitHub Desktop.
apollo on lambda
import {makeRemoteExecutableSchema, mergeSchemas, introspectSchema} from 'graphql-tools';
import { callbackify } from 'util';
const fetch = require('node-fetch');
const { HttpLink } = require('apollo-link-http');
const { ApolloServer, gql } = require('apollo-server-lambda');
const createServer = async () => {
const createRemoteSchema = async (uri) => {
const link = new HttpLink({uri, fetch});
return makeRemoteExecutableSchema({
schema: await introspectSchema(link),
link
});
};
//const originalSchema = await createRemoteSchema('http://localhost:4000');
const citySchema = await createRemoteSchema('https://evening-scrubland-62386.herokuapp.com/');
const cityEstateSchema = await createRemoteSchema('https://frozen-chamber-61303.herokuapp.com/');
const linkSchemaDefs = gql`
extend type City {
cityEstate: CityEstate
}
`;
const schema = mergeSchemas({
schemas: [citySchema, cityEstateSchema, linkSchemaDefs],
resolvers: {
City: {
cityEstate: {
fragment: `fragment CityFragment on City {code}`,
resolve(parent, args, context, info) {
const cityCode = parent.code;
//const place: string = parent.name
return info.mergeInfo.delegateToSchema({
schema: cityEstateSchema,
operation: 'query',
fieldName: 'cityEstate',
args: {cityCode},
context,
info
});
}
}
}
}
});
return new ApolloServer({ schema });
};
exports.graphqlHandler = async (event, context, callback) => {
const server = await createServer();
const handler = server.createHandler();
// const rtn = await handler(event, context, callback);
// console.log(rtn);
// return {"body":"{\"data\":{\"hello\":\"Hello world!\"}}\n","statusCode":200,"headers":{"Content-Type":"application/json"}}
var context_new = {}
Object.assign(context_new, context)
Object.assign(context_new, { done: (a, b)=>{return;}})
await handler(event,
{},
(e,result) => {
console.log(result);
callback(e, result);
});
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment