So i'm trying to figure out how to pass an array of objects from a POST request to apollo server on AWS lambda.
I've checked this out but it's not the same problem Array of objects convert into object of objects when I use Apollo
The post request looks like this...
api.post('query', { query : `mutation {saveNewItem(description: "${description}", specials: ${JSON.stringify(specials)}){name}}`})
// comment to get rid of silly scroll bar overlapping code
Schema looks like this...
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
}
`
example Specials looks like this...
[{ // Object One
description: "First One"
price: "1.00"
qty: "1"
saved: "false"
},{ // Object two
description: "Second One"
price: "1.00"
qty: "1"
saved: "false"
}]
The error I get currently is...
'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)',
If I change it to a normal "type" it complains about it not being Input type.
I've also been through the apollo server docs and can't quite see what I'm doing wrong?
Please that as mentioned by Daniel in comments whilst technically the "duplicate" answer given is correct the information offered here is far more high quality and useful to people facing the problem(in my opinion)
答案 0 :(得分:0)
您只能将输入类型用于输入(GraphQLInputObjectType),将对象类型用于输出(GraphQLObjectType)。您同时使用Specials
:作为specials
中字段ShoppingItem
的输出类型,以及作为突变参数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
}