graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

TypeError when using AuthorizePlugin #511

Open chareice opened 4 years ago

chareice commented 4 years ago

image

TS2345: Argument of type '{ type: NexusObjectTypeDef<"GroupCategory">; description: string; args: { name: NexusArgDef<"String">; }; authorize: () => boolean; resolve(parent: any, args: any, { database, tenant }: Context): Promise<...>; }' is not assignable to parameter of type 'NexusOutputFieldConfig<"Mutation", "createGroupCategory">'.   Object literal may only specify known properties, and 'authorize' does not exist in type 'NexusOutputFieldConfig<"Mutation", "createGroupCategory">'.
Weakky commented 4 years ago

Hey @chareice,

Given your TS error, it looks like the authorize property is not known at all by TS. Are you sure you've enabled the authorize plugin?

import { makeSchema, fieldAuthorizePlugin } from '@nexus/schema'

export const schema = makeSchema({
  plugins: [fieldAuthorizePlugin()],
})
chareice commented 4 years ago

@Weakky Yes, It's enabled.

Weakky commented 4 years ago

Well, I'm sorry but I cannot reproduce your error. Would you mind providing a small reproduction? 🙏

image

cvng commented 3 years ago

@chareice A quick fix I have found is to copy-paste this snippet into your project:

// Taken from https://github.com/graphql-nexus/schema/blob/4f4f9f49e8afa235da1f1cfe73e685fbf8835714/examples/ghost/src/generated/ghost-nexus.ts#L217

declare global {
  interface NexusGenPluginTypeConfig<TypeName extends string> {}
  interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
    /**
     * Authorization for an individual field. Returning "true"
     * or "Promise<true>" means the field can be accessed.
     * Returning "false" or "Promise<false>" will respond
     * with a "Not Authorized" error for the field.
     * Returning or throwing an error will also prevent the
     * resolver from executing.
     */
    authorize?: FieldAuthorizeResolver<TypeName, FieldName>
  }
  interface NexusGenPluginSchemaConfig {}
}

You could also try to use Nexus built-in typegen and the error should vanish:

https://nexusjs.org/docs/api/make-schema#shouldgenerateartifacts-outputs-typegenautoconfig

antoinelin commented 3 years ago

Hello 🖖

Encountered the same issue now and with the disparition of typegenAutoConfig I wondered if is there any different solution instead of creating definition file ourselves (which imo should not be an ideal fix) ?

tgriesser commented 3 years ago

Encountered the same issue now and with the disparition of typegenAutoConfig I wondered if is there any different solution instead of creating definition file ourselves (which imo should not be an ideal fix) ?

typegenAutoConfig hasn't gone anywhere, it was just renamed to sourceTypes

antoinelin commented 3 years ago

Indeed, my bad I wrote my comment before making further investigations about this 🤡 I found a solution in case of someone looking for a fix. I added this to tsconfig.json

  "ts-node": {
    "files": true
  },
  "files": [
    "./@types/nexus-typegen.d.ts",
  ],

And now it works for me :)