当创建突变具有必填字段但更新没有时,我如何重新使用此输入类型?
How do I re-use this input type when the create mutation has required fields but updates don't?
输入类型对我来说似乎没用。 我正在尝试删除 NewUserInput 和 UserInput 之间的冗余,因为它们实际上是同一回事。
当我去“创建”用户时,所有字段都是必需的,但用户可能不想更新他们的所有字段,因此必填字段在更新突变中变为可选。如果我不填写所需的参数,GraphQL 显然不会让我 运行 查询。
我只是想在不应该重复使用的地方重复使用输入吗?
我能想到的重用输入的唯一方法是不要求任何东西,只验证解析器中的必填字段...这似乎也不对,所以我无法弄清楚输入类型是如何重用的.
架构:
type Mutation {
createUser(input: NewUserInput!): User
deleteUser(userId: ID!): User
updateUser(userId: ID!, input: UserInput!): User
}
input NewUserInput {
firstName: String!
email: String!
age: Int
}
input UserInput {
firstName: String
email: String
age: Int
}
type User {
id: ID!
firstName: String!
email: String!
age: Int
}
解析器(仅使用数组):
Mutation: {
createUser: (parent, args, context, info) => {
const userAlreadyExists = users.some((elem) => {
return elem.email == args.email;
});
if (userAlreadyExists) {
throw new Error("User already exists");
} else {
const newUser = {
id: newUserId(),
firstName: args.input.firstName,
email: args.input.email,
age: args.input.age,
};
users.push(newUser);
return newUser;
}
},
updateUser: (_, args) => {
const userIndex = users.findIndex((elem) => elem.id == args.userId);
users[userIndex] = { ...users[userIndex], ...args.input };
return users[userIndex];
},
The only way I can think to reuse inputs is to not have anything required and just verify required fields in the resolvers.
此方法用于WPGraphQL
...自定义输入验证错误...
Am I just trying to reuse the input where its not supposed to be reused?
是的,对于手动编写的代码来说似乎是一个问题,而更频繁地生成...WPGraphQL
仍然为创建和更新突变定义单独的输入类型...因为更新输入通常包含 id
字段...两者都可以单独扩展。
输入类型对我来说似乎没用。 我正在尝试删除 NewUserInput 和 UserInput 之间的冗余,因为它们实际上是同一回事。
当我去“创建”用户时,所有字段都是必需的,但用户可能不想更新他们的所有字段,因此必填字段在更新突变中变为可选。如果我不填写所需的参数,GraphQL 显然不会让我 运行 查询。
我只是想在不应该重复使用的地方重复使用输入吗?
我能想到的重用输入的唯一方法是不要求任何东西,只验证解析器中的必填字段...这似乎也不对,所以我无法弄清楚输入类型是如何重用的.
架构:
type Mutation {
createUser(input: NewUserInput!): User
deleteUser(userId: ID!): User
updateUser(userId: ID!, input: UserInput!): User
}
input NewUserInput {
firstName: String!
email: String!
age: Int
}
input UserInput {
firstName: String
email: String
age: Int
}
type User {
id: ID!
firstName: String!
email: String!
age: Int
}
解析器(仅使用数组):
Mutation: {
createUser: (parent, args, context, info) => {
const userAlreadyExists = users.some((elem) => {
return elem.email == args.email;
});
if (userAlreadyExists) {
throw new Error("User already exists");
} else {
const newUser = {
id: newUserId(),
firstName: args.input.firstName,
email: args.input.email,
age: args.input.age,
};
users.push(newUser);
return newUser;
}
},
updateUser: (_, args) => {
const userIndex = users.findIndex((elem) => elem.id == args.userId);
users[userIndex] = { ...users[userIndex], ...args.input };
return users[userIndex];
},
The only way I can think to reuse inputs is to not have anything required and just verify required fields in the resolvers.
此方法用于WPGraphQL
...自定义输入验证错误...
Am I just trying to reuse the input where its not supposed to be reused?
是的,对于手动编写的代码来说似乎是一个问题,而更频繁地生成...WPGraphQL
仍然为创建和更新突变定义单独的输入类型...因为更新输入通常包含 id
字段...两者都可以单独扩展。