optimistic-gold
optimistic-gold7mo ago

Nested update in Hasura

I want to implement below mutation
mutation UpdateHelpCategoryWithMetadata($id: uuid!, $help_category: help_categories_set_input!, $metadata: [help_categories_metadata_insert_input!]!) {
update_help_categories_by_pk(pk_columns: {id: $id}, _set: $help_category) {
id
action
sort_order
status
help_categories_metadata {
id
language_id
name
}
}
insert_help_categories_metadata(objects: $metadata, on_conflict: {constraint: help_categories_metadata_help_category_id_language_id_key, update_columns: [name]}) {
affected_rows
returning {
id
language_id
name
}
}
}
mutation UpdateHelpCategoryWithMetadata($id: uuid!, $help_category: help_categories_set_input!, $metadata: [help_categories_metadata_insert_input!]!) {
update_help_categories_by_pk(pk_columns: {id: $id}, _set: $help_category) {
id
action
sort_order
status
help_categories_metadata {
id
language_id
name
}
}
insert_help_categories_metadata(objects: $metadata, on_conflict: {constraint: help_categories_metadata_help_category_id_language_id_key, update_columns: [name]}) {
affected_rows
returning {
id
language_id
name
}
}
}
variables
{
"id": "e0d975be-3335-443f-962d-cae07ae15832",
"help_category": {
"action": "act",
"sort_order": 3,
"status": true
},
"metadata": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "قسط D"
}
]
}
{
"id": "e0d975be-3335-443f-962d-cae07ae15832",
"help_category": {
"action": "act",
"sort_order": 3,
"status": true
},
"metadata": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "قسط D"
}
]
}
Refine's generates
mutation ($pk_columns: help_categories_pk_columns_input!, $_set: help_categories_set_input!) {
update_help_categories_by_pk (pk_columns: $pk_columns, _set: $_set) {
id, action, sort_order, status, help_categories_metadata { id, language_id, name }
}
}
mutation ($pk_columns: help_categories_pk_columns_input!, $_set: help_categories_set_input!) {
update_help_categories_by_pk (pk_columns: $pk_columns, _set: $_set) {
id, action, sort_order, status, help_categories_metadata { id, language_id, name }
}
}
variables
{
"pk_columns": {
"id": "e0d975be-3335-443f-962d-cae07ae15832"
},
"_set": {
"action": "act",
"sort_order": 3,
"status": true,
"help_categories_metadata": {
"data": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"name": "قسط D"
}
]
}
}
}
{
"pk_columns": {
"id": "e0d975be-3335-443f-962d-cae07ae15832"
},
"_set": {
"action": "act",
"sort_order": 3,
"status": true,
"help_categories_metadata": {
"data": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"name": "قسط D"
}
]
}
}
}
error
field 'help_categories_metadata' not found in type: 'help_categories_set_input'
field 'help_categories_metadata' not found in type: 'help_categories_set_input'
3 Replies
kapa.ai
kapa.ai7mo ago
Hi @narayandreamer. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It looks like you are trying to perform a nested update in Hasura using refine, but you are encountering an error because the help_categories_metadata field is not found in the help_categories_set_input type. Based on the provided information, refine's generated mutation does not support the nested update structure you are aiming for. Instead, you need to perform two separate operations: one for updating the help_categories and another for inserting/updating the help_categories_metadata. Here is how you can achieve this using the provided mutation and variables: Mutation
mutation UpdateHelpCategoryWithMetadata($id: uuid!, $help_category: help_categories_set_input!, $metadata: [help_categories_metadata_insert_input!]!) {
update_help_categories_by_pk(pk_columns: {id: $id}, _set: $help_category) {
id
action
sort_order
status
help_categories_metadata {
id
language_id
name
}
}
insert_help_categories_metadata(objects: $metadata, on_conflict: {constraint: help_categories_metadata_help_category_id_language_id_key, update_columns: [name]}) {
affected_rows
returning {
id
language_id
name
}
}
}
mutation UpdateHelpCategoryWithMetadata($id: uuid!, $help_category: help_categories_set_input!, $metadata: [help_categories_metadata_insert_input!]!) {
update_help_categories_by_pk(pk_columns: {id: $id}, _set: $help_category) {
id
action
sort_order
status
help_categories_metadata {
id
language_id
name
}
}
insert_help_categories_metadata(objects: $metadata, on_conflict: {constraint: help_categories_metadata_help_category_id_language_id_key, update_columns: [name]}) {
affected_rows
returning {
id
language_id
name
}
}
}
Variables
{
"id": "e0d975be-3335-443f-962d-cae07ae15832",
"help_category": {
"action": "act",
"sort_order": 3,
"status": true
},
"metadata": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "قسط D"
}
]
}
{
"id": "e0d975be-3335-443f-962d-cae07ae15832",
"help_category": {
"action": "act",
"sort_order": 3,
"status": true
},
"metadata": [
{
"language_id": "d8f82ec6-6881-4299-afcc-3fe859ce3b59",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "Payment0"
},
{
"language_id": "cdd56b9d-181e-4271-a47f-aa2f62ceddff",
"help_category_id": "e0d975be-3335-443f-962d-cae07ae15832",
"name": "قسط D"
}
]
}
Explanation 1. Update help_categories: This part of the mutation updates the help_categories table with the provided help_category data. 2. Insert/Update help_categories_metadata: This part of the mutation inserts new metadata or updates existing metadata based on the on_conflict constraint. Error Resolution The error field 'help_categories_metadata' not found in type: 'help_categories_set_input' occurs because help_categories_metadata is not a direct field of help_categories_set_input. Instead, you need to handle metadata updates separately as shown in the mutation above. By structuring your mutation this way, you can achieve the desired nested update functionality in Hasura. For more information on how to use refine with Hasura, you can refer to the refine Hasura documentation (https://refine.dev/docs/data/packages/hasura/#utility-types). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@689530553313394880> kapa.ai is still learning and improving, please let us know how it did by reacting below
optimistic-gold
optimistic-goldOP7mo ago
But I'm using useForm and want atomicity