gpt4 book ai didi

graphql 必填字段方法

转载 作者:行者123 更新时间:2023-12-02 16:14:30 27 4
gpt4 key购买 nike

这是我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com