mickhansen / graphql-sequelize

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

Pagination for one object #581

Closed moseleyi closed 4 years ago

moseleyi commented 6 years ago

I saw in the relay documentation that Sequelize Connection can take care of pagination but the example given is only when you want to paginate through children records (tasks of users). What if I only want to paginate through list of users, how to set it up?

intellix commented 6 years ago

I think you mean this? https://github.com/mickhansen/graphql-sequelize/issues/563#issuecomment-363579946

moseleyi commented 6 years ago

It's actually helpful, thank you, but where do I specify the pages. Do I use "first" and "last" Is that where "items per page" is calculated?

Also is it still possible to use standard ordering like:

orderBy:"username"

or because we use the SC everything goes through orderBy, I can't figure out how to still allow the above syntax for ordering. At the moment I'm able to do something like this

orderBy : new GraphQL.GraphQLEnumType({
    name : "username",
    values : {"username" : {value : ["username", "ASC"]}
});

but that needs to be called as orderBy:[username]

I know it's a small change but I'm trying to integrate with something that uses orderBy:"username"

intellix commented 6 years ago

first: 50 = you want the first 50 (ordered by ASC) last: 50 = you want the last 50 (ordered by DESC)

You've got cursors, so if you do pageInfo { endCursor } you can use it for something like: { first: 50 after: "cursor123==" }

Sounds like you wanna read up a bit more on Relay :) Your example of the orderBy is correct

moseleyi commented 6 years ago

Thanks, with the help of your link I figured it out. OrderBy is still a mystery because the connector I have wants to connect as order:"username", but in the relay we can only use order:[username]..

Do you know if there's a list of how to implement things like filter, search etc. ?

intellix commented 6 years ago

for search, you can specify a where function in the sequelizeConnection like so:

export const UserConnectionType = sequelizeConnection({
  ...
  where: (key: string, value: any, currentWhere: any) => {
    if (key === 'name') {
      return {
        name: { $like: `%${value}%` },
      };
    }

    return { [key]: value };
  },
});

in your root query:

users: {
  type: UserConnectionType.connectionType,
  args: {
    ...UserConnectionType.connectionArgs,
    name: {
      description: 'Fuzzy-matched name of user',
      type: GraphQLString,
    },
  },
  resolve: UserConnectionType.resolve,
},
mickhansen commented 6 years ago

but in the relay we can only use order:[username]

You can use order: USERNAME too. It's an ENUM yes, since you should use types whenever you can.

moseleyi commented 6 years ago

But I can't useorder:"username" anymore, can I? (I'm trying to connect CrudlIO with GraphQL Sequelize, and the way they compile the query is that you assign "sortable" attribute to a field and they use it as order:"")

Also I'm trying to find a list of all those arguments we can attach to sequelizeConnection? I would like to implement custom arguments like "search". Where can I find it?

Also the Crudl builds the descending order as order:"-username", which normally would work but after my hack to convert it to orderBy:[username], now it wants orderBy:[-username], which is of course not handled by sequelizeConnection. Would be useful to have a list of all the options of native graphql that are supported (and how to implement them), and those that are not

mickhansen commented 6 years ago

The Relay connection handler might be expecting an enum yes. There's a difference between "regular" GraphQL and Relay spec compliant GraphQL. I have no idea what Crudl is, is it a frontend client? If it's a backend thing you can just map an enum of username to "username"

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.