I'm rebuilding a former Django REST API project as a GraphQL one. I now have queries & mutations working properly.
Most of my learning came from looking at existing Graphene-Django & Graphene-Python code samples. There seem to be a lot of inconsistencies amongst them.
In some it was suggested that the GraphQL queries should be placed in schema.py
whereas the mutations should be placed in mutation.py
.
What I'm thinking makes more sense is to instead have these two files hold their respective code: - queries.py - mutations.py
I'm relatively new to Django & Python though so want to be sure that I'm not violating any conventions.
Interested in your thoughts!
Robert
I liked nik_m's answer so much I wrote some code to generate the template structure from inside the Django shell. I want to enforce some consistency as I create these files over and over again. I'm putting the code here in case someone else finds it useful.
From inside the Django shell, run like
schema_setup("my_app")
Note:
PROJECT_DIR
in your settings likePROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
from my_app.schema import MyAppQuery, MyAppMutation
There aren't any conventions yet, since GraphQL is a fairly new alternative method to REST. Thus, "conventions" are created at the moment we speak.
However, since
schema
is general-defined term you may rename it toqueries
.This is my project structure:
So the
schema.py
(__init__
) could be renamed toqueries.py
if you like. There is no much big difference between these two words.