1
這是我的graphql模式,查詢和變異。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);
},
},
};
謝謝您的回答。我讀過它。 但是可以將輸入和輸出結合起來嗎?在很多情況下,這將是代碼重複。任何想法? – Gemmi
我不認爲這是可能的 –