gpt4 book ai didi

javascript - Apollo GraphQL 嵌套突变

转载 作者:行者123 更新时间:2023-12-02 22:08:54 35 4
gpt4 key购买 nike

需要一些有关嵌套突变的帮助。

抽象场景是这样的:

我想结合 apollo-server 上的 2 个突变调用,首先创建一个 Customer,然后为该客户创建一个 AddressAddress 突变需要一个 customerID 才能执行此操作,而且还具有它需要访问的原始整体突变的信息。

这是通用代码:

makeExecutableSchema({
typeDefs: gql`
type Mutation {
createCustomerWithAddress(customer: CustomerRequest!, address: AddressRequest!): Response
}
input CustomerRequest {
name: String!
}

input AddressRequest {
address: String!
city: String!
state: String!
country: String!
}

type Response {
customerID: Int!
addressID: Int!
}
`,
resolvers: {
Mutation: {
createCustomerWithAddress: async (_, {customer}, context, info) => {
return await api.someAsyncCall(customer);
}
},
Response: {
addressID: async(customerID) => {
// how do we get AddressRequest here?
return await api.someAsyncCall(customerID, address);
}
}
}
})



我没有从原始代码中展示很多复杂性,但我想要了解的是如何通过子突变访问请求参数的根源(如果可能的话)。我真的不想将 address 从顶部突变传递到子突变。

最佳答案

您不需要解析器中的Response字段。 createCustomerWithAddress 应返回形状类似于 Response 的对象。

resolvers: {
Mutation: {
createCustomerWithAddress: async (_, {customer, address}, context, info) => {
// create customer
const customerId = await api.CreateCustomer(customer);
// create address and assign customerId
const addressId = await api.CreateAddress({ ...address, customerId });
// return response
return { customerId, addressId };
}
},
}

关于javascript - Apollo GraphQL 嵌套突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59621188/

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