How do I reference multiple schema files in Graphene Django project settings for access in graphql?

477 Views Asked by At

I got a Django project with two apps. The project uses graphene-django. Each of the apps got their own schema.py file. How do i reference both the schema files in settings.py in the project folder so that I can access all the schema's in graphql?

I have tried:

GRAPHENE = {
    "SCHEMA": 
        "blog.schema.schema",
        "newsletter.schema.schema",
GRAPHENE = {
    "SCHEMA":[
        "blog.schema.schema",
        "newsletter.schema.schema",
    ],

...and other combinations which I can't remember, but they all either lead to errors or to just the last schema in the list to work.

1

There are 1 best solutions below

0
Daniel On

The solution is to make a third schema.py file in the same folder as your settings.py file. The new schema.py file should look like:

import graphene

import first_app.schema as first_app
import second_app.schema as second_app

class Query(first_app.schema.Query, second_app.schema.Query, graphene.ObjectType):
    # Inherits all classes and methods from app-specific queries, so no need
    # to include additional code here.
    pass

class Mutation(first_app.schema.Mutation, second_app.schema.Mutation, graphene.ObjectType):
    # Inherits all classes and methods from app-specific mutations, so no need
    # to include additional code here.
    pass

schema = graphene.Schema(query=Query, mutation=Mutation)

Your settings.py file should then reference the new third schema.py file:

GRAPHENE = {
    'SCHEMA': 'parent_folder.schema.schema'
}