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

Trouble with objectTypeDefTypes #568

Open
Sytten opened this issue Oct 21, 2020 · 0 comments
Open

Trouble with objectTypeDefTypes #568

Sytten opened this issue Oct 21, 2020 · 0 comments

Comments

@Sytten
Copy link
Collaborator

Sytten commented Oct 21, 2020

This is related to my plugin https://github.com/Sytten/nexus-shield
So I am trying to define a new objectType property and I honestly tried very hard to make it work, but the TypeName always gets overwritten by the incoming interface that you put as an input and the whole thing blows up to a basic string making the rest of the typing go down the toilet :)

So basically the generated code is:

interface NexusGenPluginTypeConfig<TypeName extends string> {
    /**
     * Default authorization rule to execute on all fields of this object
     */
    shield?: ShieldRule<TypeName, any>
  }

And I generate the interface using:

type ShieldRuleConfig<TypeName extends string, FieldName extends string> = {
  type?: TypeName;
  name?: string;
  cache?: ShieldCache;
  resolve: ShieldRuleFunction<TypeName, FieldName>;
};

export const ruleType = <TypeName extends string, FieldName extends string>(
  config: ShieldRuleConfig<TypeName, FieldName>
): ShieldRule<TypeName, FieldName> => {
  return new BaseRule<TypeName, FieldName>(config, config.resolve);
};

When I define the rules in the fields, I have no problem and the typing flows from the objectType into the ShieldRule, but for some reason in the object type it doesn't and it takes the string as the type.

Problem:
Screen Shot 2020-10-20 at 10 43 18 PM
Screen Shot 2020-10-20 at 10 43 00 PM

Similar situation for field (but works correctly):
Screen Shot 2020-10-20 at 10 45 02 PM

It also works fine if I just accept a function like:

interface NexusGenPluginTypeConfig<TypeName extends string> {
    /**
     * Default authorization rule to execute on all fields of this object
     */
    shield?: (root: RootValue<TypeName>, args: ArgsValue<TypeName, FieldName>, ctx: GetGen<'context'>, info: GraphQLResolveInfo) => ShieldRuleResult | Promise<ShieldRuleResult>;
  }

But that is not really what I want to do because I miss some configuration.

Any idea is appreciated, maybe this is not doable with the current state of typescript :/
I might have to switch to a more function interface instead of OO based like I have currently.

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

1 participant