folder design when using ariadne in django

505 Views Asked by At

I am trying to explore ariadne in my django project. However, I am feeling extremely tough to create a folder structure as I don't see much example mainly focused on that. Also I did not find any single tutorial. Everywhere the same way is done that is having every code in schema.py.

Here is an example

from ariadne import QueryType, make_executable_schema

type_defs = """
    type Query {
        hello: String!
    }
"""

query = QueryType()


@query.field("hello")
def resolve_hello(*_):
    return "Hello world!"


schema = make_executable_schema(type_defs, query)

How would you design your folders in a large django application where there are say more than 10, 15 apps like accounts, products, reviews etc? If we use plain django then it already gives following structure

app_name
    views.py
    urls.py
    models.py

but if we want to use ariadne in django and consider each app's crud features how would you now design your project?

1

There are 1 best solutions below

1
On

I had this same question and ended up splitting my schema and resolvers among my Django apps Like this:

project
    app_1
       models.py
       resolvers.py
       schema.graphql
    app_2
       models.py
       resolvers.py
       schema.graphql # graphql types relevant to this app
    project
       wsgi.py
       urls.py
       settings.py
       graphql_config.py # here I tie together all my schemas and resolvers
       schema.py # this schema file has my root Query and Mutation types

In my above example, my graphq_config.py looks like this:

from ariadne import QueryType, make_executable_schema, load_schema_from_path, 
import app_1.resolvers
import app_2.resolvers

type_defs = [
    load_schema_from_path("project/schema.graphql"),
    load_schema_from_path("app_1/schema.graphql"),
    load_schema_from_path("app_2/schema.graphql"),
]

query = QueryType()
query.set_field("type_1", app_1.resolvers.type_1_resolver)
query.set_field("type_2", app_2.resolvers.type_2_resolver)

schema = make_executable_schema(type_defs, query)

Anyways, I wrote it up in more detail in a blog post here: https://perandrestromhaug.com/posts/guide-to-schema-first-graphql-with-django-and-ariadne/