“在仅API的Rails应用程序上使用GraphQL时,无法为GraphqlController找到'执行'动作”?

问题描述 投票:0回答:1

我已经在rmosolgo/graphql-ruby上发布了一个有关此问题的错误,但以防万一我可能做错了什么,我希望看看是否有人可以解决我的问题。

[创建仅API的Rails应用程序时,似乎Rails认为我的execute中的GraphqlController方法丢失了。

这是我的graphql_controller.rb文件:

class GraphqlController < ApplicationController
  # If accessing from outside this domain, nullify the session
  # This allows for outside API access while preventing CSRF attacks,
  # but you'll have to authenticate your user separately
  protect_from_forgery with: :null_session

  def execute
    variables = ensure_hash(params[:variables])
    query = params[:query]
    operation_name = params[:operationName]
    context = {
      # Query context goes here, for example:
      # current_user: current_user,
    }
    result = RailsApiGraphqlExecuteTestSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
    render json: result
  rescue => e
    raise e unless Rails.env.development?
    handle_error_in_development e
  end
# ... continues on
end

这是我的routes.rb文件:

Rails.application.routes.draw do
  post "/graphql", to: "graphql#execute"
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

当我运行rake routes时,我得到的是:

prompt> rake routes                                   
                               Prefix Verb   URI Pattern          Controller#Action
                              graphql POST   /graphql(.:format)   graphql#execute
... continues on

您应该能够通过以下命令行步骤来重现此内容:

rails new execute-test --api
cd execute-test
vim Gemfile # or open an editor and add "gem 'graphql'"
bundle
rake db:create
rails g graphql:install
rake routes # to test that the route exists
rails s

当您使用类似GraphiQL的应用程序并转到http://localhost:3000/graphql时,将出现以下错误:

Started POST "/graphql" for 127.0.0.1 at 2019-10-15 16:23:29 -0700
   (0.3ms)  SELECT sqlite_version(*)

AbstractController::ActionNotFound (The action 'execute' could not be found for GraphqlController):
... strack trace continues ...

也许我做错了什么?任何帮助将不胜感激。

ruby-on-rails graphql rails-api
1个回答
0
投票

execute方法中的这一行看起来像:

protect_from_forgery with: :null_session

是引起问题的原因。我将不得不对此进行更多研究。 +1,如果有人可以弄清楚为什么会发生,我什至会把答案标记为正确。

© www.soinside.com 2019 - 2024. All rights reserved.