Ariadne queries: Difference between revisions
From wikinotes
(Created page with "= Fields with Params = <blockquote> <syntaxhighlight lang="python"> #!/usr/bin/env python import ariadne from ariadne import asgi TASKS = [ {"id": "1", "name": "clean kit...") |
|||
Line 2: | Line 2: | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
import ariadne | import ariadne | ||
TASKS = [ | TASKS = [ | ||
Line 11: | Line 9: | ||
] | ] | ||
type_defs = ariadne.gql(""" | type_defs = ariadne.gql(""" | ||
type Query { | type Query { | ||
Line 23: | Line 20: | ||
""") | """) | ||
query = ariadne.QueryType() | query = ariadne.QueryType() | ||
@query.field("task") | @query.field("task") | ||
def resolve_task(parent, info, id): | def resolve_task(parent, info, id): # <-- 'id' param assigned if present in query | ||
for task in TASKS: | for task in TASKS: | ||
if task['id'] == id: | if task['id'] == id: | ||
return task | return task | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 13:57, 4 September 2021
Fields with Params
import ariadne TASKS = [ {"id": "1", "name": "clean kitchen"}, {"id": "2", "name": "clean bathroom"}, ] type_defs = ariadne.gql(""" type Query { task(id: ID!): Task! } type Task { id: ID! name: String! } """) query = ariadne.QueryType() @query.field("task") def resolve_task(parent, info, id): # <-- 'id' param assigned if present in query for task in TASKS: if task['id'] == id: return task{ task(id: "2") { name id } }GraphQL field params are passed as keyword arguments to the function bound to the field.