是否有可能 return 石墨烯突变的对象列表?

Is it possible to return a list of Objects for a mutation in graphene?

我创建了一个 mutation 来处理文件中的数据。这些文件由名为 IncomingFile.

的 django 模型表示

最初我一次只获取一个带有他的 ID 的 IncomingFile,但现在我认为获取一个 IncomingFile 的 ID 列表并处理它们可能会更好。

所以我以这种方式纠正我的突变:

class FileMutation:

    process_file = GpexeCustomField(
        Boolean,
        incomingfile_list_id=List(ID, required=True)
    )

    def resolve_process_file(self, info, incomingfile_list_id, **kwargs):
        try:
            ifile_list = []

            if not incomingfile_list_id:
                return False

            for id in incomingfile_list_id:
                ifile_list.append(IncomingFileType.objects(info, "write").get(id=id))

            for incomingfile in ifile_list:
                incomingfile.process()

        except Exception as e:
            logger.error(f"Error on request to process incoming files from {e}")
            return False

        return True

(GpexeCustomField 只是一个class 帮助调用正确的解析方法)

对象类型:

class IncomingFileType(DjangoObjectType):
    class Meta:
        model = IncomingFile
        fields = "__all__"

效果很好!但是现在我想 return ifile_list (它包含已处理的 IncomingFile 对象)并在 graphql 控制台中显示它们的 ID。可能吗?如果是,如何?我应该更正或添加什么? 我不想要解决方案,只想要一些让我有可能独自完成的概念。

提前致谢,抱歉英语不好!

你可以有这样的东西:

class ProcessedFile(graphene.ObjectType):
    id = graphene.ID()
    is_processed = graphene.Boolean()

class FileMutation:
    processed_files = graphene.List(
        ProcessedFile,
        incomingfile_list_id=List(ID, required=True)
    )

    def resolve_process_file(self, info, incomingfile_list_id, **kwargs):
        try:
            ifile_list = []
            if not incomingfile_list_id:
                return False
            for id in incomingfile_list_id:
                ifile_list.append(IncomingFileType.objects(info, "write").get(id=id))
            results = []
            for incomingfile in ifile_list:
                is_processed = incomingfile.process()
                # I assume that id can be retrieved
                results.append(ProcessedFile(id=incomingfile.id, is_processed=is_processed))       
          
        except Exception as e:
            logger.error(f"Error on request to process incoming files from {e}")
            return []
        return results

或使用现有的IFileType

class FileMutation:
    processed_files = graphene.List(
        IFileType,
        incomingfile_list_id=List(ID, required=True)
    )

    def resolve_process_file(self, info, incomingfile_list_id, **kwargs):
        try:
            ifile_list = []
            if not incomingfile_list_id:
                return False
            for id in incomingfile_list_id:
                ifile_list.append(IncomingFileType.objects(info, "write").get(id=id))
            for incomingfile in ifile_list:
                incomingfile.process()      
        except Exception as e:
            logger.error(f"Error on request to process incoming files from {e}")
            return []
        return ifile_list