Nexus插件报错:类型无法分配给OutputScalarConfig
Nexus Declarative Wrapping Plugin 类型错误:'nullable' 属性不存在于 OutputScalarConfig 中
可能原因与解决方案
1. 版本兼容性问题
确保nexus-plugin-declarative-wrapping与@nexus/schema(或nexus)的版本匹配。插件对核心包版本有明确要求,比如部分版本需要@nexus/schema >= 0.20.0。
- 执行以下命令查看当前版本:
npm list @nexus/schema nexus-plugin-declarative-wrapping - 若版本不匹配,升级或降级对应包到兼容版本(参考插件官方文档的版本要求)。
2. 插件顺序错误
Nexus插件的执行顺序会影响类型生成与schema处理,declarativeWrappingPlugin需要优先加载,才能正确扩展字段配置的类型定义。
调整plugins数组顺序,将其放在最前面:
export const schema = makeSchema({ types, plugins: [ declarativeWrappingPlugin(), // 移至首位 nullabilityGuardPlugin({ onGuarded: ({ ctx, info }) => { console.error( `Error: Saw a null value for non-null field ${info.parentType.name}.${info.fieldName}` ); }, fallbackValues: { String: () => undefined, ID: ({ info }) => undefined, Boolean: () => undefined, Upload: () => undefined } }), nonNullOptionalFieldPlugin() ], outputs: { schema: __dirname + '/../schema.graphql', typegen: __dirname + '/generated/nexus.ts' }, contextType: { module: require.resolve('../context'), export: 'Context' }, sourceTypes: { modules: [ { module: '@prisma/client', alias: 'prisma' } ] } }) as unknown as GraphQLSchema;
3. 类型生成文件未更新
虽然执行了生成命令,但旧的类型文件可能未被覆盖,导致类型定义未包含nullable属性。
- 删除旧的类型生成文件(比如
generated/nexus.ts) - 重新执行生成命令:
npm run generate:nexus - 检查生成的
nexus.ts文件,确认OutputScalarConfig类型中是否包含nullable字段。如果仍未出现,说明插件未正确注入类型扩展,需重新检查安装与导入。
4. 插件未正确安装或导入
- 确认已安装插件:
npm install nexus-plugin-declarative-wrapping - 确认在schema文件中正确导入插件:
import { declarativeWrappingPlugin } from 'nexus-plugin-declarative-wrapping';
5. 排除其他插件冲突
暂时移除nullabilityGuardPlugin和nonNullOptionalFieldPlugin,仅保留declarativeWrappingPlugin,重新生成schema并检查类型错误是否消失。如果问题解决,再逐个添加其他插件,排查冲突来源。
内容的提问来源于stack exchange,提问作者Arif
相关产品推荐
相关产品推荐

