This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/graphql/resolvers/create_movie.rb | |
class Resolvers::CreateMovie < GraphQL::Function | |
# arguments passed as "args" | |
argument :name, !types.String | |
argument :year, types.String | |
argument :genre, types.String | |
# return type from the mutation | |
type Types::MovieType |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/assets/javascripts/cable/subscriptions/movies.coffee | |
App.cable.subscriptions.create { channel: "MoviesChannel" }, | |
received: (data) -> | |
@appendToMoviesList(data) | |
appendToMoviesList: (data) -> | |
$('.movies-list').append( @getHtml(data) ) | |
getHtml: (data) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/channels/movies_channel.rb | |
class MoviesChannel < ApplicationCable::Channel | |
# Called when any consumer subscribes to this channel. | |
def subscribed | |
stream_from "movies" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ol class="movies-list"> | |
<% @movies.each do |movie| %> | |
<li> | |
<div> | |
<span><%= movie.name %></span> | |
</div> | |
</li> | |
<% end %> | |
</ol> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoviesController < ApplicationController | |
def index | |
@movies = Movie.all | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Resolvers::CreateMovie < GraphQL::Function | |
# arguments passed as "args" | |
argument :name, !types.String | |
argument :year, types.String | |
argument :genre, types.String | |
# return type from the mutation | |
type Types::MovieType | |
# the mutation method | |
def call(_obj, args, _ctx) | |
movie_params = { name: args[:name], year: args[:year], genre: args[:genre] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
field :create_movie, | |
description: "Create new movie", | |
function: Resolvers::CreateMovie.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Types::MovieType = GraphQL::ObjectType.define do | |
name 'Movie' | |
description 'Movie Details' | |
field :id, !types.ID | |
field :name, !types.String | |
field :genre, types.String | |
field :year, types.String | |
field :actors do | |
type types[Types::ActorType] | |
description "Actor" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Types::ActorType = GraphQL::ObjectType.define do | |
name 'Actor' | |
description 'Actor Details' | |
field :id, !types.ID | |
field :name, !types.String | |
field :gender, types.String | |
field :date_of_birth, types.String | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
field :movies do | |
type types[Types::MovieType] | |
description "Return the first movie" | |
resolve ->(obj, args, ctx) { Movie.all } | |
end |