Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: injected dependency is not available in filter functions. #2310

Open
2 tasks done
hsblhsn opened this issue Feb 4, 2022 · 7 comments
Open
2 tasks done

bug: injected dependency is not available in filter functions. #2310

hsblhsn opened this issue Feb 4, 2022 · 7 comments

Comments

@hsblhsn
Copy link

hsblhsn commented Feb 4, 2022

Hello, dependency injection is a good feature and I have followed this tutorial to implement mine.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Currently, my dependencies are available inside the hook functions and in every mutation like ent.UserMutation. But I can't access them inside the filter function or type. They are not available in ent.UserFilter.

Expected Behavior 🤔

I expect the injected dependencies to be available all over my ent packages wherever I can touch.

Your Environment 🌎

Tech Version
Go 1.17.5
Ent 0.10.x
Database Postgresql
Driver pgx
@crossworth
Copy link
Contributor

Hello @hsblhsn, could you create a simple example using https://github.com/ent/bug?

@yonidavidson
Copy link
Collaborator

Hi @hsblhsn , which filters function do you mean?
Can you give me an example?
For example

c.User.Query().Where(user.NameEQ("yoni")).OnlyX(ctx)

Where are you expecting to get access to the Dependency?

@hsblhsn
Copy link
Author

hsblhsn commented Apr 14, 2022

Suppose I have pet and user microservice.

And I have a api client to check if the user has access to the pet.

so in a privacy filter function, I would use it like.

pets := m.APIClient.GetAllPets(ctx)
filter.Where(user.HasPetsIn(pets))

I have injected APIClient using entc.Dependency options as shown here at https://entgo.io/blog/2021/11/1/sync-to-external-data-systems-using-hooks

The problem is I can't access APIClient from FilterFuncs but I can easily access them using them in hooks. I want to access it in FilterFuncs as well.

Thank you!

@yonidavidson
Copy link
Collaborator

Why not add your API Client to the context (where you add the pet name)?

@hsblhsn
Copy link
Author

hsblhsn commented Apr 14, 2022

Nice idea! Thanks for that. But still, it would be nice for a dependency to be accessible from FilterFuncs. Because that's the whole point of creating a dependency at entity level.

@yonidavidson
Copy link
Collaborator

I think the reason is to avoid security issues by allowing the privacy layer to access the data it's protecting.
@a8m is this the reason?

@a8m
Copy link
Member

a8m commented Apr 14, 2022

No real reason, the Depdendecy option was added after privacy. However, I just landed #2473 to inject the config object (with all its dependencies) to the typed filters. See example -

func FilterUsesDep() privacy.QueryRule {
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
u, ok := f.(*ent.UserFilter)
if !ok {
return privacy.Denyf("unexpected filter type %T", f)
}
// Access the dependency after the type is resolved.
_ = u.HTTPClient
return privacy.Skip
})
}

@hsblhsn, I wonder how you use the Filter functions. Because the current privacy.Filter is an interface and dependencies are injected as fields. So currently, there's no way to access them without type-assertion.

Do you use generic Filters that are applied on multiple types or type-assertion is good enough for you? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants