Skip to content

Instantly share code, notes, and snippets.

@timrs2998
Last active January 4, 2018 18:38
Show Gist options
  • Save timrs2998/37719ba28bde8474c986c93b02672962 to your computer and use it in GitHub Desktop.
Save timrs2998/37719ba28bde8474c986c93b02672962 to your computer and use it in GitHub Desktop.
example of extraContext not being passed
node_modules/

Setup

git clone https://gist.github.com/37719ba28bde8474c986c93b02672962.git
cd 37719ba28bde8474c986c93b02672962
yarn install
yarn start

Usage

$ curl --request POST \
  --url 'http://localhost:8080/graphql' \
  --header 'content-type: application/json' \
  --data '{"query":"{\n  someField\n}","variables":{}}'
{"data":{"someField":"Hello, world"}}
const Express = require('express');
const bodyParser = require('body-parser');
const gramps = require('@gramps/gramps').default;
const { GraphQLSchema } = require('graphql');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const dataSourceExample = {
namespace: 'GitLastCommit',
context: {
getSomeField: () => Promise.resolve('Hello, world'),
},
typeDefs: `
type Query {
someField: String
}
`,
resolvers: {
Query: {
someField(_, __, context) {
console.log(context) // only shows 'getSomeField()'
console.log(context.some) // should log 'string' here, get 'undefined'
return context.getSomeField()
},
},
},
}
const app = Express();
const GraphQLOptions = gramps({
dataSources: [dataSourceExample],
extraContext: req => ({
some: 'string',
})
});
app.use(bodyParser.json());
app.use('/graphql', graphqlExpress(GraphQLOptions));
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
app.listen(8080, () => console.log(`=> running at http://localhost:8080/`));
{
"name": "demo-broken-context",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@gramps/gramps": "^1.1.0",
"apollo-server-express": "^1.3.2",
"express": "^4.16.2",
"graphql": "^0.12.3"
},
"devDependencies": {
"@gramps/cli": "^1.1.4"
},
"scripts": {
"start": "node .",
"dev": "npm start"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment