class Product(SQLAlchemyObjectType):
class Meta:
model = ProductModel
interfaces = (relay.Node, )
@classmethod
def get_node(cls, id, context, info):
#must implement to relay
pass
class Query(graphene.ObjectType):
product = graphene.Field(Product, id=graphene.String())
node = relay.Node.Field()
def resolve_product(self, args, context, info):
session = context['session']
id_ = from_global_id(args['id'])[1]
p = session.query(ProductModel).filter(ProductModel.id == id_).first()
return p
I am trying to use GraphQL + Relay + Grephene. But I am a little bit confused. Can I support both queries using relay? Or when implementing realy I will only support the second one?
Do I always need to translate the globalId to database primary key?
Graph QL:
{
product (id: "XYZ"){
id
title
}
}
Relay:
{
node(id: "XYZ") {
id
... on product {
title
}
}
The Relay spec and implementation requires you to support a
node
query in addition you your query root (usuallyviewer
)Relay will use
node
internally when it needs to fetch fields for a specific node. Regardless of node, if you really want aproduct
query and call it yourself from one of your Relay Containers.As for global ids, yes - you always need to translate from global id to primary key. But to be more exact - you always need to translate from global ID to the internal key representation of whatever DB behind the ObjectType you're querying. The global id contains the type information - which ObjectType we're trying to fetch. Without that information the framework wouldn't know what ObjectType to send the id to so it can do the fetching.