使用石墨烯执行突变时获取全局记录ID

时间:2018-01-22 04:32:25

标签: python graphql mutation graphene-python

我正在使用python包Flask,SQLAlchemy,Graphene和Graphene-SQLAlchemy构建GraphQL API。我跟着SQLAlchemy + Flask Tutorial。我能够执行突变来创建记录,我想在API的响应中获得此记录的全局ID:

以下是为突变实现的类:

from graphql_relay.node.node import to_global_id
import api_utils # Custom methods to create records in database
import batch_schema
import graphene


class CreateBatchOwnerInput(graphene.InputObjectType):
    """Input to create batch owner."""
    name = graphene.String(required=True)


class CreateBatchOwner(graphene.Mutation):
    """Create batch owner."""
    class Arguments:
        input = CreateBatchOwnerInput(required=True)

    # Class attributes
    batch_owner = graphene.Field(batch_schema.BatchOwner)

    @staticmethod
    def mutate(root, info, input=None):
        """Method to create batch owner."""
        record = {'name': input.name}
        batch_owner = api_utils.create('BatchOwner', record)  # This returns a dictionary {'id': int, 'name': string}
        batch_owner = batch_schema.BatchOwner(**batch_owner)
        batch_owner.id = to_global_id('BatchOwner', batch_owner.id)  # Convert database Id to global Id
        return CreateBatchOwner(batch_owner=batch_owner)

以下突变正常

mutation Mutation {
    createBatchOwner(input: {name:"Jake"}) {
        batchOwner {
          name
        }
    }
}

response:

{
  "data": {
    "createBatchOwner": {
      "batchOwner": {
        "name": "Jake"
      }
    }
  }
}

但是当我添加" id"有效负载中的字段我收到以下错误消息:

mutation Mutation {
    createBatchOwner(input: {name:"Antoine"}) {
        batchOwner {
          id
          name
        }
    }
}

response:

{
  "data": {
    "createBatchOwner": {
      "batchOwner": null
    }
  },
  "errors": [
    {
      "message": "'BatchOwner' object has no attribute '__mapper__'",
      "locations": [
        {
          "column": 11,
          "line": 4
        }
      ]
    }
  ]
}

以下是Flask应用程序生成的日志:

Traceback (most recent call last):
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 311, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executors/sync.py", line 7, in execute
    return fn(*args, **kwargs)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphene/relay/node.py", line 38, in id_resolver
    type_id = parent_resolver(root, info, **args)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphene_sqlalchemy/types.py", line 160, in resolve_id
    keys = self.__mapper__.primary_key_from_instance(self)
AttributeError: 'BatchOwner' object has no attribute '__mapper__'
Traceback (most recent call last):
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error
    exe_context, return_type, field_asts, info, result)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 405, in complete_value
    return complete_object_value(exe_context, return_type, field_asts, info, result)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 504, in complete_object_value
    return execute_fields(exe_context, return_type, result, subfield_asts)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 148, in execute_fields
    source_value, field_asts)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 247, in resolve_field
    result
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 324, in complete_value_catching_error
    return complete_value(exe_context, return_type, field_asts, info, result)
  File "/home/alexis/data-quality-venv/lib/python3.5/site-packages/graphql/execution/executor.py", line 383, in complete_value
    raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: 'BatchOwner' object has no attribute '__mapper__'

2 个答案:

答案 0 :(得分:1)

确切的答案是id保留给石墨烯,尽管在本教程中已经使用过,但从未在您的任何模型中使用它

答案 1 :(得分:0)

我已经解决了这个问题,并在教程中将其记录在案。

Flask-Graphene-SQLAlchemy Tutorial

似乎我的mutate方法和我的SQLAlchemy类的定义都被错误地定义了。