Skip to main content

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gql-relay-result-0.0.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

gql_relay_result-0.0.1-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page