2

在我的项目中,我有一个名为的对象Workflows,它与另一个名为DataSources. 我非常接近能够使用 GraphQL Mutation 创建新工作流。这是相关的代码:

class DataSourceInput(graphene.InputObjectType):
    id = graphene.Int()
    database_name = graphene.String()
    table_name = graphene.String()

    @classmethod
    def get_node(cls, id, context, info):
        node = get_datasource(id)
        return node


class CreateWorkflow(relay.ClientIDMutation):
    class Input:
        name = graphene.String()
        description = graphene.String()
        datasources = graphene.List(DataSourceInput)

    workflow = graphene.Field(Workflow)
    success = graphene.Boolean()
    errors = graphene.String()

    @classmethod
    def mutate_and_get_payload(cls, input, context, info):
        name = input.get('name')
        description = input.get('description')
        datasources = input.get('datasources')

        try:
            workflow = WorkflowModel.create(name, description, datasources)
            return CreateWorkflow(workflow=workflow, success=True)

        except Exception as e:
            success = False
            return CreateWorkflow(workflow=None, success=False, errors=str(e))

似乎失败的是提取我希望与我正在创建的新工作流相关联的数据源。我收到一条错误消息,上面写着“不可散列的类型:'dict'”

我不确切知道此错误消息暗示我做了什么。

任何想法将不胜感激!

罗伯特

4

1 回答 1

0

在python中,如果将其指定为字典键,可能会发生错误。例如。

>>> a = dict()
>>> type(a)
<class 'dict'>
>>> a[[0]] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

在字典中,使用哈希函数来响应低成本处理的要求,避免在设置和获取值时通过eq进行整个搜索。

hashable 的一个例子是内置的 int、str、tuple、frozenset。

此上下文中的 dict 是 Graphql 请求。所以首先检查查询中键的值是否是可散列的。另外,最好将我请求的查询示例同时放入。

于 2018-08-22T04:02:00.927 回答