在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"
}
}
]
}
}
如何获取数据库级别的对象的原始" ID(例如"112")而不是该节点的唯一标识符?
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屋!