Graphql字段在类型上不存在 [英] Graphql Field doesn't exist on type
问题描述
浏览了Graphql的文档后,我开始在玩具rails/reactJS项目中实现它.这些项目允许用户通过设计登录,然后访问显示艺术家列表的虚拟/艺术家路线.直到我尝试使用来自React应用程序的GraphQL使用api数据以获取艺术家并显示它们为止,一切似乎都可以正常工作.
After skimming through the docs of Graphql I've started to implement it on a toy rails/reactJS project. 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.
在服务器端,我有一个 graphql_controller.rb ,例如:
On the server side, I have a graphql_controller.rb such as:
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
然后,按照我的模型逻辑,我在graph/下设置了带有以下文件的graphql:
Then, following to my model logic, I have set up graphql under graph/ with the following files:
图/查询/artist_query.rb
ArtistQuery = GraphQL::ObjectType.define do
name 'ArtistQuery'
description 'The query root for this schema'
field :artists, types[Types::ArtistType] do
resolve(->(_, _, _) {
Artist.all
})
end
end
types/artist_type.rb
Types::ArtistType = GraphQL::ObjectType.define do
name 'Artist'
description 'A single artist.'
field :id, !types.ID
field :name, types.String
field :description, types.String
end
schema.rb
Schema = GraphQL::Schema.define do
query ArtistQuery
end
在客户端,为了使内容井井有条,我使用了3个文件来绘制此艺术家列表:
On the client side, for the sake of keeping things organized, I use 3 files to render this artist list:
首先, ArtistSchema.js
import { gql } from 'react-apollo';
const artistListQuery = gql`
{
query {
artists {
id
name
description
}
}
}
`;
export default artistListQuery;
然后是一个 Artist.js
Then, an Artist.js
import React, { Component } from 'react';
class Artist extends Component {
render() {
return (
<tr>
<td>{this.props.index + 1}</td>
<td>{this.props.data.name}</td>
<td>{this.props.data.description} min</td>
</tr>
);
}
}
export default Artist;
最后,将两者包装成更大的布局: Artists.jsx :
And finally, wrapping these two together in a larger layout: Artists.jsx:
import React, { Component } from 'react';
import {graphql} from 'react-apollo';
import Artist from './Artist';
import artistListQuery from './ArtistSchema';
class Artists extends Component {
render() {
if(this.props.data.loading) {
return (<p>Loading</p>)
} else {
console.log(this.props.data)
const ArtistsItems = this.props.data.artists.map((data,i) => {
return (<Artist key={i} index={i} data={data}></Artist>);
});
return (
<div>
<h1>Artists</h1>
<table className="table table-striped table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{ ArtistsItems }
</tbody>
</table>
</div>
);
}
}
}
export default graphql(artistListQuery)(Artists);
执行此代码后会发生什么:
What happens when this code is executed:
在服务器端(对未格式化的输出很抱歉,但它在控制台中显示如下):
On server-side (sorry for the unformatted output, but it displays like this in console):
Processing by GraphqlController#index as */*
18:49:46 api.1 | Parameters: {"query"=>"{\n query {\n artists {\n id\n name\n description\n __typename\n }\n __typename\n }\n}\n", "operationName"=>nil, "graphql"=>{"query"=>"{\n query {\n artists {\n id\n name\n description\n __typename\n }\n __typename\n }\n}\n", "operationName"=>nil}}
随后出现错误:
Completed 422 Unprocessable Entity in 36ms (Views: 0.2ms | ActiveRecord: 0.0ms)
在客户端,如果我监视网络"> graphql的响应,我(当然)会收到422错误代码和以下错误消息:
On the client side, if I monitor Network > Response for graphql, I (of course) receive a 422 error code and the following error message:
{"errors":[{"message":"Field 'query' doesn't exist on type 'ArtistQuery'","locations":[{"line":2,"column":3}],"fields":["query","query"]}]}
我认为查询未正确完成.我一直在尝试各种查询格式(来自docs或gists示例),但是我无法结束寻找正确的方式来获取我的艺术家数据的方法.
I assume my query is not done correctly. I have been trying various queries formats (from docs or gists examples) but I cannot end finding a correct way to get back my artist data.
我在做什么错了?
推荐答案
对于这种特殊情况,我不认为这是问题,但是我遇到了此错误,事实证明这是由于简单的语法错误引起的.查询属性必须采用camelCase格式,而不是under_score格式.也许这将有助于像我一样在搜索此错误时落入此处的其他人.
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. Maybe this will help someone else that lands here when searching for this error like I did.
这篇关于Graphql字段在类型上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!