3

我正在尝试在石墨烯中编写嵌套突变,但在将一个突变的输出传递给另一个时遇到了麻烦。我希望WriteImage解析器能够接受来自ReadImage解析器的输入图像。最终,WriteImage也应该能够接受来自其他解析器的图像,以便用户可以编写管道脚本。

架构如下所示:

import SimpleITK as sitk
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor

class Image(graphene.Scalar):
    '''SimpleITK Image Type'''

    @staticmethod
    def serialize(Image):
        return Image

    @staticmethod
    def parse_literal(node):
        if isinstance(node, sitk.Image):
            return node

    @staticmethod
    def parse_value(value):
        return value

class ReadImage(graphene.Mutation):
    class Input:
        path = graphene.String()

    image = Image()

    @staticmethod
    async def mutate(cls, args, context, info):
        image = sitk.ReadImage(args.get('path'))
        return ReadImage(image=image)


class WriteImage(graphene.Mutation):
    class Input:
        image = Image()
        path = graphene.String()

    image = Image()

    @staticmethod
    async def mutate(cls, args, context, info):
        image = args.get('image')
        sitk.WriteImage(image, args.get('path'))
        return WriteImage(image=image)


class Mutation(graphene.ObjectType):
    readImage = ReadImage.Field()
    writeImage = WriteImage.Field()


schema = graphene.Schema(
    query=Query,
    mutation=Mutation,
)

executed = schema.execute(
    """
        mutation {
            writeImage(
                image: {
                    readImage(
                        path: "/path/to/image.nii"
                    ) {
                        image
                    }
                },
                path: "/Users/kasper/Desktop"
            ) {
                image
            }
        }
    """, executor=AsyncioExecutor())

print(executed.data)
print(executed.errors)

这输出

>>> None
>>> [GraphQLError('Argument "image" has invalid value ReadImageInput.\nExpected "ReadImageInput", found not an object.',)]

这个错误信息是什么意思?为什么呢not an object?架构应该如何构建,以便我可以使用一个解析器的输出作为另一个解析器的输入?

附加信息:执行ReadImage产生以下输出并允许您查看图像类型:

executed = schema.execute(
    """
        mutation {
            readImage(
                path: "/path/to/image.nii"
            ) {
                image
            }
        }
    """, executor=AsyncioExecutor())

print(executed.data)
print(executed.errors)

>>> OrderedDict([('readImage', OrderedDict([('image', <SimpleITK.SimpleITK.Image; proxy of <Swig Object of type 'std::vector< itk::simple::Image >::value_type *' at 0x1087b5030> >)]))])
>>> None
4

0 回答 0