来自 React 的对象数组 Apollo Server 和 post

Array of Objects Apollo Server and post from react

所以我想弄清楚如何将对象数组从 POST 请求传递到 AWS lambda 上的 apollo 服务器。

我已经检查过了,但这不是同一个问题

post 请求如下所示...

    api.post('query', { query : `mutation {saveNewItem(description: "${description}", specials: ${JSON.stringify(specials)}){name}}`})
// comment to get rid of silly scroll bar overlapping code

架构看起来像这样...

    const { gql } = require('apollo-server-lambda')

    const typeDefs = gql`
      type ShoppingItem {
        description: String
        specials: [Specials]
      }

      input Specials {
        description: String
        price: String
        qty: String
        saved: String
      }

  type Mutation {
    saveNewItem(description: String!, specials: [Specials]) : ShoppingItem
  }
`

示例特价商品如下所示...

[{ // Object One
description: "First One"
price: "1.00"
qty: "1" 
saved: "false"
},{ // Object two
description: "Second One"
price: "1.00"
qty: "1" 
saved: "false"
}]

我目前得到的错误是...

'Error: The type of ShoppingItem.specials must be Output Type but got: [Specials].',
  'at assertValidSchema (/Users/me/Desktop/Projects/app/build/node_modules/graphql/type/validate.js:71:11)',

如果我将其更改为正常 "type" 它会抱怨它不是输入类型。

我也浏览过 apollo 服务器文档,但不太明白我做错了什么?

请注意,正如 Daniel 在评论中提到的,虽然技术上 "duplicate" 给出的答案是正确的,但此处提供的信息质量更高,对面临问题的人更有用(在我看来)

您只能对输入使用输入类型 (GraphQLInputObjectType),对输出使用对象类型 (GraphQLObjectType)。您正在使用 Specials 作为两者:作为 ShoppingItem 中字段 specials 的输出类型和突变参数 specials 中的输入类型。为此,您需要两种类型。这样做的原因是输出类型(可以)具有解析器(在您的情况下,这实际上是从 apollo 服务器抽象出来的)。您将必须创建两种不同的类型:

type ShoppingItem {
    description: String
    specials: [Specials]
}

type Specials {
    description: String
    price: String
    qty: String
    saved: String
}

input SpecialsDraft {
    description: String
    price: String
    qty: String
    saved: String
}

type Mutation {
    saveNewItem(description: String!, specials: [SpecialsDraft]) : ShoppingItem
}