Object types

Object types are the fundamentals of any GraphQL schema, the are used to defined the kind of objects that exist in a schema. Object types are created by defining a name and a list of fields, here’s an example object type defined using the GraphQL schema language:

type Character {
name: String!
age: Int!
}

A note on Query, Mutation and Subscription

While reading about GraphQL you might have encountered 3 special object types: Query, Mutation and Subscription. They are defined as standard object types, with the difference that they are also used as entry point for your schema (also referred as root types).

  • Query is the entry point for all the query operations
  • Mutation is the entry point for all the mutations
  • Subscription is the entry point for all the subscriptions.

. TODO: add a link to docs on defining a schema.

Defining object types

In Strawberry, you can define object types by using the @strawberry.type decorator, like this:

Python
import strawberry
@strawberry.type
class Character:
name: str
age: int
Schema
type Character {
name: String!
age: int!
}

You can also refer to other types, like this:

Python
import strawberry
@strawberry.type
class Character:
name: str
age: int
@strawberry.type
class Book:
title: str
main_character: Character
Schema
type Character {
name: String!
age: Int!
}
type Book {
title: String!
mainCharacter: Character!
}

API

@strawberry.type(name: str = None, description: str = None)

Creates an object type from a class definition.

name: if set this will be the GraphQL name, otherwise the GraphQL will be generated by camel-casing the name of the class.

description: this is the GraphQL description that will be returned when introspecting the schema or when navigating the schema using GraphiQL.

Was this helpful?

Edit on Github

Newsletter πŸ’Œ

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