質問編集履歴
4
エラーの部分の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,9 @@
|
|
24
24
|
どのあたりに問題があるかご教授いただきたいです。
|
25
25
|
|
26
26
|
### エラー
|
27
|
+
赤波線で`Cannot query field "createLikeFrom" on type "Mutation".`と出力される
|
27
|
-
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-13/
|
28
|
+
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-13/2c1a4138-c232-4fdc-b272-618b5a655927.png)
|
29
|
+
|
28
30
|
|
29
31
|
### 該当のソースコード
|
30
32
|
|
3
mutation_type.rbを一部修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,8 +100,7 @@
|
|
100
100
|
```ruby
|
101
101
|
module Types
|
102
102
|
class MutationType < Types::BaseObject
|
103
|
-
field :create_like_to, mutation: Mutations::CreateLikeTo
|
104
|
-
field :create
|
103
|
+
field :createLikeFrom, mutation: Mutations::CreateLikeFrom
|
105
104
|
end
|
106
105
|
end
|
107
106
|
```
|
2
クエリの内容を変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,10 +7,7 @@
|
|
7
7
|
```
|
8
8
|
mutation {
|
9
9
|
createLikeFrom(
|
10
|
-
input:{
|
11
|
-
user_id: 1
|
12
|
-
to
|
10
|
+
input:{userId: 1, toUserId: 3}
|
13
|
-
}
|
14
11
|
){
|
15
12
|
likes {
|
16
13
|
id
|
1
質問内容を一部修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,37 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
|
3
3
|
あるユーザーがどのユーザーに対してLikeをしたのか、その情報を保持させるLikeモデルをGraphQLで実装しています。
|
4
|
+
mutationでCreateLikeFromというメソッドをつくり、`user_id: 1`が`user_id: 3`をLikeする実装をしたいです。
|
4
|
-
現在の下記のコードでは、クエリを投げても
|
5
|
+
現在の下記のコードでは、クエリを投げてもエラーが出力されてしまいます。
|
6
|
+
- クエリ
|
7
|
+
```
|
8
|
+
mutation {
|
9
|
+
createLikeFrom(
|
10
|
+
input:{
|
11
|
+
user_id: 1
|
12
|
+
to_user_id: 3
|
13
|
+
}
|
14
|
+
){
|
15
|
+
likes {
|
16
|
+
id
|
17
|
+
fromUser {
|
18
|
+
id
|
19
|
+
}
|
20
|
+
toUser {
|
21
|
+
id
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
```
|
5
|
-
|
27
|
+
どのあたりに問題があるかご教授いただきたいです。
|
6
28
|
|
29
|
+
### エラー
|
7
|
-
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-
|
30
|
+
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-13/16c517df-d100-42e3-bcb9-25dee06fd935.png)
|
8
31
|
|
9
32
|
### 該当のソースコード
|
10
33
|
|
11
|
-
user.rb
|
34
|
+
models/user.rb
|
12
35
|
```ruby
|
13
36
|
class User < ApplicationRecord
|
14
37
|
has_many :likes_from, class_name: "Like", foreign_key: :from_user_id, dependent: :destroy
|
@@ -17,14 +40,14 @@
|
|
17
40
|
has_many :passive_likes, through: :likes_to, source: :from_user
|
18
41
|
end
|
19
42
|
```
|
20
|
-
like.rb
|
43
|
+
models/like.rb
|
21
44
|
```ruby
|
22
45
|
class Like < ApplicationRecord
|
23
46
|
belongs_to :to_user, class_name: "User", foreign_key: :to_user_id
|
24
47
|
belongs_to :from_user, class_name: "User", foreign_key: :from_user_id
|
25
48
|
end
|
26
49
|
```
|
27
|
-
user_type.rb
|
50
|
+
graphql/types/user_type.rb
|
28
51
|
```ruby
|
29
52
|
module Types
|
30
53
|
class UserType < Types::BaseObject
|
@@ -32,10 +55,12 @@
|
|
32
55
|
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
|
33
56
|
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
|
34
57
|
field :like, [Types::LikeType], null: false
|
58
|
+
def like_from; object.likes_from; end
|
59
|
+
def like_to; object.likes_to; end
|
35
60
|
end
|
36
61
|
end
|
37
62
|
```
|
38
|
-
like_type.rb
|
63
|
+
graphql/types/like_type.rb
|
39
64
|
```ruby
|
40
65
|
module Types
|
41
66
|
class LikeType < Types::BaseObject
|
@@ -45,10 +70,12 @@
|
|
45
70
|
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
|
46
71
|
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
|
47
72
|
field :user, Types::UserType, null: false
|
73
|
+
field :from_user, Types::UserType, null: false
|
74
|
+
field :to_user, Types::UserType, null: false
|
48
75
|
end
|
49
76
|
end
|
50
77
|
```
|
51
|
-
query_type.rb
|
78
|
+
graphql/types/query_type.rb
|
52
79
|
```ruby
|
53
80
|
module Types
|
54
81
|
class QueryType < Types::BaseObject
|
@@ -72,3 +99,40 @@
|
|
72
99
|
end
|
73
100
|
end
|
74
101
|
```
|
102
|
+
graphql/types/mutation_type.rb
|
103
|
+
```ruby
|
104
|
+
module Types
|
105
|
+
class MutationType < Types::BaseObject
|
106
|
+
field :create_like_to, mutation: Mutations::CreateLikeTo
|
107
|
+
field :create_like_from, mutation: Mutations::CreateLikeFrom
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
graphql/mutations/create_like_from.rb
|
112
|
+
```ruby
|
113
|
+
module Mutations
|
114
|
+
class CreateLikeFrom < BaseMutation
|
115
|
+
graphql_name 'CreateLikeFrom'
|
116
|
+
|
117
|
+
field :user, Types::UserType, null: false
|
118
|
+
field :like, Types::LikeType, null: false
|
119
|
+
|
120
|
+
argument :id, ID, required: true
|
121
|
+
argument :user_id, ID, required: true
|
122
|
+
argument :from_user_id, ID, required: true
|
123
|
+
argument :to_user_id, ID, required: true
|
124
|
+
argument :from_user, Integer, required: true
|
125
|
+
argument :to_user, Integer, required: true
|
126
|
+
|
127
|
+
def resolve(**args)
|
128
|
+
user = User.find(args[:user_id])
|
129
|
+
user.likes_from.create!(to_user_id: to_user_id)
|
130
|
+
{
|
131
|
+
like: like,
|
132
|
+
}
|
133
|
+
rescue => e
|
134
|
+
GraphQL::ExecutionError.new(e.message)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|