
在graphql与prisma结合开发时,实现嵌套数据创建(如同时创建用户及其关联档案)是常见需求。本文旨在解决在graphql突变中尝试进行嵌套创建时,因输入结构不匹配导致“字段未提供”的错误。我们将详细解析问题根源,并提供正确的graphql输入结构和prisma解析器实现方式,确保数据能够无缝同步创建。
在使用GraphQL进行数据操作时,我们经常需要一次性创建多个相关联的数据模型。例如,在创建用户时,可能需要同时创建其对应的个人档案(Profile)。Prisma ORM提供了强大的嵌套写入功能来支持这一点。然而,在将GraphQL输入与Prisma操作结合时,可能会遇到“字段未提供”的错误,这通常是由于GraphQL客户端发送的输入结构与GraphQL模式中定义的输入类型不匹配造成的。
考虑以下场景:我们有一个User模型和一个Profile模型,User与Profile之间存在一对一关系。我们希望通过一个addUser突变同时创建用户和其档案。
原始的GraphQL突变尝试(导致错误):
mutation {
addUser(
input: {
firstName: "Jane"
lastName: "Doe"
roleId: "bfb3d29a-379e-4558-b2fd-af98b666c100"
username: "jdoe"
email: "jane.doe@example.com"
password: "1234567890"
profile: {
create: { # 这里的 `create` 层级是问题的根源
addressOne: "Runda, Kenya"
addressTwo: "Murang'a, Kenya"
dob: "12-12-1990"
zip: "22333-00100"
}
}
}
) {
id
firstName
lastName
profile {
id
dob
}
}
}GraphQL模式定义:
input addUserInput {
firstName: String!
middleName: String
lastName: String
username: String!
email: String
roleId: String!
password: String
profile: addProfileInput # 注意这里,profile直接期望 addProfileInput 类型
}
input addProfileInput {
addressOne: String!
addressTwo: String!
zip: String!
dob: String!
}
type Mutation {
signUp(input: addUserInput!): AuthPayload
}当执行上述突变时,会收到类似"message": "Field \"addProfileInput.addressOne\" of required type \"String!\" was not provided."的错误。这个错误信息非常关键,它指出addProfileInput类型期望的addressOne字段没有被提供。但从突变请求来看,addressOne明明在profile.create内部提供了。
问题分析:
问题的核心在于GraphQL模式定义与客户端发送的突变输入结构之间的不匹配。 在addUserInput中,profile字段被定义为addProfileInput类型:profile: addProfileInput。这意味着addUserInput期望profile字段的值直接就是一个addProfileInput对象,而不是一个包含create字段的对象,而create字段内部再包含addProfileInput对象。
尽管Prisma在其API中支持create: { ... }这种语法来表示嵌套创建,但这仅仅是Prisma客户端API的约定,并非GraphQL模式定义input类型时的通用规则。GraphQL模式应该准确地描述客户端期望发送的数据结构。
要解决这个问题,我们需要移除GraphQL突变输入中profile字段下的create: { ... }层级,直接将addProfileInput的数据作为profile字段的值。
修正后的GraphQL突变:
mutation {
addUser(
input: {
firstName: "Jane"
lastName: "Doe"
roleId: "bfb3d29a-379e-4558-b2fd-af98b666c100"
username: "jdoe"
email: "jane.doe@example.com"
password: "1234567890"
profile: { # 直接提供 addProfileInput 的内容
addressOne: "Runda, Kenya"
addressTwo: "Murang'a, Kenya"
dob: "12-12-1990"
zip: "22333-00100"
}
}
) {
id
firstName
lastName
profile {
id
dob
}
}
}通过移除create层级,现在客户端发送的profile字段的数据结构完全符合addUserInput中profile: addProfileInput的定义。
在解析器中,Prisma的嵌套写入语法是正确的,它需要create: { ... }来指示创建关联记录。因此,解析器代码无需修改,它会正确地处理传入的input.profile数据。
Prisma解析器示例(无需修改):
signUp: async (_, { input }) => {
const password = await hash(input.password, 10); // 假设 hash 是一个密码哈希函数
const newUser = await prisma.user.create({
data: {
firstName: input.firstName,
middleName: input.middleName,
lastName: input.lastName,
roleId: input.roleId,
username: input.username,
email: input.email,
password,
profile: {
// Prisma 内部需要 'create' 关键字来处理嵌套创建
create: {
addressOne: input.profile.addressOne, // 从 input.profile 中获取数据
addressTwo: input.profile.addressTwo,
zip: input.profile.zip,
dob: input.profile.dob,
},
},
},
include: {
profile: {
select: {
dob: true,
},
},
},
});
return newUser;
},在上述解析器中,input参数将包含以下结构(基于修正后的GraphQL突变):
{
"firstName": "Jane",
// ...其他用户字段
"profile": {
"addressOne": "Runda, Kenya",
"addressTwo": "Murang'a, Kenya",
"zip": "22333-00100",
"dob": "12-12-1990"
}
}解析器通过input.profile可以直接访问到addProfileInput中的所有字段,然后将其包装在Prisma所需的create: { ... }结构中,传递给prisma.user.create方法。
通过以上调整,您现在可以成功地在GraphQL突变中实现用户和档案的嵌套创建,同时避免“字段未提供”的错误。
以上就是GraphQL嵌套突变与Prisma:解决“字段未提供”错误的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号