How to modularize resolvers file? #672
-
So for now I have been keeping every query and mutation resolver in one file and then import everything in the views.py like this: However I have been trying to create modules, so I pushed the initial Query initialization to base.py module:
And now in my other modules I import those query and mutation vars:
And after creating this directory with modules I tried importing query and mutation to view.py: This is my file strcture: api |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Your resolvers aren't getting registered because you need to import python modules in order for code in them to be executed. As for ideas how to organize your resolvers, I split them in python packages ( Example
from .query import query_type
from .thread import thread_type
from .user import user_type
types = [query_type, thread_type, user_type] In
And I aggregate those mutations in import .user_register
import .user_login
import .thread_post
import .thread_reply
import .thread_delete
mutations = [
user_register.mutation,
user_login.mutation,
thread_post.mutation,
thread_reply.mutation,
thread_delete.mutation,
] I can then create executable schema like this: from .mutations import mutations
from .types import types
type_defs = ...
schema = make_executable_schema(type_defs, types, mutations) |
Beta Was this translation helpful? Give feedback.
Your resolvers aren't getting registered because you need to import python modules in order for code in them to be executed.
As for ideas how to organize your resolvers, I split them in python packages (
types
,mutations
,scalars
) and I give each GraphQL type and mutation separate module. I'm then aggregating those resolvers in__init__.py
of each of packages, which I in turn import inschema
module and pass tomake_executable_schema
.Example
types
dir will contain files like this:query.py
thread.py
user.py
types/__init__.py
will look like this:In
mut…