【问题标题】:dry-rb_autoinject and GraphQLdry-rb_autoinject 和 GraphQL
【发布时间】:2018-10-25 09:31:24
【问题描述】:

我正在尝试制作一个 GraphQL API,但也通过dry-rb_autoinject 实现依赖注入。我设法通过控制器和上下文来做到这一点。这是我的测试 QueryType。

Types::QueryType = GraphQL::ObjectType.define do
  name "Query"
  # Add root-level fields here.
  # They will be entry points for queries on your schema.

  # TODO: remove me
  field :testField, types.String do
    description "An example field added by the generator"
    resolve ->(obj, args, ctx) {
     ctx[:services][:output_service].()
    }
  end
end

我只是在 graphql_controller 中做

class GraphqlController < ApplicationController
  include IMPORT[:output_service]
  def execute
   ...
      services: {
        output_service: output_service
      }
    ...

但是这个解决方案似乎不是很好,因为我导入了所有服务,即使是我不需要的当前字段。有没有更好的方法,也许不是通过上下文?

【问题讨论】:

    标签: ruby-on-rails ruby graphql graphql-ruby dry-rb


    【解决方案1】:

    显然,最好的解决方案是为每个根字段编写解析器函数并在那里注入,因为解析器函数是一个继承自 GraphQL::Function

    的类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2020-03-22
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      相关资源
      最近更新 更多