Closed mikefowler closed 6 years ago
Hi @mikefowler
Usually you'll create a context wherever you initiate the query, or in the middleware, at Snaplytics we create a context the same place we setup the graphql viewer.
@mickhansen I understand the concept of context, and where/how you plug it into a GraphQL schema, but it's still not clear to me how graphql-sequelize
is intended to be used in conjunction with dataloader-sequelize
. I think a small examples/
file would be greatly beneficial.
Don't remember entirely all the changes, but I think it looks like this:
sequelize.ts
export const sequelize = new Sequelize(config[env]);
sequelize.addModels([Note, Transaction, User]);
schema.ts
import { schema } from './sequelize.ts';
const { resolver, typeMapper } = require('graphql-sequelize');
const { createContext, EXPECTED_OPTIONS_KEY } = require('dataloader-sequelize');
// Globally enable dataloader
const dataloaderContext = createContext(sequelize);
resolver.contextToOptions = { [EXPECTED_OPTIONS_KEY]: dataloaderContext };
...
nodeTypeMapper.mapTypes({ ... });
...
export const schema = new GraphQLSchema({ query: Query });
@intellix has a good example there, however i would not use a global dataloader but a per request one (since it has a cache)
Thanks, @intellix. @mickhansen, the per-request context example is precisely what I think would be valuable to see, if you already have it implemented in one of your projects.
@mikefowler It's the example that @intellix posted but rather than creating a context globally you do it for each request and then add it to the graphql context.
I have it implemented but copy pasting the code wouldn't make sense due to how the project is set up.
Hi, i'm having a difficult time figuring out how I would be able to use the dataloader context per request. From my understanding, for example I would setup a basic express-graphql
like so:
const dataloaderContext = createContext(sequelize);
app.use('/', GraphHTTP((request) => ({
schema: Schema,
context: dataloaderContext,
})));
Then the GraphQLSchema
would be setup like in the README, but then after that i'm lost. Can we get like a most basic of basic example of doing this? Thanks!
@MauriceMahan context
is the graphql context, it should have a key pointing to the dataloader sequelize context.
That's where the EXPECTED_OPTIONS_KEY
comes in.
context[EXPECTED_OPTIONS_KEY] = dataloaderContext
and then resolver.contextToOptions = { [EXPECTED_OPTIONS_KEY]: EXPECTED_OPTIONS_KEY }
Hi @mickhansen (and @idris who helped propose some of these changes)! I'm finally getting around to upgrading my version of
graphql-sequelize
from 5.6 to current.Is there a guide or example repository showing the correct way to set up
dataloader-sequelize
'screateContext
method in the context of a Node server? I've read the docs for both repositories several times over and it remains unclear to me where I actually usecreateContext
.Thanks for all the great work on this project and Sequelize!