mickhansen / graphql-sequelize

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

[Help] How To integrate dataloader-sequelize #645

Closed mubaidr closed 5 years ago

mubaidr commented 5 years ago

There are conflicting documentation by both repositories on how to integrate dataloader-sequelize with graphql-sequelize.

I tried following the example project graphql-yoga, but there is an error when running any query:

TypeError: Cannot read property 'findById' of undefined
    at shimModel (D:\Current\ors-node\api\node_modules\dataloader-sequelize\lib\index.js:281:14)
    at createContext (D:\Current\ors-node\api\node_modules\dataloader-sequelize\lib\index.js:526:3)
    at GraphQLServer.context (D:\Current\ors-node\api\index.js:21:31)
    at GraphQLServer.<anonymous> (D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:231:51)
    at step (D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:43:23)
    at Object.next (D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:24:53)
    at D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:18:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:14:12)
    at D:\Current\ors-node\api\node_modules\graphql-yoga\dist\index.js:224:118
    at D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:52:40
    at step (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:32:23)
    at Object.next (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:13:53)
    at D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:3:12)
    at resolveGraphqlOptions (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\graphqlOptions.js:43:12)
    at Object.<anonymous> (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\runHttpQuery.js:79:71)
    at step (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\runHttpQuery.js:42:23)
    at Object.next (D:\Current\ors-node\api\node_modules\apollo-server-core\dist\runHttpQuery.js:23:53)
    at D:\Current\ors-node\api\node_modules\apollo-server-core\dist\runHttpQuery.js:17:71
    at new Promise (<anonymous>)

and response from query:

Invalid options provided to ApolloServer: Cannot read property 'findById' of undefined

Server starting file: https://github.com/mubaidr/ors-node-graphql.js/blob/master/api/index.js#L21

GraphQl schema loading code: https://github.com/mubaidr/ors-node-graphql.js/blob/master/api/db/graphQl.js#L19

Can somebody guide me how to do it?

mickhansen commented 5 years ago

Your setup looks correct. It's failing in the shimModel code which is odd, apparently it's being passed an undefined Model, my best guess is one of your associations is defined incorrectly

mubaidr commented 5 years ago

Interesting. But when I disable dataloader-sequelize context, graphql api works fine. (which means, i think, all models and relationships are defined correctly?) 🤔

Also, what do you recommend to further debug this issue?

mickhansen commented 5 years ago

Hmm actually, looking at the line of code likely being hit here: https://github.com/mickhansen/dataloader-sequelize/blob/master/src/index.js#L474

What version of Sequelize are you using?

mubaidr commented 5 years ago

Ohh. Right now its on v5 beta.

I will test this with v4 and will report back. Thanks

mickhansen commented 5 years ago

It should work for v5 too, looking at the code, but i'm personally still on v3 so

mubaidr commented 5 years ago

@mickhansen Yup, that was it, sorry to bother you. Downgraded to v4; graphql api now seems to work. and I can confirm that dataloader-sequelize is working too, there is some noticeable difference in response time for nested query.

Thanks for your awesome work and support. 👍

mickhansen commented 5 years ago

I'd appreciate if you could test why that regex doesn't work @mubaidr and potentially provide a fix :)

mubaidr commented 5 years ago

Hmm Will try that. 👍