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">
#!/usr/bin/env python
import ariadne
import ariadne
from ariadne import asgi


TASKS = [
TASKS = [
Line 11: Line 9:
]
]


# Your GraphQL SDL Schema
type_defs = ariadne.gql("""
type_defs = ariadne.gql("""
     type Query {
     type Query {
Line 23: Line 20:
""")
""")


# Resolvers for Query 'hello'
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
# Build Schema, and create app
schema = ariadne.make_executable_schema(type_defs, query)
graphql_app = asgi.GraphQL(schema, debug=True)
</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.