実現したいこと
あるユーザーがどのユーザーに対してLikeをしたのか、その情報を保持させるLikeモデルをGraphQLで実装しています。
mutationでCreateLikeFromというメソッドをつくり、user_id: 1
がuser_id: 3
をLikeする実装をしたいです。
現在の下記のコードでは、クエリを投げてもエラーが出力されてしまいます。
- クエリ
mutation { createLikeFrom( input:{userId: 1, toUserId: 3} ){ likes { id fromUser { id } toUser { id } } } }
どのあたりに問題があるかご教授いただきたいです。
エラー
赤波線でCannot query field "createLikeFrom" on type "Mutation".
と出力される
該当のソースコード
models/user.rb
ruby
1class User < ApplicationRecord 2 has_many :likes_from, class_name: "Like", foreign_key: :from_user_id, dependent: :destroy 3 has_many :likes_to, class_name: "Like", foreign_key: :to_user_id, dependent: :destroy 4 has_many :active_likes, through: :likes_from, source: :to_user 5 has_many :passive_likes, through: :likes_to, source: :from_user 6end
models/like.rb
ruby
1class Like < ApplicationRecord 2 belongs_to :to_user, class_name: "User", foreign_key: :to_user_id 3 belongs_to :from_user, class_name: "User", foreign_key: :from_user_id 4end
graphql/types/user_type.rb
ruby
1module Types 2 class UserType < Types::BaseObject 3 field :id, ID, null: false 4 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 5 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 6 field :like, [Types::LikeType], null: false 7 def like_from; object.likes_from; end 8 def like_to; object.likes_to; end 9 end 10end
graphql/types/like_type.rb
ruby
1module Types 2 class LikeType < Types::BaseObject 3 field :id, ID, null: false 4 field :from_user_id, Integer, null: false 5 field :to_user_id, Integer, null: false 6 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 7 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 8 field :user, Types::UserType, null: false 9 field :from_user, Types::UserType, null: false 10 field :to_user, Types::UserType, null: false 11 end 12end
graphql/types/query_type.rb
ruby
1module Types 2 class QueryType < Types::BaseObject 3 field :like, Types::LikeType, null: false do 4 description 'いいね情報(id指定で1件)' 5 argument :id, ID, required: true 6 end 7 8 def like(id:) 9 Like.find(id) 10 end 11 12 field :likes, [Types::LikeType], null: false do 13 description 'いいね情報(全件)' 14 end 15 16 def likes 17 Like.all 18 end 19 20 end 21end
graphql/types/mutation_type.rb
ruby
1module Types 2 class MutationType < Types::BaseObject 3 field :createLikeFrom, mutation: Mutations::CreateLikeFrom 4 end 5end
graphql/mutations/create_like_from.rb
ruby
1module Mutations 2 class CreateLikeFrom < BaseMutation 3 graphql_name 'CreateLikeFrom' 4 5 field :user, Types::UserType, null: false 6 field :like, Types::LikeType, null: false 7 8 argument :id, ID, required: true 9 argument :user_id, ID, required: true 10 argument :from_user_id, ID, required: true 11 argument :to_user_id, ID, required: true 12 argument :from_user, Integer, required: true 13 argument :to_user, Integer, required: true 14 15 def resolve(**args) 16 user = User.find(args[:user_id]) 17 user.likes_from.create!(to_user_id: to_user_id) 18 { 19 like: like, 20 } 21 rescue => e 22 GraphQL::ExecutionError.new(e.message) 23 end 24 end 25end
あなたの回答
tips
プレビュー