一个 graphql 突变如何接受带有 apollo 服务器表达的一系列参数



我正在前端创建一个购物车,我想将购物车中的所有项目作为数组传递给后端的突变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!
}

最新更新