作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 graphql 架构、查询和突变。
我在架构中用“!”标记了必填字段
我如何创建突变来添加新客户端?
我真的需要再次编写相同的必填字段吗?
像createClient(contactMethod: String!,hearAbout: String!........): Client
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);
},
},
};
最佳答案
您不需要为每个突变编写相同的字段。您可以定义一个输入
类型。请看一下这个cheat sheet .
所以在你的情况下它可能看起来像:
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!
}
input ClientInput {
contactMethod: String!
hearAbout: String!
.....
}
`;
const mutationShard = `
removeClient(shortId : Int!): Client
createClient(clientInput: ClientInput!): Client
`;
关于graphql 必填字段方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207501/
在使用网站、加载内容等一段时间后,此消息显示“Fill:SelectCommand.Connection 属性尚未初始化”!我认为这是因为 sql 连接,但不确定......我想知道我能做些什么来防止
我是一名优秀的程序员,十分优秀!