如何在graphql中使用Rails Helper?

问题描述

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

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不起作用。

解决方法

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