This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
repositories_controller.rb
176 lines (158 loc) · 4.89 KB
/
repositories_controller.rb
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class RepositoriesController < ApplicationController
# Define query for repository listing.
#
# All queries MUST be assigned to constants and therefore be statically
# defined. Queries MUST NOT be generated at request time.
IndexQuery = GitHub::Client.parse <<-'GRAPHQL'
# All read requests are defined in a "query" operation
query {
# viewer is the currently authenticated User
viewer {
# "...FooConstant" is the fragment spread syntax to include the index
# view's fragment.
#
# "Views::Repositories::Index::Viewer" means the fragment is defined
# in app/views/repositories/index.html.erb and named Viewer.
...Views::Repositories::Index::Viewer
}
}
GRAPHQL
# GET /repositories
def index
# Use query helper defined in ApplicationController to execute the query.
# `query` returns a GraphQL::Client::QueryResult instance with accessors
# that map to the query structure.
data = query IndexQuery
# Render the app/views/repositories/index.html.erb template with our
# current User.
#
# Using explicit render calls with locals is preferred to implicit render
# with instance variables.
render "repositories/index", locals: {
viewer: data.viewer
}
end
# Define query for "Show more repositories..." AJAX action.
MoreQuery = GitHub::Client.parse <<-'GRAPHQL'
# This query uses variables to accept an "after" param to load the next
# 10 repositories.
query($after: String!) {
viewer {
repositories(first: 10, after: $after) {
# Instead of refetching all of the index page's data, we only need
# the data for the repositories container partial.
...Views::Repositories::Repositories::RepositoryConnection
}
}
}
GRAPHQL
# GET /repositories/more?after=CURSOR
def more
# Execute the MoreQuery passing along data from params to the query.
data = query MoreQuery, after: params[:after]
# Using an explicit render again, just render the repositories list partial
# and return it to the client.
render partial: "repositories/repositories", locals: {
repositories: data.viewer.repositories
}
end
# Define query for repository show page.
ShowQuery = GitHub::Client.parse <<-'GRAPHQL'
# Query is parameterized by a $id variable.
query($id: ID!) {
# Use global id Node lookup
node(id: $id) {
# Include fragment for app/views/repositories/show.html.erb
...Views::Repositories::Show::Repository
}
}
GRAPHQL
# GET /repositories/ID
def show
# Though we've only defined part of the ShowQuery in the controller, when
# query(ShowQuery) is executed, we're sending along the query as well as
# all of its fragment dependencies to the API server.
#
# Here's the raw query that's actually being sent.
#
# query RepositoriesController__ShowQuery($id: ID!) {
# node(id: $id) {
# ...Views__Repositories__Show__Repository
# }
# }
#
# fragment Views__Repositories__Show__Repository on Repository {
# id
# owner {
# login
# }
# name
# description
# homepageUrl
# ...Views__Repositories__Navigation__Repository
# }
#
# fragment Views__Repositories__Navigation__Repository on Repository {
# hasIssuesEnabled
# }
data = query ShowQuery, id: params[:id]
if repository = data.node
render "repositories/show", locals: {
repository: repository
}
else
# If node can't be found, 404. This may happen if the repository doesn't
# exist, we don't have permission or we used a global ID that was the
# wrong type.
head :not_found
end
end
StarMutation = GitHub::Client.parse <<-'GRAPHQL'
mutation($id: ID!) {
star(input: { starrableId: $id }) {
starrable {
...Views::Repositories::Star::Repository
}
}
}
GRAPHQL
def star
data = query StarMutation, id: params[:id]
if repository = data.star
respond_to do |format|
format.js {
render partial: "repositories/star", locals: { repository: data.star.starrable }
}
format.html {
redirect_to "/repositories"
}
end
else
head :not_found
end
end
UnstarMutation = GitHub::Client.parse <<-'GRAPHQL'
mutation($id: ID!) {
unstar(input: { starrableId: $id }) {
starrable {
...Views::Repositories::Star::Repository
}
}
}
GRAPHQL
def unstar
data = query UnstarMutation, id: params[:id]
if repository = data.unstar
respond_to do |format|
format.js {
render partial: "repositories/star", locals: { repository: data.unstar.starrable }
}
format.html {
redirect_to "/repositories"
}
end
else
head :not_found
end
end
end