OSDN Git Service

Regular updates
[twpd/master.git] / absinthe.md
1 ---
2 title: Absinthe
3 category: Hidden
4 layout: 2017/sheet
5 tags: [WIP]
6 updated: 2017-10-10
7 intro: |
8   [Absinthe](http://absinthe-graphql.org/) allows you to write GraphQL servers in Elixir.
9 ---
10
11 ## Introduction
12
13 ### Concepts
14
15 - `Schema` - The root. Defines what queries you can do, and what types they return.
16 - `Resolver` - Functions that return data.
17 - `Type` - A type definition describing the shape of the data you'll return.
18
19 ### Plug
20
21 #### web/router.ex
22
23 ```elixir
24 defmodule Blog.Web.Router do
25   use Phoenix.Router
26
27   forward "/", Absinthe.Plug,
28     schema: Blog.Schema
29 end
30 ```
31 {: data-line="4,5"}
32
33 Absinthe is a Plug, and you pass it one **Schema**.
34
35 See: [Our first query](http://absinthe-graphql.org/tutorial/our-first-query/)
36
37 ## Main concepts
38 {: .-three-column}
39
40 ### Schema
41
42 #### web/schema.ex
43
44 ```elixir
45 defmodule Blog.Schema do
46   use Absinthe.Schema
47   import_types Blog.Schema.Types
48
49   query do
50     @desc "Get a list of blog posts"
51     field :posts, list_of(:post) do
52       resolve &Blog.PostResolver.all/2
53     end
54   end
55 end
56 ```
57 {: data-line="5,6,7,8,9,10"}
58
59 This schema will account for `{ posts { ··· } }`. It returns a **Type** of `:post`, and delegates to a **Resolver**.
60
61 ### Resolver
62
63 #### web/resolvers/post_resolver.ex
64
65 ```elixir
66 defmodule Blog.PostResolver do
67   def all(_args, _info) do
68     {:ok, Blog.Repo.all(Blog.Post)}
69   end
70 end
71 ```
72 {: data-line="3"}
73
74 This is the function that the schema delegated the `posts` query to.
75
76 ### Type
77
78 #### web/schema/types.ex
79
80 ```elixir
81 defmodule Blog.Schema.Types do
82   use Absinthe.Schema.Notation
83
84   @desc "A blog post"
85   object :post do
86     field :id, :id
87     field :title, :string
88     field :body, :string
89   end
90 end
91 ```
92 {: data-line="4,5,6,7,8,9"}
93
94 This defines a type `:post`, which is used by the resolver.
95
96 ## Schema
97
98 ### Query arguments
99
100 #### GraphQL query
101
102 ```
103 { user(id: "1") { ··· } }
104 ```
105
106 #### web/schema.ex
107
108 ```elixir
109 query do
110   field :user, type: :user do
111     arg :id, non_null(:id)
112     resolve &Blog.UserResolver.find/2
113   end
114 end
115 ```
116 {: data-line="3"}
117
118 #### Resolver
119
120 ```elixir
121 def find(%{id: id} = args, _info) do
122   ···
123 end
124 ```
125 {: data-line="1"}
126
127 See: [Query arguments](http://absinthe-graphql.org/tutorial/query-arguments/)
128
129 ### Mutations
130
131 #### GraphQL query
132
133 ```
134 {
135   mutation CreatePost {
136     post(title: "Hello") { id }
137   }
138 }
139 ```
140
141 #### web/schema.ex
142
143 ```elixir
144 mutation do
145   @desc "Create a post"
146   field :post, type: :post do
147     arg :title, non_null(:string)
148     resolve &Blog.PostResolver.create/2
149   end
150 end
151 ```
152 {: data-line="1"}
153
154 See: [Mutations](http://absinthe-graphql.org/tutorial/mutations/)
155
156 ## References
157
158   - [Absinthe website](http://absinthe-graphql.org/) _(absinthe-graphql.org)_
159   - [GraphQL cheatsheet](./graphql) _(devhints.io)_