在Ruby中定义嵌套的GraphQL对象

时间:2018-06-29 04:43:10

标签: ruby graphql

在Ruby中,我可以使用字段定义GraphQL对象

public class Appsingleton {
    // put Log func on Constructor 
    public Appsingleton() {
        Log.i("tag", "my message");
    }

    // put LOg on method
    public static void showLog(String message){
        Log.i("tag", message);
    }
}

但是如何创建多层嵌套的GraphQL对象?

我不确定如何解释documentation GraphqlObjectType = GraphQL::ObjectType.define do field :metadata end 的定义吗?

我想以一个结构结束

types[PersonType]

在哪里/如何定义嵌套的解析器?

metadata: {
    form_issues: {
        person: {
        ...
        }
    }
}

1 个答案:

答案 0 :(得分:1)

 field :director, PersonType
  field :cast, CastType
  field :starring, types[PersonType] do
    argument :limit, types.Int
    resolve ->(object, args, ctx) {
      stars = object.cast.stars
      args[:limit] && stars = stars.limit(args[:limit])
      stars
    }
   end

假设您已定义PersonType,则field :director, PersonType引用该类型的单个实例。 field :starring, types[PersonType]的意思是“ PersonType的数组”。这只是graphql-ruby表示此概念的方式,类似于!,表示“必需/非null”。

PersonType # => person, can be null.
!PersonType # non-null person.
types[PersonType] # array of people, can be null itself, the array. People inside can be null too.
!types[PersonType] # array can't be null, but can contain nulls.
!types[!PersonType] # array can't be null and can't contain nulls.

因此您的架构可以如下所示:

field :metadata, MetadataType

# MetadataType
field :form_issues, FormIssuesType # why is this not an array, as the name suggests?
# field :form_issues, types[FormIssueType] # use this for an array

# FormIssuesType / FormIssueType
field :person, PersonType

在适当的地方应用“非空”修饰符。