question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access request user in schema resolve function

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
HeyHugocommented, Oct 19, 2022

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

2reactions
M3te0rcommented, Aug 12, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found