-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathschema.ex
More file actions
71 lines (53 loc) · 1.38 KB
/
schema.ex
File metadata and controls
71 lines (53 loc) · 1.38 KB
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
defmodule BlogWeb.Schema do
use Absinthe.Schema
import_types Absinthe.Type.Custom
import_types BlogWeb.Schema.AccountTypes
import_types BlogWeb.Schema.ContentTypes
alias BlogWeb.Resolvers
alias Blog.Content
# =============
# Dataloader
def context(ctx) do
loader =
Dataloader.new()
|> Dataloader.add_source(Content, Content.data())
Map.put(ctx, :loader, loader)
end
def plugins do
[Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults()]
end
query do
@desc "Get all posts"
field :posts, list_of(:post) do
resolve &Resolvers.Content.list_posts/3
end
@desc "Get a user of the blog"
field :user, :user do
arg :id, non_null(:id)
resolve &Resolvers.Accounts.find_user/3
end
end
mutation do
@desc "Create a post"
field :create_post, :post do
arg :title, non_null(:string)
arg :body, non_null(:string)
arg :published_at, :naive_datetime
resolve &Resolvers.Content.create_post/3
end
@desc "Create a user"
field :create_user, :user do
arg :name, non_null(:string)
arg :contact, non_null(:contact_input)
arg :password, non_null(:string)
resolve &Resolvers.Accounts.create_user/3
end
end
subscription do
field :new_post, :post do
config fn _args, _info ->
{:ok, topic: "*"}
end
end
end
end