Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Last active January 15, 2018 04:05
Show Gist options
  • Save jlengstorf/f6b6933e83f38a80e6914ded46b0385e to your computer and use it in GitHub Desktop.
Save jlengstorf/f6b6933e83f38a80e6914ded46b0385e to your computer and use it in GitHub Desktop.
Schema stitching in GrAMPS with two external data sources.

Schema Stitching in GrAMPS Using External Data Sources

In this example, we install two GrAMPS data sources from npm, then extend the schema to allow one data source to access data on the other.

{
"presets": [
[
"@babel/preset-env",
{
"targets": { "node": "6" },
"shippedProposals": true,
"useBuiltIns": "usage"
}
]
]
}
import Express from "express";
import getPort from "get-port";
import bodyParser from "body-parser";
import { prepare } from "@gramps/gramps";
import { GraphQLSchema } from "graphql";
import { mergeSchemas } from "graphql-tools";
import { graphqlExpress, graphiqlExpress } from "apollo-server-express";
// Data sources
import XKCD from "@gramps/data-source-xkcd";
import Numbers from "@gramps/data-source-numbers";
const gramps = prepare({
dataSources: [XKCD, Numbers]
});
const linkTypeDefs = `
extend type XKCD_Comic {
numbers: Numbers_Trivia
}
`;
const schema = mergeSchemas({
schemas: [gramps.schema, linkTypeDefs],
resolvers: mergeInfo => ({
XKCD_Comic: {
numbers: {
fragment: `fragment XKCDFragment on XKCD_Comic { num }`,
resolve: (parent, args, context, info) => {
return mergeInfo.delegate(
"query",
"trivia",
{ number: parent.num },
context,
info
);
}
}
}
})
});
const app = new Express();
app.use(bodyParser.json());
app.use(
"/graphql",
graphqlExpress({
schema,
context: gramps.context
})
);
app.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));
async function startServer(app) {
const PORT = await getPort(8080);
app.listen(PORT, () => {
console.log(`\n=> http://localhost:${PORT}/graphiql`);
});
}
startServer(app);
{
"name": "gramps-stitching-external",
"version": "1.0.0",
"main": "index.js",
"author": "Jason Lengstorf (https://lengstorf.com) <jason@lengstorf.com>",
"license": "MIT",
"scripts": {
"start": "nodemon -e js,json,graphql index.js --exec babel-node",
"build": "babel index.js -d dist",
"predev": "yarn build",
"dev": "gramps dev -d ../../gramps-graphql/data-source-base -g dist"
},
"dependencies": {
"@gramps/data-source-numbers": "^1.0.0-beta3",
"@gramps/data-source-xkcd": "^1.0.3-beta4",
"@gramps/gramps": "^1.2.0",
"apollo-server-express": "^1.3.2",
"body-parser": "^1.18.2",
"express": "^4.16.2",
"get-port": "^3.2.0",
"graphql": "^0.12.3",
"graphql-tools": "^2.18.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.37",
"@babel/core": "^7.0.0-beta.37",
"@babel/preset-env": "^7.0.0-beta.37",
"@gramps/cli": "^1.1.4",
"nodemon": "^1.14.11"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment