Pageable resultset for gql queries
Project description
gql-relayresult
gql-relayresult is a simple result datastructure for GraphQL gql queries (https://pypi.org/project/gql/, working with the latest prerelase) which supports pagination so that it's easy to iterate through all pages like this:
async for x in result:
mylist.append(x["node"]["value"])
The result must contain pageInfo based on
https://graphql.org/learn/pagination/
Example
Let's say you have a GraphQL query like this:
query =
"""
query getNumericValues($first: Int, $after: String) {
numericValues(first: $first, after: $after) {
pageInfo {
hasNextPage,
hasPreviousPage,
startCursor,
endCursor
}
edges {
node {
value
}
}
}
}
"""
Then you simply can execute the query and iterate through the resultset while the following result pages are automatically fetched when needed.
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from gql_relay_result import GqlRelayResult
transport = AIOHTTPTransport(url=url)
client = Client(transport=self._transport, fetch_schema_from_transport=True)
gqlQuery = gql(query)
params = {"first": first}
data = await client.execute_async(gqlQuery, params)
result = GqlResult(data, gqlQuery, client.execute_async)
#now you can itereate through the result
actual = []
async for x in result:
actual.append(x["node"]["value"])
It's important that you work with query variables as the after
parameter is automatically replaced by the endCursor
provided in the pageInfo
structure.
All other parameters won't be changed.
Providing a factory method
If you don't want to access the resulting dictionary but create custom instances you can pass in a factory method.
# using a lamda to create instances of type 'Data'
result = GqlRelayResult(result, gqlQuery, params, executor, lambda x: Data(x["node"]["value"]))
for x in result:
self.assertIsInstance(x, Data)
Querying child result sets
If your resulting data has child items which can be paged as well you may use the 'SubResult' class to query all remaining children if you need the complete data at once.
Just have a look at the 'test_that_resolve_nested_items_works' UnitTest there is an examplte to get an idea how it works.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file gql_relay_result-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: gql_relay_result-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48ac9e56b7c90644c88d7f9db6894cb1ad186ff3fea5816aa4f0161494832d50 |
|
MD5 | feed272c131655232d787ee925d57c3c |
|
BLAKE2b-256 | beb395077b544acce5c08a479cb8bc093f2d000f0ad9492d33b5817385c45c25 |