简体   繁体   English

类型上不存在 Graphql 字段

[英]Graphql Field doesn't exist on type

After skimming through the docs of Graphql I've started to implement it on a toy rails\/reactJS project.在浏览了 Graphql 的文档后,我开始在一个玩具 rails\/reactJS 项目上实现它。 The projects allows an user to sign in through devise then access a dummy \/artist route that displays a list of artists.这些项目允许用户通过设计登录,然后访问显示艺术家列表的虚拟\/艺术家路线。 Everything seems to work fine until I try to consume api data with GraphQL from the react app to get artists and display them.一切似乎都很好,直到我尝试使用来自 react 应用程序的 GraphQL 来使用 api 数据来获取艺术家并显示它们。

On the server side, I have a graphql_controller.rb<\/strong> such as:在服务器端,我有一个graphql_controller.rb<\/strong>例如:

class GraphqlController < ApiController
  rescue_from Errors::Unauthorized do |exception|
    render json: {errors: ['Unauthorized.']}, status: :unauthorized
  end

  def index
    result = Schema.execute params[:query], variables: params[:variables], context: context
    render json: result, status: result['errors'] ? 422 : 200
  end

private

  def context
    token, options = ActionController::HttpAuthentication::Token.token_and_options request
    {
      ip_address: request.remote_ip
    }
  end
end

I don't think this is the issue for this particular case, but I was getting this error and it turned out to be due to a simple syntax error.我不认为这是这个特殊情况的问题,但我收到了这个错误,结果是由于一个简单的语法错误。 Query attributes need to be in camelCase format, not under_score format.查询属性需要是驼峰格式,而不是 under_score 格式。 Maybe this will help someone else that lands here when searching for this error like I did.也许这会帮助其他人在像我一样搜索此错误时降落在这里。

The GQL query you sent is malformed, it is asking for the query field of Query root object.您发送的 GQL 查询格式错误,它要求Query根对象的query字段。 Use this instead:改用这个:

const artistListQuery = gql`
    query UseTheNameYouWantHere {
        artists {
            id
            name
            description     
        }
    }
`;

BTW, you can add graphiql gem ( https://github.com/rmosolgo/graphiql-rails ) to have a playground with your GraphQL API.顺便说一句,您可以添加graphiql gem ( https://github.com/rmosolgo/graphiql-rails ) 来为您的 GraphQL API 提供一个游乐场。

All right, the problem is not in the code itself.好吧,问题不在于代码本身。 I changed the query to the one you adviced and updated the name artists to something else on both client and server side.我将查询更改为您建议的查询,并将名称艺术家更新为客户端和服务器端的其他内容。 It seems like apollo-rails is caching previous api calls, even if they failed, that why I was always getting the same error.似乎 apollo-rails 正在缓存以前的 api 调用,即使它们失败了,这也是为什么我总是遇到相同的错误。 I still have to figure out how to clean this cache.我仍然需要弄清楚如何清理这个缓存。 Thanks for your time.谢谢你的时间。

I had this error in RSpec , but not in a real call, ie Postman, for the same query.我在RSpec 中有这个错误,但不是在真正的调用中,即邮递员,对于相同的查询。 The solution was to change the query in spec from解决方案是将规范中的查询从

<<~QUERY
  query users {
    posts {
      ...
    }
  }
QUERY

to

<<~QUERY
  query users {
    users {
      posts {
        ...
      }
    }
  }
QUERY

However in other specs, the first approach was working with similarly looking mutation, so I am not sure what was wrong但是在其他规范中,第一种方法是使用类似的突变,所以我不确定出了什么问题

In my case the issue was that I had a structure like this:就我而言,问题是我有一个这样的结构:

module Types
  class SomeType < Types::BaseObject
    field :comparator,
          Types::ComparatorType
    field :options,
          [Types::OptionType]
  end
end

BUT in the query I had other nested structure called data that I forgot about:但是在查询中,我有其他名为data 的嵌套结构,但我忘记了:

mutation {
  myMutation(
    ...  
  ) {
    someType {
      comparator {
        ...
      }
      data {
        options {
           ...
        }
      }
    }
  }
}

So after changing SomeType class to add that missing key solved my issue.因此,在更改SomeType类以添加缺少的密钥后,解决了我的问题。 So now it look like this:所以现在它看起来像这样:

module Types
  class SomeType < Types::BaseObject
    field :comparator,
          Types::ComparatorType
    field :data,
          Types::DataType
  end
end

# New file
module Types
  class DataType < Types::BaseObject
    field :options,
          [Types::OptionType]
  end
end

You might need to use a newer API endpoint URL version.您可能需要使用更新的 API 端点 URL 版本。

Example Request URL: https:\/\/shopify-graphiql-app.shopifycloud.com\/api\/*2022-01*\/graphql<\/a>示例请求 URL: https<\/a> :\/\/shopify-graphiql-app.shopifycloud.com\/api\/*2022-01*\/graphql

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM