Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

graphql - Array of Objects Apollo Server and post from react

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can only use input types for input (GraphQLInputObjectType) and object types for output (GraphQLObjectType). You are using Specials as both: As output type for the field specials in ShoppingItem and as input type in mutation argument specials. To do this you need two types. The reason for this is that output types (can) have resolvers (this is actually abstracted away from apollo server in your case). You will have to create two different types:

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...