djmastov
(radik)
November 22, 2024, 3:57pm
1
inserting the data
uid(contact_{idx}) <uuid> "{contact_user["profile_uuid"].hex}" .
uid(contact_{idx}) <is_shadow_banned> "{int(contact_user["is_shadow_banned"])}" .
uid(contact_{idx}) <is_blocked> "{int(contact_user["is_blocked"])}" .
uid(contact_{idx}) <phone> "{contact_user["phone"]}" .
uid(contact_{idx}) <updated_at> "{contact_user["updated_at"]}" .
uid(contact_{idx}) <is_default_nickname> "{int(contact_user["is_default_nickname"])}" .
uid(contact_{idx}) <dgraph.type> "User" .
uid(user) <HAS_CONTACT> uid(contact_{idx}) (created_at="{created_at}") .
making a request
{
users(func: type(User), first: 10) {
uuid
updated_at
HAS_CONTACT @facets
}
}
but I don’t get the created_at field in the response
In the future, I need to make max by the created_at value
What am I doing wrong? Help please
Raphael
(Raphaël Derbier)
November 22, 2024, 10:54pm
2
The node you create with uuid, dgraph.type does not seems to be same node you add “HAS_CONTACT” predicate too.
Could you dump the generated RDF?
HAS_CONTACT is added to the node uid(user)
The rest of the RDFs are on the uid uid(contact_{idx})
djmastov
(radik)
November 23, 2024, 3:58pm
3
Sorry, This is how I add data in python code
url_path = "mutate?commitNow=true"
query = f"""
upsert {{
query {{
user_query(func: eq(uuid, "{user.user_uuid.hex}")) {{
user as uid
}}
"""
mutations = f"""mutation {{ set {{
uid(user) <uuid> "{user.user_uuid.hex}" .
uid(user) <is_shadow_banned> "{int(user.is_shadow_banned)}" .
uid(user) <is_blocked> "{int(user.is_blocked)}" .
uid(user) <phone> "{user.phone}" .
uid(user) <is_default_nickname> "{int(user.is_default_nickname)}" .
uid(user) <dgraph.type> "User" .
"""
created_at = int(datetime.datetime.now().timestamp())
for idx, contact_user in enumerate(users):
query = f"""{query}
contact_query_{idx}(func: eq(phone, "{contact_user["phone"]}")) {{
contact_{idx} as uid
}}
"""
if "profile_uuid" in contact_user:
mutations = f"""
{mutations}
uid(contact_{idx}) <uuid> "{contact_user["profile_uuid"].hex}" .
uid(contact_{idx}) <is_shadow_banned> "{int(contact_user["is_shadow_banned"])}" .
uid(contact_{idx}) <is_blocked> "{int(contact_user["is_blocked"])}" .
uid(contact_{idx}) <phone> "{contact_user["phone"]}" .
uid(contact_{idx}) <updated_at> "{contact_user["updated_at"]}" .
uid(contact_{idx}) <is_default_nickname> "{int(contact_user["is_default_nickname"])}" .
uid(contact_{idx}) <dgraph.type> "User" .
uid(user) <HAS_CONTACT> uid(contact_{idx}) (created_at={created_at}) .
"""
if contact_user["has_subscribe"]:
mutations = f"""{mutations}
uid(user) <SUBSCRIBED> uid(contact_{idx}) .
"""
if contact_user["has_subscribe_out"]:
mutations = f"""{mutations}
uid(contact_{idx}) <SUBSCRIBED> uid(user) .
"""
if contact_user["in_blacklist"]:
mutations = f"""{mutations}
uid(user) <BLACKLISTED> uid(contact_{idx}) .
"""
if contact_user["user_in_blacklist"]:
mutations = f"""{mutations}
uid(contact_{idx}) <BLACKLISTED> uid(user) .
"""
else:
mutations = f"""{mutations}
uid(contact_{idx}) <phone> "{contact_user["phone"]}" .
uid(contact_{idx}) <updated_at> "{contact_user["updated_at"]}" .
uid(contact_{idx}) <dgraph.type> "User" .
uid(user) <HAS_CONTACT> uid(contact_{idx}) (created_at={created_at}) .
uid(contact_{idx}) <phone_book_order> "{contact_user["phone_book_order"]}" .
"""
dql = f"{query}}}{mutations}}}}}}}"
logger.info(dql)
resp = await self._mutate(
dql,
path=url_path,
)
resp_json = resp.json()
Raphael
(Raphaël Derbier)
November 25, 2024, 9:10pm
4
Thanks for the details.
Now I have a better understanding of your issue.
When you try to create the HAS_CONTACT predicate in :
you are using the uid() function both on subject and object. This is a tricky case and I need to verify if it is supported.
The complexity is that uid(user) is a list of uids (in your case it’s only one, but theoretically uid(v) is the list of all uids of the the map variable). So using it in both object and subject leads to handling the cartesian product of uid(user) and uid(contact_{idx}).
I’ll check the options…
Raphael
(Raphaël Derbier)
November 26, 2024, 5:13pm
5
We actually support uid() in both SUBJECT and OBJECT.
So the mutation is working.
The issue is in your query:
HAS_CONTACT
is a relationship so you must query something from the related node to get a result
Just add
HAS_CONTACT @facets { uid }
as a result, you should get something like
{
"uid": "0xd",
"HAS_CONTACT|created_at": 1732640718
}
Let me know if that works.
BTW you can simplify the query to get the uids:
contact_query_{idx}(func: eq(phone, "{contact_user["phone"]}")) {{
contact_{idx} as uid
}}
is equivalent to
contact_{idx} as var (func: eq(phone, "{contact_user["phone"]}"))
1 Like
djmastov
(radik)
November 27, 2024, 2:48pm
6
Thank you very much. It worked for my request. You’re the best!
HAS_CONTACT @facets { uuid }
1 Like