Skip to content
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

Updated deprecated variable name from format_error to GraphQLFormattedError #1320

Closed
wants to merge 1 commit into from

Conversation

qe
Copy link

@qe qe commented Apr 9, 2022

While using graphene-django, I ran into the following ImportError:

ImportError: cannot import name 'format_error' from 'graphql.error'

Looking into graphql.error, here is an excerpt of graphql-core's /error/__init__.py file:

__all__ = [
    "GraphQLError",
    "GraphQLErrorExtensions",
    "GraphQLFormattedError",
    "GraphQLSyntaxError",
    "located_error",
]

I believe graphene_django/views.py's format_error variable name is most likely deprecated, so locally, I replaced format_error with GraphQLFormattedError from the excerpt above, and it is now working locally.

Here is graphql-core's commit that deprecated the format_error variable name

Copy link

@NwawelAIroume NwawelAIroume left a comment

Choose a reason for hiding this comment

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

tested the change on my side and it is working on my updated project

Copy link

@NwawelAIroume NwawelAIroume left a comment

Choose a reason for hiding this comment

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

Hi tested again the your commit in my project and it is not OK. i get an error on the change you made.

'GraphQLError' object is not iterable
.
.
.
graphene_django/views.py", line 389, in format_error
    return format_graphql_error(error)
  File "/usr/lib/python3.8/typing.py", line 1709, in _dict_new
    return dict(*args, **kwargs)

Exception Type: TypeError at /openapiql
Exception Value: 'GraphQLError' object is not iterable

look at the following code in the python graphql package to take into consideration
python3.8/site-packages/graphql/error/graphql_error.py

def format_error(error: GraphQLError) -> GraphQLFormattedError:
    """Format a GraphQL error.

    Given a GraphQLError, format it according to the rules described by the "Response
    Format, Errors" section of the GraphQL Specification.

    .. deprecated:: 3.2
       Please use ``error.formatted`` instead. Will be removed in v3.3.
    """
    if not isinstance(error, GraphQLError):
        raise TypeError("Expected a GraphQLError.")
    return error.formatted

so GraphQLError instances which are to be formatted should be error.formatted

Can you review your changes? Thanks

@moritz89
Copy link

moritz89 commented Jun 1, 2022

It might make sense to restrict the graphql-core dependency to the minor version as they have mentioned that the minor versions are not compatible with another.

Note that for various reasons, GraphQL-core does not use SemVer like GraphQL.js. Increases in the major version of GraphQL.js are reflected in the minor version of GraphQL-core instead. This means there can be breaking changes in the API when the minor version changes, and only patch releases are fully backward compatible. Therefore, we recommend something like =~ 3.2.0 as version specifier when including GraphQL-core as a dependency.

@tcleonard
Copy link
Collaborator

I honestly don't understand why they removed it from the init because the format_error method still exists... they only changed the returned type to be more specific than a dict that could basically have any keys before!
so I guess the real fix is to change the import from

from graphql.error import format_error as format_graphql_error

to

from graphql.error.graphql_error import format_error as format_graphql_error

@erikwrede
Copy link
Member

Fixed in #1327

@erikwrede erikwrede closed this Aug 15, 2022
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.

6 participants