Reputation: 3924
Having an issue when executing a paginated query with Hasura graphql Go client github.com/hasura/go-graphql-client
but haven't found the answer in the docs. The graphql query is as follows (simplified it for this question):
query ($after: String) {
entity (first: 10, after: $after) {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
}
}
}
The Go code
type Query struct {
Entity struct {
PageInfo struct {
HasNextPage bool `graphql:"hasNextPage"`
EndCursor string `graphql:"endCursor"`
} `graphql:"pageInfo"`
Nodes []struct {
Name string `graphql:"name"`
} `graphql:"nodes`
} `graphql:"entity(first: 10, after: $after)"`
}
var q Query
var after string = ""
var hasNextPage bool = true
variables := map[string]interface{}{
"after": after,
}
err := client.Query(context.Background(), &q, variables)
The thing is that I don't know how to declare the $after variable and when executed it throws an error
"Failed to parse \"String\": EOF while parsing a value at line 1 column 0"
Anyone can help with the issue? Thanks in advance.
Upvotes: 0
Views: 134
Reputation: 3924
The code is ok, the problem is that the query throws an error if the after variable (cursor Id) is empty or has a value that doesn't match with a cursor.
So the solution was to do a first query without the $after variable like this
type Query struct {
Entity struct {
PageInfo struct {
HasNextPage bool `graphql:"hasNextPage"`
EndCursor string `graphql:"endCursor"`
} `graphql:"pageInfo"`
Nodes []struct {
Name string `graphql:"name"`
} `graphql:"nodes`
} `graphql:"entity(first: 10)"`
}
...
variables := map[string]interface{}{}
and once we have a right value for Endcursor we can do the subsequent queries with that value for the $after variable like in the question
type Query struct {
Entity struct {
PageInfo struct {
HasNextPage bool `graphql:"hasNextPage"`
EndCursor string `graphql:"endCursor"`
} `graphql:"pageInfo"`
Nodes []struct {
Name string `graphql:"name"`
} `graphql:"nodes`
} `graphql:"entity(first: 10, after: $after)"`
}
...
variables := map[string]interface{}{
"after": after,
}
Maybe there's a better way to do it without duplicating code, but haven't found it.
Upvotes: 0