Skip to content

Add extensions support to GraphQLError #214

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

Merged
merged 1 commit into from
May 4, 2019
Merged

Conversation

odero
Copy link
Contributor

@odero odero commented Nov 6, 2018

No description provided.

@nikordaris
Copy link

@syrusakbary can we get this pulled in? This is a big blocker on our ability to pass error code metadata to the client with graphql-python.

@nikordaris
Copy link

nikordaris commented Apr 26, 2019

To add some more context for this PR. This is enabling the optional capability of the GraphQL spec that allows generic Map data to be included in the { errors: []} section of the response. The spec mandates this data be in the extensions field of an error. {errors: [{extensions: {}}]}.

https://graphql.github.io/graphql-spec/June2018/#sec-Errors

GraphQL services may provide an additional entry to errors with key extensions. This entry, if set, must have a map as its value. This entry is reserved for implementors to add additional information to errors however they see fit, and there are no additional restrictions on its contents.

My company is currently using this to pass error codes to the client as well as field level validation. Because the extensions section allows free form Map data the schema is up to us to decide what we want to include. We decided to follow this format:

{
  errors: [{
    extensions: {
      error: {
        error_code: "MyErrorCode",
        error_fields: [{path: "name", error: "Custom field error message"}],
        support_id: "1234-1234"
      }
    }
  }]
}

This PR makes defining the extensions data as a simple kwarg off of the GraphQLError type so that when I detect an error in my resolver i can simple raise GraphQLError('Foobar', extensions={'error': {'error_code': 'MyErrorCode'}}) or for convenience i can create my own Error type that will have kwargs for my specific error schema that would then get wrapped into the extensions field.

@ekampf ekampf requested a review from dan98765 April 26, 2019 22:40
@@ -40,6 +42,7 @@ def __init__(
self._positions = positions
self._locations = locations
self.path = path
self.extensions = extensions
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return None seems unnecessary.
Any idea why it's here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess would be for the typings check. It was there prior to this PR though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ekampf
Copy link
Contributor

ekampf commented Apr 26, 2019

Ok approving.
Also made sure there's a feature parity with graphql-core-next: https://github.com/graphql-python/graphql-core-next/blob/72171604d160774e384f51b74afb14c7e1b01b91/graphql/error/graphql_error.py#L79

@ekampf ekampf merged commit 3ba7f92 into graphql-python:master May 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants