繁体   English   中英

graphql突变如何在apollo服务器表达式中接受参数数组?

[英]How can a graphql mutation accept an array of arguments with apollo server express?

我正在前端创建一个购物车,并且要将购物车中的所有项目作为数组传递给后端的我的变异addLicense,因此不必进行多个API调用。 这可能吗? 如果是这样,怎么办,或者我有什么选择?

我试图在这样的参数周围加上方括号

extend type Mutation {
    addLicense([
        licenseType: String!
        licenseAmount: String!
        isActive: Boolean
        expirationDate: Date!
    ]): License!
}

我也尝试将对象类型设置为参数

extend type Mutation {
    addLicense(
    license: [License]
): License!

第一次尝试会引发此错误

/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)

另一个尝试抛出此错误

Error: The type of Mutation.addLicense(license:) must be Input Type but got: [License].

您的突变:

type Mutation {
    """
    Add multiple items to basket
    """
    addLicenses(licenses: [License!]!): LicenseMutationBatchResult
}

您的界面输入:

input License {
    licenseType: String!
    licenseAmount: String!
    isActive: Boolean
    expirationDate: Date!
}

无论你给什么:

type LicenseMutationBatchResult {
    ...whatever you give back here...
}

我在GraphQL Slack上获得了一些建议。 我通过添加新的输入类型解决了它。

input LicenseInput {
    id: ID
    licenseType: String!
    licenseAmount: String!
    isActive: Boolean
    expirationDate: Date
    course: String!
    organizationID: String!
    price: Int!
}

这样我就可以像这样添加我的突变

extend type Mutation {
    addLicense(
        license: [LicenseInput] <-
    ): License!
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM