The GraphQL query you are talking about is a typical NoSQL data set, where there is duplication (as there is no “relation” between sub-trees) or rather the data has been “joined”.
That is, of course, different from getting a dataset that is “unjoined”, for example just a userId, e.g.
{
posts {
id
body
userId
}
users {
user {
id
name
}
}
}
{
"data": {
"posts": [{
"id": "1",
"body": "Hello John",
"userId": "1",
},
{
"id": "2",
"body": "Hi Bob",
"userId": "2",
},
{
"id": "3",
"body": "What's the latest?",
"userId": "1",
}],
"users": [
user: {
id: "1",
name: "Bob"
},
user: {
id: "2",
name: "John"
}],
}
}
as you describe.
It’s not like there is a right answer. It just depends on what you are trying to do with the data.
If you are just going to display it, maybe the api is fine as it, and you have duplicated data.
If you are looking to update the data, then that is an entirely different problem, as I’d assume you have to push the changes back to a server.
In that case, yes, you have to deal with the Nothing | Just User issue you mention when displaying a post, as you’d clearly want to only update an author’s info in one place, and the assumption would be that if there is no userId in the users list/dict, you would be unable to update it.
I wouldn’t consider this an “impossible state”. It’s possible that a post is written, but the author forgot to sign it. Or deleted their name without deleting the post. Etc. Or maybe there are two authors. Etc. Or there is an author that hasn’t written any posts.
And the same applies in reverse. An author may not have any posts.
All of that CRUD logic would have to be enforced at the database level or the on the server at the time the post was created or updated.
In other words, there are two possibilities.
- Authors are directly embedded in blog posts, in which case there is no real relation and each article has an author. If there happens to be the same author on multiple posts, that is just coincidence, and therefore updating an author on one post doesn’t imply anything else should change.
- Authors are independent of posts, in which case the relations must be enforced somehow (e.g. at the database level with foreign keys, etc.), and therefore updates to author’s names must be pushed back to a server so that the next time a post is queried, the author’s name is updated.
To sum it up, I don’t think this is suboptimal. It is just the nature of dealing with relational data that may or may not exist. I think when you actually go to implement this, it will be quite straightforward!
Hope this helps!