【问题标题】:How to use rails Helpers in graphql?如何在graphql中使用rails Helpers?
【发布时间】:2020-08-14 09:38:58
【问题描述】:

如何在我的 /app/graphql 目录中使用 /app/helpers 目录中的 Helpers?例如,有一个数据类型是嵌套的 JSON 对象,我得到了一个 JSON Schema 文件来描述它的结构。还有一个 JsonSchemaHelper,我想用它来针对 JSON 模式验证标量类型。像这样:

class Types::Scalar::Node < Types::Base::BaseScalar
  def self.coerce_input(value, _context)
    if Validators::GraphqlValidator.is_parsable_json?(value)
      value = JSON.parse(value)
    end
    Validators::Node.validate!(value)
    value
  end

  #Validators could be used to check if it fit the client-side declared type
  def self.coerce_result(value, _context)
    Validators::Node.validate!(value)
    value
  end
end

验证器看起来像:

module Validators
  class Node
    include JsonSchemaHelper
    def self.validate!(ast)
      json_schema_validate('Node', ast)
    end
  end
end

include JsonSchemaHelper 不起作用。

【问题讨论】:

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


    【解决方案1】:

    includeJsonSchemaHelper 的方法添加为Validators::Node 类的实例方法。 self.validate!(ast) 是类方法,您尝试将 json_schema_validate 作为类方法调用。将include JsonSchemaHelper 更改为extend JsonSchemaHelper

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多