Help us understand the problem. What is going on with this article?

【GraphQL】モデルとタイプの不一致によるエラー

More than 1 year has passed since last update.

1対多の関係のモデルのレコードを双方向から取得したい(GraphQLで)

エラー

下記のエラーが出る。(実際の業務でのエラー)

sample.json
{
  "data": null,
  "errors": [
    {
      "message": " Failed to implement TwSurveyAnswerSet.twSurveyAnswer, tried:\n\n        - `Types::TwSurveyAnswerSetType#tw_survey_answer`, which did not exist\n        - `TwSurveyAnswerSet#tw_survey_answer`, which did not exist\n        - Looking up hash key `:tw_survey_answer` or `\"tw_survey_answer\"` on `#<TwSurveyAnswerSet:0x00007f09add42200>`, but it wasn't a Hash\n\n        To implement this field, define one of the methods above (and check for typos)\n",
      "locations": [
        {
          "line": 10,
          "column": 5
        }
      ],
      "path": [
        "twSurveyAnswerSetResolver",
        0,
        "twSurveyAnswer"
      ],
      "extensions": {
        "code": "SERVICE_UNAVAILABLE"
      }
    }
  ]
}

エラーが起きた事例

image.png

リゾルバー

sample_resolver.rb
class Types::SampleType < Types::BaseObject
  field :id, ID, null: false
  field :sample_message, Types::SampleMessageType.connection_type, null: false, scope: false
end

モデル

sample.rb
class Sample < ApplicationRecord
  has_many :sample_messages, dependent: :destroy
end

モデルとタイプが一致していない
sample_messageとsample_messages

解決策

リゾルバー

sample_resolver.rb
class Types::SampleType < Types::BaseObject
  field :id, ID, null: false
  field :sample_messages, Types::SampleMessageType.connection_type, null: false, scope: false
end

モデル

sample.rb
class Sample < ApplicationRecord
  has_many :sample_messages, dependent: :destroy
end

モデルとタイプを一致させること!!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away