Created
January 11, 2018 22:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
getVal | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"getVal": "value!" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment