mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

Migration guide from 5.6.x => 6? #610

Closed mikefowler closed 6 years ago

mikefowler commented 6 years ago

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's createContext 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 use createContext.

Thanks for all the great work on this project and Sequelize!

mickhansen commented 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.

mikefowler commented 6 years ago

@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.

intellix commented 6 years ago

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 });
mickhansen commented 6 years ago

@intellix has a good example there, however i would not use a global dataloader but a per request one (since it has a cache)

mikefowler commented 6 years ago

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.

mickhansen commented 6 years ago

@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.

mmdevcodes commented 6 years ago

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!

mickhansen commented 6 years ago

@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 }