在 GraphQL 中检索对象 ID [英] Retrieve the object ID in GraphQL
问题描述
我想知道是否有可能获得一个对象的原始 id"作为查询的结果.每当我向服务器发出请求时,它都会返回节点全局标识符",类似于 U29saWNpdGFjYW9UeXBlOjEzNTkxOA==
.
I'd like to know whether it is possible to get the "original id" of an object as the result of the query. Whenever I make a request to the server, it returns the node "global identifier", something like U29saWNpdGFjYW9UeXBlOjEzNTkxOA==
.
查询类似于这个:
{
allPatients(active: true) {
edges {
cursor
node {
id
state
name
}
}
}
返回的是:
{
"data": {
"edges": [
{
"cursor": "YXJyYXljb25uZWN0aW9uOjA=",
"node": {
"id": "U29saWNpdGFjYW9UeXBlOjEzNTkxOA==",
"state": "ARI",
"name": "Brad"
}
}
]
}
}
如何在数据库级别(例如112")而不是该节点唯一标识符获取对象的原始"ID?
How can I get the "original" id of the object at the database level (e.g. '112') instead of that node unique identifier?
ps.:我在服务器端使用 graphene-python 和 Relay.
ps.: I am using graphene-python and Relay on the server side.
推荐答案
重写 Node 对象中的默认 to_global_id 方法对我有用:
Overriding default to_global_id method in Node object worked out for me:
class CustomNode(graphene.Node):
class Meta:
name = 'Node'
@staticmethod
def to_global_id(type, id):
return id
class ExampleType(DjangoObjectType):
class Meta:
model = Example
interfaces = (CustomNode,)
这篇关于在 GraphQL 中检索对象 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!