Access request user in schema resolve function
See original GitHub issueI have likes model between users, meaning that I can like other’s profile. I am looking forward to include likes count in schema, when accessing others profile, so that I can pass data to frontend.
class DetailedProfileSchema(Schema):
id: uuid.UUID
.....
likes_count: int
def resolve_likes_count(self, obj) -> int:
print("1")
request_profile: Profile = Profile.objects.get(id=self.request.user.id) <<<<< I need to access request user profile, how can I do it?
print("2")
return request_profile.likes.filter(target_profile__id=obj.id).count()
resolve function does not raise any errors, it reaches print(1)
but not print(2)
, the only log I get is:
pydantic.error_wrappers.ValidationError: 1 validation errors for NinjaResponseSchema
E response -> likes_count
E field required (type=value_error.missing)
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >How to access the request object inside a GraphQL resolver ...
The request object should be accessed through the context. ... graphqlExpress(req => ({ schema, context: { user: req.user } })).
Read more >Resolvers - graphql-compose
The main aim of Resolver is to keep available resolve methods for Type and use them for building relation with other types. Resolver...
Read more >A complete guide to permissions in a GraphQL API
Access control – Checking whether the user is authorized to access the ... Provide resolver functions for your schema fields const resolvers ...
Read more >Resolvers – GraphQL Tools
To respond to queries, a schema needs to have resolvers for all fields. Resolvers are per field functions that are given a parent...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Pydantic v2 will add some means to have validation context, assuming django-ninja will adopt v2 maybe this will be relevant for including request/user context. https://pydantic-docs.helpmanual.io/blog/pydantic-v2/#validation-context
Hi @vitalik I’m using this method of transferring out the user to the schema quite a lot in my company project. It works well but it feels a bit hacky sometimes when I have a list of objects and nested objects/schemas (essentially for my use of django-guardian for objects permissions).
Accessing the user (or request) seems to be something needed given the previous issues on this subject : #495 #484 #422
It would be great to have a nicer way of dealing with this