Skip to content

Customizable ExecutionResult, or how to get other API response parts #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
extrin opened this issue Jan 23, 2021 · 7 comments · Fixed by #257
Closed

Customizable ExecutionResult, or how to get other API response parts #188

extrin opened this issue Jan 23, 2021 · 7 comments · Fixed by #257
Labels
type: feature A new feature

Comments

@extrin
Copy link
Contributor

extrin commented Jan 23, 2021

return ExecutionResult(errors=result.get("errors"), data=result.get("data"))

Case:

The GraphQL API that I need to make requests to, responds with some other useful information, except of data block:

{
    "data": {...},
    "extensions": {
        "pageInfo": {
            "limitPage": 200,
            "totalCount": 23306516,
            "hasNextPage": true,
            "lastId": 41922710
        }
    }
}

But, as gql transport composes ExecutionResult from data and errors blocks only, I'm not able to get and use the extensions part in my code.
I was hoping to use hasNextPage and lastId values to iterate through pages of data, to load all the data to database.

Question:

Is there any way to get extensions part of response, along with data and errors, using gql?

@leszekhanusz
Copy link
Collaborator

Currently this is not possible, the execute and subscribe methods of the session only return the data or throw an error and as you noticed, the ExecutionResult instance is not created with the possible extensions field in the transports.

If you want direct access to the ExecutionResult instance, you could use the _execute and _subscribe methods of the session.

A simple PR could be made to add the extensions field of the ExecutionResult object for all the transports.

@leszekhanusz leszekhanusz added the type: feature A new feature label Jan 23, 2021
@KingDarBoja
Copy link
Contributor

KingDarBoja commented Jan 23, 2021

AFAIK the execution result only returns the data and errors fields according to the official spec and js implementation: https://graphql.org/graphql-js/execution/#execute

So no clue how that graphql-API is returning such extensions.

EDIT: I remember now this is some new change at graphql-js 15.0 and above. This was discussed at graphql-core issue 107, which I pointed to the spec as well 🙈 https://spec.graphql.org/draft/#sec-Response-Format

@extrin
Copy link
Contributor Author

extrin commented Jan 23, 2021

Thanks for your answers!
I see that the extensions field was added to ExecutionResult in graphql-core.
It seems that this change is not in release version yet. I just tried to fork repo and add extensions field for transports, but the ExecutionResult imported from graphql doesn't have this field either.

@dkbarn
Copy link

dkbarn commented Mar 26, 2021

A simple PR could be made to add the extensions field of the ExecutionResult object for all the transports.

Even if "extensions" was added to the ExecutionResult object for all transports, it would not be retrievable using Client.execute, because that method returns a Dict object containing only the contents of the "data" key: https://github.com/graphql-python/gql/blob/master/gql/client.py#L420

@KingDarBoja
Copy link
Contributor

This should be fixed by #190

@leszekhanusz
Copy link
Collaborator

Even if "extensions" was added to the ExecutionResult object for all transports, it would not be retrievable using Client.execute, because that method returns a Dict object containing only the contents of the "data" key: https://github.com/graphql-python/gql/blob/master/gql/client.py#L420

What we could maybe do, is to add an argument to the execute method to specify a key which will be inserted inside the data Dict.

    async def execute(self, document: DocumentNode, extensions_key: Optional[str] = None, *args, **kwargs) -> Dict:

        ....

        if result.extensions is not None and extensions_key is not None:
            result.data[extensions_key] = result.extensions

        return result.data

leszekhanusz pushed a commit that referenced this issue Apr 24, 2021
* Add extensions field to ExecutionResult
* Update graphql-core min version to 3.1.4
@leszekhanusz
Copy link
Collaborator

Finally I choose to add a get_execution_result argument to execute and subscribe methods.

See PR #257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants