Python Django Graphene ImportError

Problem

You did a tutorial with Django and Graphene and your ode just doesn´t work but it looks the same? Or are you working on an existing project, doing some extensions analog a different module and get unexplainable Import Errors?

Do you receive  Could not import 'ss.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: type object 'Query' has no attribute '_meta'. ?

Solution

There are 2 potential issues that know:

  1. The order of your imports
  2. The order of your query/mutation functions

Order of Imports

The import order is often important.

First import your base classes (e.g. BaseQuery, BaseMutation).

Then import the derived classes (the ones that use your base classes as the super class).

Import Queries first, Mutations second.

That was one big issue I had, ordering in the imports mixed up and Django threw the Import error as it wasn´t able to fix it automatically.

Order of Query/Mutation functions

If you still receive the import error, you likely have Query function definition and Mutation function definitions in the same file and mixed together (e.g. AuthorNode, AuthorMutation, BookNode, BookMutation, Query, Mutation in your schema file).

That is confusing Django to throw the error too.

First, have all your Query function definition and Nodes, then the Query Class.

Then have all your mutation function definitions and your Mutation class.

Reload your server or if you use auto-reload on file change, just wait until it reloaded. If it was this problem, it works now.

Let me know if it helped you.

Best,

Frank

Sources

https://stackoverflow.com/questions/47267330/django-importerror-at-graphql

Leave a Reply