File: root_types.md

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download | duplicates (3)
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
---
layout: guide
doc_stub: false
search: true
section: Schema
title: Root Types
desc: Root types are the entry points for queries, mutations and subscriptions.
index: 2
---

GraphQL queries begin from [root types](https://graphql.org/learn/schema/#the-query-and-mutation-types): `query`, `mutation`, and `subscription`.

Attach these to your schema using methods with the same name:

```ruby
class MySchema < GraphQL::Schema
  # required
  query Types::QueryType
  # optional
  mutation Types::MutationType
  subscription Types::SubscriptionType
end
```

The types are `GraphQL::Schema::Object` classes, for example:

```ruby
# app/graphql/types/query_type.rb
class Types::QueryType < GraphQL::Schema::Object
  field :posts, [PostType], 'Returns all blog posts', null: false
end

# Similarly:
class Types::MutationType < GraphQL::Schema::Object
  field :create_post, mutation: Mutations::AddPost
end
# and
class Types::SubscriptionType < GraphQL::Schema::Object
  field :comment_added, subscription: Subscriptions::CommentAdded
end
```

Each type is the entry point for the corresponding GraphQL query:

```ruby
query Posts {
  # `Query.posts`
  posts { ... }
}

mutation AddPost($postAttrs: PostInput!){
  # `Mutation.createPost`
  createPost(attrs: $postAttrs)
}

subscription CommentAdded {
  # `Subscription.commentAdded`
  commentAdded(postId: 1)
}
```