从石墨烯(graphql)突变返回用户定义的数据结构

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

我有一个突变,我想返回任意形状的数据,但是我很难想象如何通过graphene突变来做到这一点。

我想返回这样的数据:

{
    "foo": {
        "success": [
            {
                "id": "abc123",
                "goober": {
                    "success": [
                        {
                            "id": "goober-type-1",
                            "baz": "blahblahblah"
                        },
                        {
                            "id": "goober-type-2",
                            "baz": "blahblahblah"
                        }
                    ],
                    "failed": [],
                    "cancelled": []
                },
                "tronic": {
                    "success": [
                        {
                            "id": "tronic-type-1",
                            "baz": "blahblahblah"
                        },
                        {
                            "id": "tronic-type-2",
                            "baz": "blahblahblah"
                        }
                    ],
                    "failed": [],
                    "cancelled": []
                }
            }
        ],
        "failed": [
            {
                "id": "abc123",
                "goober": {
                    "success": [],
                    "failed": [
                        {
                            "id": "goober-type-3",
                            "baz": "blahblahblah"
                        }
                    ],
                    "cancelled": [
                        {
                            "id": "goober-type-4",
                            "baz": "blahblahblah"
                        }
                    ]
                },
                "tronic": {
                    "success": [],
                    "failed": [
                        {
                            "id": "tronic-type-4",
                            "baz": "blahblahblah"
                        }
                    ],
                    "cancelled": [
                        {
                            "id": "tronic-type-3",
                            "baz": "blahblahblah"
                        }
                    ]
                }
            }
        ]
    }
}

我的直觉是想以这种方式定义graphene.ObjectType

class Goober(graphene.ObjectType):
    id = graphene.String()
    baz = graphene.String()

class Tronic(graphene.ObjectType):
    id = graphene.String()
    baz = graphene.String()

class Foo(graphene.ObjectType):

    id = graphene.String()
    goober = graphene.ObjectType(
        success = graphene.List(Goober),
        failed = graphene.List(Goober),
        cancelled = graphene.List(Goober)
    )
    tronic = graphene.ObjectType(
        success = graphene.List(Tronic),
        failed = graphene.List(Tronic),
        cancelled = graphene.List(Tronic)
    )

class Results(graphene.ObjectType):
    foo = graphene.ObjectType(
        success=graphene.List(Foo),
        failed=graphene.List(Foo)
    )

但是我真的不能用这种方法走到哪里,感觉到我从根本上误会了一些东西。

我在其他领域取得了成功,返回了SQL对象和一些自定义对象,但没有像这样生成和返回相对复杂的嵌套数据。任何建议将不胜感激。

python graphql graphene-python
1个回答
1
投票

事实是,石墨烯文档缺乏很多。看一下这个复制数据的冗长但完整的示例:

import json
import graphene

# let's just prepare some data
SUCCESS = [{
    "id": "abc123",
    "goober": {
        "success": [
            {
                "id": "goober-type-1",
                "baz": "blahblahblah"
            },
            {
                "id": "goober-type-2",
                "baz": "blahblahblah"
            }
        ],
        "failed": [],
        "cancelled": []
    },
    "tronic": {
        "success": [
            {
                "id": "tronic-type-1",
                "baz": "blahblahblah"
            },
            {
                "id": "tronic-type-2",
                "baz": "blahblahblah"
            }
        ],
        "failed": [],
        "cancelled": []
    }
}]

FAILED = [{
    "id": "abc123",
    "goober": {
        "success": [],
        "failed": [
            {
                "id": "goober-type-3",
                "baz": "blahblahblah"
            }
        ],
        "cancelled": [
            {
                "id": "goober-type-4",
                "baz": "blahblahblah"
            }
        ]
    },
    "tronic": {
        "success": [],
        "failed": [
            {
                "id": "tronic-type-4",
                "baz": "blahblahblah"
            }
        ],
        "cancelled": [
            {
                "id": "tronic-type-3",
                "baz": "blahblahblah"
            }
        ]
    }
}]

class Goober(graphene.ObjectType):
    id = graphene.String()
    baz = graphene.String()

class Tronic(graphene.ObjectType):
    id = graphene.String()
    baz = graphene.String()

class GooberResult(graphene.ObjectType):
    success = graphene.List(Goober)
    failed = graphene.List(Goober)
    cancelled = graphene.List(Goober)

class TronicResult(graphene.ObjectType):
    success = graphene.List(Tronic)
    failed = graphene.List(Tronic)
    cancelled = graphene.List(Tronic)

class FooResult(graphene.ObjectType):
    id = graphene.String()
    goober = graphene.Field(GooberResult)
    tronic = graphene.Field(TronicResult)

class Foo(graphene.Mutation):
    success = graphene.List(FooResult)
    failed = graphene.List(FooResult)

    def mutate(self, info):
        # provide some data as a mutation result
        success = SUCCESS
        failed = FAILED
        return Foo(success=success, failed=failed)

class Mutation(graphene.ObjectType):
    foo = Foo.Field()

schema = graphene.Schema(mutation=Mutation)
result = schema.execute("""
mutation Foo {
    foo {
        success {
            id
            goober {
                success {
                    id
                    baz
                }
            }
        }
        failed {
            id
            tronic {
                cancelled {
                    id
                    baz
                }
            }
        }
    }
}
""")

print(json.dumps(result.data, indent=2))

运行此脚本时,将获得预期的结果:

{
  "foo": {
    "success": [
      {
        "id": "abc123",
        "goober": {
          "success": [
            {
              "id": "goober-type-1",
              "baz": "blahblahblah"
            },
            {
              "id": "goober-type-2",
              "baz": "blahblahblah"
            }
          ]
        }
      }
    ],
    "failed": [
      {
        "id": "abc123",
        "tronic": {
          "cancelled": [
            {
              "id": "tronic-type-3",
              "baz": "blahblahblah"
            }
          ]
        }
      }
    ]
  }
}

文档中的开始位置可能在relationship between ObjectTypes and FieldsObjectType,但是您定义的Field没有,这就是为什么需要将它们包装在Scalars act as Fields中的原因。然后,只需看看Field即可一起使用。

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