-
-
Notifications
You must be signed in to change notification settings - Fork 172
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
The context
argument of inputRule
has insufficient type definition
#1315
Comments
Hey @Jinsung-L 👋, Thank you for opening an issue. We will get back to you as soon as we can. Have you seen our Open Collective page? Please consider contributing financially to our project. This will help us involve more contributors and get to issues like yours faster. https://opencollective.com/graphql-shield
|
context
argument of inputRule
is insufficient type definitioncontext
argument of inputRule
has insufficient type definition
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Is there any update on this issue? If you need a temporary solution, simply create a wrapper around the inputRule function to convert the type. I use this in my project and it works perfectly fine. export const inputRuleWithContext = inputRule as unknown as <T>(
name?: string
) => (
schema: (
yup: typeof Yup,
ctx: GraphQLContext
) => Yup.BaseSchema<T, import('yup/lib/types.js').AnyObject, any>,
options?:
| import('yup/lib/types.js').ValidateOptions<import('yup/lib/types.js').AnyObject>
| undefined
) => InputRule<T>; Where |
Bug report
Describe the bug
inputRule
is defined as follows:graphql-shield/packages/graphql-shield/src/rules.ts
Lines 210 to 228 in 8fdb22f
Suppose that I have defined an input rule to check if there is already existing user with the given email address.
Then I would like I use the
prisma
client that I have passed in the context of the GraphQL resolver like as follows:However, since the type of context in
IRuleFunction
is defined asIShieldContext
, type error occurs sayingProperty 'prisma' does not exist on type 'IShieldContext'
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
Expected behavior
Accessing
prisma
from thecontext
argument should be able.Actual behaviour
Can not access
prisma
from thecontext
argument due to the type error.Additional context
My current workaround is to use type casting.
But this workaround looks messy. So I think there should be essential solution to this problem.
I have two idea.
The first idea is
To remove the
IShieldContext
type definition for thecontext
parameter and useany
instead.This way people may set their own types for the context parameter.
The downside of this is that it disables the type inference of
IShieldContext
. Not should whether it's useful though.Normal
rule
defines its context parameter asany
as well.The second idea is
To use generic types.
Type definition of inputRule already uses one generic type
T
that I don't know what for.You can use another generic type
TContext
to merge withIShieldContext
.This way you can keep the
IShieldContext
while allowing users to define their own context type.But I think you should either provide the default type for T or override the method or something like that. I'm not really familiar with using generic types, so please improve this idea to make it better.
The text was updated successfully, but these errors were encountered: