Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Created January 11, 2018 22:29
Show Gist options
  • Save jlengstorf/1d9a6ebb34f3b5533f13acea87abb981 to your computer and use it in GitHub Desktop.
Save jlengstorf/1d9a6ebb34f3b5533f13acea87abb981 to your computer and use it in GitHub Desktop.
An example of a GraphQL gateway set up using the `prepare()` function from GrAMPS.
import Express from "express";
import getPort from "get-port";
import bodyParser from "body-parser";
import { prepare } from "@gramps/gramps";
import { GraphQLSchema } from "graphql";
import { graphqlExpress, graphiqlExpress } from "apollo-server-express";
const MyDataSource = {
namespace: "Test",
typeDefs: `type Query { getVal: String }`,
resolvers: { Query: { getVal: (_, __, ctx) => ctx.getVal() } },
context: { getVal: () => "value!" }
};
async function startServer(app) {
const PORT = await getPort(8080);
app.listen(PORT, () => {
console.log(`=> server running at http://localhost:${PORT}/`);
});
}
const app = Express();
const GraphQLOptions = prepare({ dataSources: [MyDataSource] });
app.use(bodyParser.json());
app.use("/graphql", graphqlExpress(GraphQLOptions));
app.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));
startServer(app);
{
"data": {
"getVal": "value!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment