在浏览了 Graphql 的文档后,我开始在玩具导轨/reactJS 项目中实现它。这些项目允许用户通过设计登录,然后访问显示艺术家列表的虚拟 /artist 路径。在我尝试使用来自 react 应用程序的 GraphQL 的 api 数据来获取艺术家并显示它们之前,一切似乎都正常工作。
在服务器端,我有一个graphql_controller.rb如:
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
Run Code Online (Sandbox Code Playgroud)
然后,按照我的模型逻辑,我在 graph/ 下使用以下文件设置了 graphql:
图/查询/artist_query.rb
ArtistQuery = GraphQL::ObjectType.define do
name 'ArtistQuery'
description 'The query root for …Run Code Online (Sandbox Code Playgroud)