Replies: 2 comments
-
I just realized if I replace, |
Beta Was this translation helpful? Give feedback.
0 replies
-
How about this: class UserType(ObjectType):
__schema__ = gql(
"""
type User {
posts: [Post]!
}
"""
)
__requires__ = [PostType] # PostType TBD
def resolve_posts(root, *_):
return list(
{"id": id} | post
for id, post in posts.items()
if post["user_id"] == root["id"]
)
class Query(ObjectType):
__schema__ = gql(
"""
type Query {
user: User!
users: [User]!
}
"""
)
__requires__ = [UserType]
@staticmethod
def resolve_user(*_, id: int):
if id in users:
return {"id": id, "username": users[id]}
@staticmethod
def resolve_users(*_):
return [{"id": id, "username": username} for id, username in users.items()] This is already possible with /~https://github.com/mirumee/ariadne-graphql-modules |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was using "Strawberry" for GraphQL, which is "code-first". Then I tried Ariadne, and I liked the fact it was "schema-first", but got bogged down in no longer being able to map resolver functions declaratively.
I eventually came up with this simple function:
Then I only need to write some nested classes with a decorator, (in this example, "users" is a dictionary of usernames, "posts" is a list of comments per user:
Then I can create my app with:
Is this approach useful for others? I don't have to remember all those types and function calls. The argument to
bind
is the path to a schema file, but you can also do "type_defs=gql(...)" to use GraphQL inline as a string.Beta Was this translation helpful? Give feedback.
All reactions