[英]How to design schema in GraphQL considering input types for mutation?
这个架构看起来是否正确?
type User {
id : ID!
username : String!
email : String!
name : String!
}
input UserInput {
username : String!
email : String!
name : String!
}
mutation createNewUser($usr: UserInput!) {
createUser(user: $usr)
}
由于用户的内部ID将在用户创建时分配,此模式中是否应该有单独的type
和input
,或者可以input
用户 ? 因此架构看起来像这样
input User {
id: ID
username : String!
email : String!
name : String!
}
mutation createNewUser($usr: User!) {
createUser(user: $usr) : User
}
在第一种方法中你是对的。 id
字段不能为null( id: ID!
),当您需要创建用户时,它不能有值,因此您需要为其提供另一种输入类型。
正如mattdionis也指出的那样, 文档中有一个非常类似的样本。
你的架构应该是
const typeDefinitions = `
type User {
id : ID!
username : String!
email : String!
name : String!
}
input UserInput {
username : String!
email : String!
name : String!
}
type Mutation {
createUser(user: UserInput) User
}
schema {
mutation:Mutation
}
`;
export default [typeDefinitions];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.