0.45.4

Docs

General

Types

Features

Concepts

Integrations

Operations

»Schema

Every GraphQL API has a schema and that is used to define all the functionalities for an API. A schema is defined by passing 3 object types: Query, Mutation and Subscription.

Mutation and Subscription are optional, meanwhile Query has to always be there.

This is an example of a schema defined using Strawberry:

import strawberry
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello World"
schema = strawberry.Schema(Query)

»API

strawberry.Schema(
query: Type,
mutation: Type = None,
subscription: Type = None
validate_queries: bool = True
)

Creates a GraphQL schema

  • query: The root query type
  • mutation: The root mutation type
  • subscription: The root subscription type

».execute() (async)

Executes a GraphQL operation against a schema (async)

execute(query, variable_values, context_value, root_value, operation_name, validate_queries)

Parameter nameTypeDefaultDescription
querystrN/AThe document to be executed
variable_valuesOptional[Dict[str, Any]]NoneThe variables for this operation
context_valueOptional[Any]NoneThe value of the context that will be passed down to resolvers
root_valueOptional[Any]NoneThe value for the root type that will passed down to root resolvers
operation_nameOptional[str]NoneThe name of the operation you want to execute, useful when sending a document with multiple operations
validate_queriesboolTrueThis flag allows to disable query validation

».execute_sync()

Executes a GraphQL operation against a schema

execute_sync(query, variable_values, context_value, root_value, operation_name, validate_queries)

Parameter nameTypeDefaultDescription
querystrN/AThe document to be executed
variable_valuesOptional[Dict[str, Any]]NoneThe variables for this operation
context_valueOptional[Any]NoneThe value of the context that will be passed down to resolvers
root_valueOptional[Any]NoneThe value for the root type that will passed down to root resolvers
operation_nameOptional[str]NoneThe name of the operation you want to execute, useful when sending a document with multiple operations
validate_queriesboolTrueThis flag allows to disable query validation

»Query validation

When creating a schema you can decide to disable the validation of the queries sent from clients. This can be useful to improve performances in some specific cases, for example when dealing with internal APIs where queries can be trusted.

⚠️ NOTE: make sure you understand the trade-offs of disabling validation, for example when asking for field that don't exist the GraphQL schema won't return any error, which is something that breaks the safety of having a typed schema.

Edit on Github

Newsletter 💌

Do you want to receive the latest updates on Strawberry? Subscribe to our newsletter!