【发布时间】:2019-08-15 09:11:30
【问题描述】:
所以我在尝试解决包含来自其他输入类型的嵌套输入类型的突变时遇到问题,如果我对模型进行错误设计,请纠正我。
这是突变,我正在使用 Playground 进行检查:
mutation{
createOrganization(
name: "Bitas"
staff: [
{
firstName: "Albert"
lastName: "Chavez"
position: "Developer"
contactInformation:[
{
email: "hola@mail.com"
phone:"9187631"
linkedin: "whatever"
},
{
email: "hola2@mail.com"
phone:"91876312"
linkedin: "whatever2"
}
]
}
]
){
name
staff{
firstName
contactInformation{
email
}
}
}
}
这种突变正在创建组织和员工之间的关系,同时也在创建员工和联系信息之间的关系......这是架构:
type Organization {
id: ID!
name: String!
staff: [Employee!]!
}
type Employee {
id: ID!
firstName: String!
lastName: String!
position: String!
contactInformation: [ContactInfo!]!
belongsToOrg: Organization
}
input employeeInput {
firstName: String!
lastName: String!
position: String!
contactInformation: [contactInfoInput!]!
belongsToOrg: ID
}
type ContactInfo {
id: ID!
email: String!
phone: String!
linkedin: String!
belongsTo: Employee!
}
input contactInfoInput {
email: String!
phone: String!
linkedin: String!
}
如果我没有正确创建突变,请纠正我
type Mutation {
createOrganization(name: String!, staff: [employeeInput!]): Organization!
createEmployee(firstName: String!, lastName: String!, position:String!, contactInformation: [contactInfoInput!]!): Employee!
}
下面是要创建的函数:
function createEmployee(parent, args, context, info) {
return context.prisma.createEmployee({
firstName: args.firstName,
lastName: args.lastName,
position: args.position,
contactInformation: {
create: args.contactInformation
},
})
}
function createOrganization(parent, args, context, info) {
return context.prisma.createOrganization({
name: args.name,
staff: {
create: args.staff
}
})
}
function staff(parent, args, context) {
return context.prisma.organization({id: parent.id}).staff();
}
function contactInformation(parent, args, context) {
return context.prisma.employee({id: parent.id}).contactInformation()
}
function belongsTo(parent, args, context) {
return context.prisma.contactInfo({id: parent.id}).belongsTo()
}
所以当我在 Playground 上遇到突变时,它给了我错误:
原因:“staff.create[0].contactInformation”应为“ContactInfoCreateManyWithoutEmployeeInput”,找不到对象。
请有人解释一下这是什么意思?我没有正确设计架构或关系吗?或者可能是因为嵌套输入的级别太多? 如果我控制台记录 createOrganization 函数上的 contactInformation 字段,则该值未定义。
注意:创建 Employee 时,嵌套的变更工作正常。
提前致谢。
【问题讨论】:
标签: javascript graphql prisma