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

Allow default to be set on the objectType #50

Open
Sytten opened this issue Jul 29, 2020 · 10 comments
Open

Allow default to be set on the objectType #50

Sytten opened this issue Jul 29, 2020 · 10 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@Sytten
Copy link
Owner

Sytten commented Jul 29, 2020

It would be nice to be able to set a default rule on the object type with overrides on each field that defines one.

@Sytten Sytten added the enhancement New feature or request label Jul 29, 2020
@Sytten Sytten self-assigned this Jul 29, 2020
@Sytten
Copy link
Owner Author

Sytten commented Aug 4, 2020

Partially fixed by #59, but it breaks the typing and I still have no idea why.

@Sytten Sytten added the bug Something isn't working label Oct 12, 2020
@Sytten
Copy link
Owner Author

Sytten commented Dec 15, 2020

@lewebsimple Not sure how good you are with advanced typescript but I could use a hand on that one if you have time.
I put most details in there: graphql-nexus/nexus#568
The issue is quite easy to reproduce and I could not find a good fix for it.

@lewebsimple
Copy link
Contributor

@Sytten hmmm I'm no TypeScript wizard so I compared your code to the official fieldAuthorizePlugin which should be similar in scope.

At first glance, the corresponding types FieldShieldResolver and FieldAuthorizeResolver differ in a significant way.. while FieldAuthorizeResolver has a (function) signature of (root, args, ctx, info) => MaybePromise<boolean | Error>, yours is of type { resolve: (root, args, ctx, info, options) => Promise<ShieldRuleResult> }.

So maybe using the .resolve type instead of the (parent) FieldShieldResolver could fix that ?

Also, I fail to see the difference between ShieldRule and ShieldRuleFunction.

@lewebsimple
Copy link
Contributor

lewebsimple commented Dec 15, 2020

It also occurs to me that the generated type differ from what you state in the other issue:

This is what you wrote:

interface NexusGenPluginTypeConfig<TypeName extends string> {
  shield?: ShieldRule<TypeName, any>
}

This is what I get in my project:

interface NexusGenPluginTypeConfig<TypeName extends string> {
  shield?: ObjectTypeShieldResolver<TypeName>;
}

where ObjectTypeShieldResolver<TypeName> corresponds to ShieldRule<TypeName, never>

Should it be any instead of never ?

Update : I manually changed ObjectTypeShieldResolver<TypeName> to ShieldRule<TypeName, any> and adjusted the imports in the generated nexus-types and it seems the typings work !? If this is the case I'm on a lucky streak since yesterday ! Nevermind VSCode gave me false hopes...

@Sytten
Copy link
Owner Author

Sytten commented Dec 15, 2020

Yeah I tried to simplify it, the ObjectTypeShieldResolver is just an alias type that plugins are supposed to use:
https://github.com/Sytten/nexus-shield/blob/master/src/plugin.ts#L48
Its import process that is a bit strange.
For some reason TS cant infer the real type and falls back to string which backs populate to the object and messes up the whole typing.

@lewebsimple
Copy link
Contributor

Did you base your code off of the official fieldAuthorize plugin ?

@Sytten
Copy link
Owner Author

Sytten commented Dec 16, 2020

Similar yes, but I reused the class based system of graphql-shield. I did my best on the TS trickery (some of it is explained at the end of the README), but this last problem I really have no clue. I would need better AST debugging tooling to understand why the compiler cant infer the parameter on object but can on the resolvers.

@sooxt98
Copy link

sooxt98 commented Sep 27, 2021

Im getting this error when using shield on objectType + t.field (nexus-prisma)
but t.string works fine

image

Here's the typing im using

declare global {
    interface NexusGenPluginFieldConfig<
        TypeName extends string,
        FieldName extends string
        > {
        shield?: FieldShieldResolver<TypeName, FieldName>;
    }
    
    interface NexusGenPluginTypeConfig<TypeName extends string> {
        shield?: ObjectTypeShieldResolver<TypeName>;
    }
}

@Sytten
Copy link
Owner Author

Sytten commented Sep 27, 2021

I am not really sure why the typing is broken. I don't have a lot of time to work that, so if it is important for you I would suggest you take a look at the code (will review PRs).

@Sytten
Copy link
Owner Author

Sytten commented May 30, 2022

Just a note for later, with the new in/out keyword for typescript there is probably a way to force the compiler to understand that the type used is an in and it should not override the assignee typing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants