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:
- 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',
},
})
- 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)
},
})
},
})
- 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
}
- Generated Type Definitions:
The
nexus.tsfile 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.
- 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
- Verified that the
fieldAuthorizePluginis correctly registered in themakeSchemaconfiguration. - Deleted the
generatedfolder and rebuilt the types using:npx prisma generate --schema ./src/infrastructure/database/schema.prisma - Confirmed that
authorizeis present in theNexusGenPluginFieldConfigwithin thenexus.tsfile. - Added the
generatedfolder totsconfig.jsonundertypeRoots. - Instead of starting the server with
ts-nodeandnodemonafter this issue tryts-node-dev
Question
Why is TypeScript throwing an error about authorize not being a valid property of NexusOutputFieldConfig when:
- The plugin is correctly registered.
- The generated types (
nexus.ts) includeauthorize.
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!