0

I'm using Nexus with the fieldAuthorizePlugin to add an authorize function to my GraphQL schema fields for handling authentication. While everything works correctly at runtime (with @ts-ignore above authorize), TypeScript throws the following error during build time:

Object literal may only specify known properties, and 'authorize' does not exist in type 'NexusOutputFieldConfig<"Mutation", "createUser">'

Here is my setup:

  1. Nexus Schema Configuration:
import { makeSchema } from 'nexus'
import { fieldAuthorizePlugin } from 'nexus'
import path from 'path'

export const schema = makeSchema({
    types: schemaTypes, // All my types, queries, and mutations
    plugins: [fieldAuthorizePlugin()], // Registering the plugin
    outputs: {
        schema: path.join(__dirname, '../generated/schema.graphql'),
        typegen: path.join(__dirname, '../generated/nexus.ts'),
    },
    contextType: {
        module: require.resolve('../context'),
        export: 'Context',
    },
})
  1. Mutation Example:
export const CreateUserMutation = extendType({
    type: 'Mutation',
    definition(t) {
        t.field('createUser', {
            type: 'User',
            args: {
                input: nonNull(arg({ type: 'CreateUserInput' })),
            },
            authorize: isAuthenticated,
            resolve: async (_, { input }) => {
                const createUser = container.get<ICreateUser>(DIIdentifiers.UseCases.CreateUser)
                return createUser.execute(input)
            },
        })
    },
})
  1. Middleware:
export const isAuthenticated: FieldAuthorizeResolver<'Mutation', string> = async (_, args, context) => {
    const authHeader = context.req.headers.authorization

    if (!authHeader)
        throw new Error('Authorization header is missing')

    const token = authHeader.replace('Bearer ', '')
    const validateToken = container.get<IValidateToken>(DIIdentifiers.UseCases.ValidateToken)
    const isValid = await validateToken.execute(token)

    if (!isValid)
        throw new Error('Invalid or expired token')

    return true
}
  1. Generated Type Definitions: The nexus.ts file contains the following plugin-related type:
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
    authorize?: FieldAuthorizeResolver<TypeName, FieldName>;
}

However, it seems TypeScript still doesn't recognize authorize as a valid property of NexusOutputFieldConfig.

  1. tsconfig.json:
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "esnext",
    "baseUrl": "./",
    "paths": {
      "@generated/*": ["./generated/*"]
    },
    "outDir": "./dist",
     "typeRoots": [
        "./node_modules/@types",
        "./src/interface/generated"
     ]
  },
  "include": [
    "src/**/*.ts"
  ]
}

What I've Tried

  1. Verified that the fieldAuthorizePlugin is correctly registered in the makeSchema configuration.
  2. Deleted the generated folder and rebuilt the types using:
    npx prisma generate --schema ./src/infrastructure/database/schema.prisma
    
  3. Confirmed that authorize is present in the NexusGenPluginFieldConfig within the nexus.ts file.
  4. Added the generated folder to tsconfig.json under typeRoots.
  5. Instead of starting the server with ts-node and nodemon after this issue try ts-node-dev

Question

Why is TypeScript throwing an error about authorize not being a valid property of NexusOutputFieldConfig when:

  1. The plugin is correctly registered.
  2. The generated types (nexus.ts) include authorize.

How can I fix this so that TypeScript recognizes authorize in the field configuration?


Feel free to post this, and if you'd like, I can help refine it further!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.