Problem getting pagination to work.
deep-c opened this issue · comments
Hey,
I am attempting to get pagination working using limit, offset, and order to no avail. I keep getting back the first 50 results and I am unsure if I am doing anything wrong.
I am attempting to do offset based pagination like so. Here is the message query.
const messageQuery = {
messages: {
type: new GraphQLList(messageType),
args: {
channelId: {
type: GraphQLString
},
order: {
type: GraphQLString
},
limit: {
type: GraphQLInt
},
offset: {
type: GraphQLInt
},
},
resolve: resolve('Messages', null, {
before: (opts, args, context, info) => {
console.log('ARGS/OPTS', args, opts)
console.log('offset', opts.offset, 'limit', opts.limit)
return opts
}
})
},
message: {
type: messageType,
args: {
id: {
type: new GraphQLNonNull(GraphQLString)
},
order: {
type: GraphQLString
},
...commonArgs
},
resolve: resolve('Messages')
}
};
The model is as follows:
const Messages = thinky.createModel("Messages", {
id: type.string().uuid(4).allowNull(false),
content: type.string().allowNull(false),
channelId: type.string().allowNull(false),
createdBy: type.string().uuid(4).allowNull(false),
createdOn: type.date().default(thinky.r.now()),
})
Messages.relations = () => {
Messages.belongsTo(thinky.models.Channels, "channel", "channelId", "id")
Messages.belongsTo(thinky.models.Users, 'creator', 'createdBy', 'id')
}
The query is as such:
query MessagesForChannel($channelId: String!, $offset: Int, $limit: Int, $orderBy: String){
messages(channelId: $channelId, offset: $offset, limit: $limit, order: $orderBy){
id
content
createdOn
creator {
id
displayName
name {
givenName
familyName
middleName
}
avatar
status
}
}
}
I can see that the appropriate variables are being sent along with the query however the offset and limit args dont seem to be picked up and used when resolving the query.
variables
:
Object
channelId
:
"tech-links"
offset
:
350
orderBy
:
"reverse:createdOn"
I am using v0.4.0-rc-3 without relay. (Using apollo client and its offset based pagination http://dev.apollodata.com/react/pagination.html)
Thanks!
Also one other thing i noticed when attempting to use relay style pagination was that when i used after
and first
the next page included the after cursor result. After reading this https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm it seems that the next page shouldnt include that cursor specified as after. That is unless im doing something wrong.
Sorry for the trouble :)
@deep-c Hi deep-c thanks for opening this issue, going trough the source-code I saw that pagination is done with the following parameters:
offset
: 10 - skip
: 2
not sure why limit is not used instead of offset, I will look at that. I'll keep you up to date
Also one other thing i noticed when attempting to use relay style pagination was that when i used after and first the next page included the after cursor result. After reading this https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm it seems that the next page shouldnt include that cursor specified as after. That is unless im doing something wrong.
Thanks for the gotcha will look at the too
@fenos Thanks!. I just have a question in terms of the use of limit, offset and skip. Correct me if I am wrong but shouldn't offset be used to determine the position from which you would like to retrieve records from, and limit used to define the number of records you would like from that position? I.e assuming you have 50000 records and have specific an offset of 20000
and limit of 50
you would get back records 20000 to 20049.
Also i see there is a maxLimit option which defaults to 50. How do I change this so that i am able to paginate through all results (past the first 50). When I turn opts.maxLimit = false
in the before method of resolve i still get 0 results returned past 50 .