[英]How can a graphql mutation accept an array of arguments with apollo server express?
I'm creating a shopping cart in my frontend, and I want to pass all the items in the shopping cart as an array to my mutation addLicense on my backend, so I don't have to make multiple API calls. 我正在前端创建一个购物车,并且要将购物车中的所有项目作为数组传递给后端的我的变异addLicense,因此不必进行多个API调用。 Is this possible?
这可能吗? If so, how, or what are my options?
如果是这样,怎么办,或者我有什么选择?
I've tried to add square brackets around the arguments like this 我试图在这样的参数周围加上方括号
extend type Mutation {
addLicense([
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date!
]): License!
}
I've also tried to set the object type as an argument 我也尝试将对象类型设置为参数
extend type Mutation {
addLicense(
license: [License]
): License!
First attempt throws this error 第一次尝试会引发此错误
/app/node_modules/graphql/language/parser.js:1463
throw (0, _error.syntaxError)(lexer.source, token.start, "Expected ".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));
^
GraphQLError: Syntax Error: Expected Name, found [
at syntaxError
(/app/node_modules/graphql/error/syntaxError.js:24:10)
The other attempt throws this error 另一个尝试抛出此错误
Error: The type of Mutation.addLicense(license:) must be Input Type but got: [License].
your mutation: 您的突变:
type Mutation {
"""
Add multiple items to basket
"""
addLicenses(licenses: [License!]!): LicenseMutationBatchResult
}
your interface input: 您的界面输入:
input License {
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date!
}
whatever you give back: 无论你给什么:
type LicenseMutationBatchResult {
...whatever you give back here...
}
I got some advices over at the GraphQL Slack. 我在GraphQL Slack上获得了一些建议。 I solved it by adding a new Input Type.
我通过添加新的输入类型解决了它。
input LicenseInput {
id: ID
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date
course: String!
organizationID: String!
price: Int!
}
I was then able to add my mutation like so 这样我就可以像这样添加我的突变
extend type Mutation {
addLicense(
license: [LicenseInput] <-
): License!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.