graphql必填字段方法
这是我的graphql模式、查询和转换 我用“!”标记了架构中的必填字段 如何创建变异以添加新客户机 我真的需要再次写入相同的必填字段吗 像graphql必填字段方法,graphql,apollo,apollo-server,Graphql,Apollo,Apollo Server,这是我的graphql模式、查询和转换 我用“!”标记了架构中的必填字段 如何创建变异以添加新客户机 我真的需要再次写入相同的必填字段吗 像createClient(contactMethod:String!、hearAbout:String!…):客户机 您不需要为每个变异编写相同的字段。您可以定义输入类型。请看看这个 因此,在您的情况下,它可能看起来像: const typeShard=` 类型ClientProfile{ 名字:字符串! 姓:弦! 地址:String 语言:字符串! }
createClient(contactMethod:String!、hearAbout:String!…):客户机
您不需要为每个变异编写相同的字段。您可以定义输入
类型。请看看这个
因此,在您的情况下,它可能看起来像:
const typeShard=`
类型ClientProfile{
名字:字符串!
姓:弦!
地址:String
语言:字符串!
}
类型客户端{
_id:字符串
isEdit:布尔型
createdAt:String
短ID:Int
简介:ClientProfile
注释:字符串
联系方式:字符串!
听说:绳子!
leadAgentId:String
布兰奇:绳子!
}
输入客户端输入{
联系方式:字符串!
听说:绳子!
.....
}
`;
常数突变硬=`
removeClient(shortId:Int!):客户端
createClient(clientInput:clientInput!):客户端
`;代码>谢谢你的回答。我读到了。但有可能把输入和输出结合起来吗?在许多情况下,这将是代码重复。。有什么想法吗?我认为这是不可能的
const typeShard = `
type ClientProfile {
name: String!
surname: String!
address: String
language: String!
}
type Client {
_id: String
isEdit: Boolean
createdAt: String
shortId: Int
profile: ClientProfile
comments: String
contactMethod: String!
hearAbout: String!
leadAgentId: String
branchId: String!
}
`;
const queryShard = `
getAllClients: [Client]
`;
const mutationShard = `
removeClient(shortId : Int!): Client
createClient(contactMethod: String!, hearAbout: String! ......... ): Client
`;
const resolvers = {
Query: {
getAllClients: () => MongoClients.find().fetch(),
},
Mutation: {
removeClient(root, { shortId }) {
const client = MongoClients.findOne({ shortId });
if (!client) throw new Error(`Couldn't find client with id ${shortId}`);
MongoClients.remove({ shortId });
return client;
},
createClient: (_, args) => {
return MongoClients.insert(args);
},
},
};