File: object-types.md

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (103 lines) | stat: -rw-r--r-- 2,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
title: Object types
---

# Object types

Object types are the fundamentals of any GraphQL schema, they are used to define
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:

```graphql
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 points 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.

For a walk-through on how to define schemas, read the
[schema basics](../general/schema-basics.md).

## Defining object types

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

<CodeGrid>

```python
import strawberry


@strawberry.type
class Character:
    name: str
    age: int
```

```graphql
type Character {
  name: String!
  age: int!
}
```

</CodeGrid>

You can also refer to other types, like this:

<CodeGrid>

```python
import strawberry


@strawberry.type
class Character:
    name: str
    age: int


@strawberry.type
class Book:
    title: str
    main_character: Character
```

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

type Book {
  title: String!
  mainCharacter: Character!
}
```

</CodeGrid>

## 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.