-
Hi Jack, What does 'root' mean where it is used in the context of checking the user.id?
Is it a reference to the model calling the middleware? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yeah so in this particular example we are applying a "method decorator" called UseIsCurrentUser to a field on the user model/type. The purpose is that once we've retrieved the user from the database, we check if the current user (i.e the logged in user "making" the request), is authorised to "see" the email of the user that is being requested. This is stop random users getting information of other users that we don't want! When applying a method decorator to a field or field resolver, the root refers to the type "above" the current field in the graphql tree/schema so for example, imagine we are querying a user, our graphql query will look something like: user(id: String!) { # <--- this is the root
id
email # <--- this is the field we are applying the decorator to
firstName
} So you can see that when we "ask" for the email field, type-graphql will pass the user retrieved from the database (i.e the result of the "user" resolver, found in user.resolver.ts), to the UseIsCurrentUser decorator as "root". This applies to all nested types in the tree. Type-graphql just works its way down your query running all the fields, field resolvers, decorators etc before returning to the frontend. post {
id
title # <--- the root for this field is the post, Post type
author {
id
email # <--- the root for this field is the author, User type
}
} I'd recommend reading this particular page from the type-graphql docs. https://typegraphql.com/docs/custom-decorators.html#method-decorators and if you haven't already, read the entire docs, as its basically the entire framework of the backend! |
Beta Was this translation helpful? Give feedback.
Yeah so in this particular example we are applying a "method decorator" called UseIsCurrentUser to a field on the user model/type. The purpose is that once we've retrieved the user from the database, we check if the current user (i.e the logged in user "making" the request), is authorised to "see" the email of the user that is being requested. This is stop random users getting information of other users that we don't want! When applying a method decorator to a field or field resolver, the root refers to the type "above" the current field in the graphql tree/schema
so for example, imagine we are querying a user, our graphql query will look something like: