首页 > 解决方案 > GraphQL Rails 出错(GraphQL::ObjectType 无法定义“查询”)

问题描述

我正在尝试使用 Rails 创建使用 GraphQL,并且一直在遵循一些指南,其中最新的是:https ://medium.com/@UnicornAgency/you-should-be-using-graphql-a-ruby-介绍-9b1de3b001dd

我创建了一个简单的“电影”模型

我已经添加gem 'graphql'

Bundle Install

rails g graphql:install

Bundle Install

然后我更新query_type.rb为:

 Types::QueryType = GraphQL::ObjectType.define do
    name “Query”
    # Add root-level fields here.
    # They will be entry points for queries on your schema.
    field :allMovies do
      type types[Types::MovieType]
      description “A list of all the movies”

      resolve -> (obj, args, ctx) {
        Movie.all
      }
    end

    field :movie do
      type Types::MovieType
      description “Return a movie”
      argument :id, !types.ID
      resolve -> (obj, args, ctx) { Movie.find(args[:id]) }
    end   
end

movie_type.rb好像:

module Types
  class MovieType < Types::BaseObject
    name “Movie”

    field :id, !types.ID
    field :title, !types.String
    field :description, types.String
  end
end

但是,当我启动服务器并转到时,localhost:3000/graphql我收到以下消息。"GraphQL::ObjectType can't define '“Query”'"

我注意到的是,当我最初打开query_type.rb它时,它有以下代码,我在网上看到的所有教程都没有提及:

module Types
  class QueryType < Types::BaseObject
end
end

如果我在其中添加代码,则代码不起作用,如果我按照上面的方式完全替换,代码也不起作用。

有任何想法吗?

标签: ruby-on-railsgraphql

解决方案



推荐阅读