Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit 8c35324

Browse files
xuorigcjoudrey
authored andcommitted
Add Film Mutation
1 parent a71bfb0 commit 8c35324

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

app/controllers/graphql_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ class GraphqlController < ApplicationController
44
def execute
55
query_string = params[:query].to_s
66
variables = ensure_hash(params[:variables])
7-
context = {
8-
user: @user
9-
}
7+
context = { user: @user }
108

119
result = Graph::Schema.execute(query_string, variables: variables, context: context)
1210
render json: result

app/models/graph/mutations/.keep

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Graph
2+
module Mutations
3+
FilmRate = GraphQL::Relay::Mutation.define do
4+
name "FilmRate"
5+
6+
input_field :filmId, !types.ID
7+
input_field :rating, !types.Int
8+
9+
return_field :film, Graph::Types::Film
10+
11+
resolve ->(_, input, ctx) do
12+
# TODO auth error
13+
return unless user = ctx[:user]
14+
15+
# TODO user error
16+
return unless film = Film.find_by(id: input["filmId"])
17+
18+
rating = user.ratings.build(
19+
film: film,
20+
rating: input["rating"]
21+
)
22+
23+
if rating.save
24+
{
25+
film: film
26+
}
27+
else
28+
# TODO add user errors
29+
{
30+
film: film
31+
}
32+
end
33+
end
34+
end
35+
end
36+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Graph
2+
module Mutations
3+
Mutation = GraphQL::ObjectType.define do
4+
name "Mutation"
5+
field :filmRate, field: Graph::Mutations::FilmRate.field
6+
end
7+
end
8+
end

app/models/graph/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Graph
22
Schema = GraphQL::Schema.define do
33
query Graph::Types::Query
4+
mutation Graph::Mutations::Mutation
45

56
resolve_type ->(obj, ctx) do
67
Graph::Schema.types.values.find { |type| type.name == obj.class.name }

0 commit comments

Comments
 (0)