Is embedding an authenticated user preferences object into another object in GraphQL a common pattern?

Is embedding an authenticated user preferences object into another object in GraphQL a common pattern?


0

Let’s say I have the following 3 GraphQL types/objects.

type Post {
   id: ID!
   title: Strings!
   author: User!
}

# composite unique key (postId & userId)
type UserPostVote {
   post: Post!
   user: User!
   value: Int! # -1 or 1
}

type User {
   id: ID!
   username: String!
}

Now the frontend wants to know whether the user has voted or not and whether the user can edit the post or not, this is based not only on whether the post was created by an authenticated user or not, but also whether the post is locked (uneditable) or not or whether user banned or not, there are some similar related properties I need to add and have more complex logic, just to keep that short as an example.

Because of that, I created a new type below (MeOnPost). I used quite specific names because there will be similar types, like MeOnComment and so on.

# new
type MeOnPost {
   isVoted: Boolean!
   editable: Boolean!
}

type Post {
   id: ID!
   title: Strings!
   author: User!

   # new, may be null if the request was made by an unauthenticated user
   me: MeOnPost
}

Here I embedded a MeOnPost object into Post.me to provide the frontend with authenticated user preferences. Is this a common pattern? or is there another better method?


Load 4 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *