I'm working on a GraphQL AppSync project for 6 months by now and I'm already quite familiar with the concept so far.
But I came across one thing, which is not explained in tutorials or documentations at all. What is the best practice of the return type of a Mutation? (especially of a partial update)
Here is one simplified sample schema:
type Article {
uuid: ID
title: String
description: String
price: Int
tax: Int
category_uuid: ID
status: Int
type: Int
}
input ArticleUpdateInput {
uuid: ID!
title: String
description: String
price: Int
tax: Int
category_uuid: ID
status: Int
type: Int
}
type Mutation {
updateArticle(input: ArticleUpdateInput!): Article!
}
The following mutation would be valid:
mutation foo {
updateArticle(input: {
uuid: "c63c6dcb-6c09-4952-aae2-26e3fde47262",
title: "BBQ Burger",
price: 699
}) {
__typename
uuid
title
description
price
tax
category_uuid
status
type
}
}
Since I only specified the title and the price the other fields of the response will be null like so:
{
"data": {
"updateArticle": {
"__typename": "Article",
"uuid": "c63c6dcb-6c09-4952-aae2-26e3fde47262",
"title": "BBQ Burger",
"description": null,
"price": 699,
"tax": null,
"category_uuid": null
"status": null
"type": null
}
}
}
What would be a best practice here to avoid to return these null fields? Should I trigger a getArticle query after the update and return the whole article record from the database? I think this would be very inefficient because if you want to add n articles, there will be 2*n roundtrips to the database.
Any ideas so far?
description,status...) to be filled with data or you just don't need them in the response at all? What data source are you using? - TinouString. If your application requires the entireArticle, then you would need to do a read after your update to grab all the fields. - Tinou