任何人都可以使用他们想要的任何字段查询用户,但是出于某种原因 q_id 字段解析器父类型包括所有字段。如何修复父类型并使其对每个字段都可以为空?
我只想在 q_is_public 时加载 q_id (它可以工作,但我需要记住,parant 字段可以是未定义的)。
import { extendType, inputObjectType, objectType, arg } from '@nexus/schema'
export const User = objectType({
name: 'User',
definition(t) {
t.model.id()
t.model.nickname()
t.model.q_is_public()
t.field('q_id', {
type: 'Int',
nullable: true,
resolve: (parent) => {
/*
parent type is:
{
id: number;
nickname: string;
q_is_public: boolean;
}
parent type actual:
{
id?: number;
nickname?: string;
q_is_public?: boolean;
q_id?: boolean;
... and so on, what user requested
}
*/
return parent?.q_is_public ? parent?.q_id : null
},
})
},
})
const FindOneUserInput = inputObjectType({
name: 'FindOneUserInput',
definition(t) {
t.int('id', { required: true })
},
})
export const FindOneUser = extendType({
type: 'Query',
definition(t) {
t.field('findOneUser', {
type: 'User',
nullable: true,
args: { where: arg({ type: FindOneUserInput, required: true }) },
resolve: async (_parent, { where }, { db }, _info) => {
// here will be used pal.select instead all fields load, to load only selected fields
const res = await db.user.findOne({
where,
})
return res
},
})
},
})