用n-deep wrapper类型的栈获取模式。

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

我通过GraphQL从数据库中获取数据。 有两种类型。Group 和... Person. 组有一个领域 people 这是由 Person 对象。

我试图使用GraphQL内置的 自省. 我的问题是 people 字段是一个不可空的类型包装一个列表类型包装一个不可空的类型,我必须用这个啰嗦的查询。

{
    __type(name: "Group") {
        name
        fields {
            name
            type {
                name
                kind
                ofType {
                    kind
                    name
                    ofType {
                        kind
                        name
                        ofType {
                            kind
                            name
                            ofType {
                                kind
                                name
                            }
                        }
                    }
                }
            }
        }
    }
}

为了得到这个模式

{
    "data": {
        "__type": {
            "name": "Group",
            "fields": [
                {
                    "name": "people",
                    "type": {
                        "name": null,
                        "kind": "NON_NULL",
                        "ofType": {
                            "kind": "LIST",
                            "name": null,
                            "ofType": {
                                "kind": "NON_NULL",
                                "name": null,
                                "ofType": {
                                    "kind": "OBJECT",
                                    "name": "Person",
                                    "ofType": null
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

除了是一个不方便且难以阅读的查询之外,它也不是通用的,而且我必须知道模式中包装类型的最大深度才能构建它。

有什么办法可以得到一个模式中所有的包装器类型,不管深度如何?

graphql introspection graphql-schema
1个回答
1
投票

不幸的是,没有办法做到这一点。没有最大的深度,因为GraphQL支持用List任意地包装一个类型。所以通常你只会看到

[String!]!

这也是一种有效的类型

[[[[[[[[[[[[String]!]!]!]!]!]!]!]!]!]!]!]!

你几乎必须选择一个合理的深度,然后滚滚而来。对于它的价值。这个 是官方 "完整 "反省查询的样子。它的深度为7。

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