mickhansen / graphql-sequelize

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

Support for root Relay Connections #514

Closed mcurcio closed 7 years ago

mcurcio commented 7 years ago

Is there currently a way to generate Relay-compatible connection schemas from a single Model (i.e. not an association)?

mickhansen commented 7 years ago

Yes, you simply use target: Model and scope everything in your before

mcurcio commented 7 years ago

I'm not sure what you mean by "scope everything in your before". I tried applying a model as the target and I am not seeing good results. My schema looks something like this:

User = sequelize.define('user', {});

const Users = sequelizeConnection({
        name: 'Users',
        nodeType: User,
        target: User
});

const schema = new GraphQLSchema({
        query: new GraphQLObjectType({
                name: 'RootQueryType',
                fields: {
                        users: {
                                type: Users.connectionType,
                                args: Users.connectionArgs,
                                resolve: Users.resolve
                        }
                }
        })
});

And when run against this query:

{
  users {
    pageInfo {
      hasNextPage
    }
  }
}

I get the following error:

{
  "data": {
    "users": null
  },
  "errors": [
    {
      "message": "Cannot return null for non-nullable field UsersConnection.pageInfo.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "users",
        "pageInfo"
      ]
    }
  ]
}
mickhansen commented 7 years ago

That's odd, target: Model should work, we use it a few places and then we filter based on source in the before resolver options hook

mcurcio commented 7 years ago

Good to know. I'll poke around some more.

mcurcio commented 7 years ago

It is working now. Thanks for the guidance. I didn't make any significant changes at all ... it simply started working.

I think my issue is that I was mixing up my resolvers since I have the very similarly named objects User and Users where the former is a GraphQLObjectType and the latter is a graphql-sequelize Connection. For what its worth, I did also upgrade to the latest sequelize/graphql-sequelize.