1

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)

Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • 2
    This might not be an exact duplicate, but there's a number of these "input where output" and "output where input" questions out there. Here's just two ([here](https://stackoverflow.com/questions/52744900/apollo-graphql-type-must-be-input-type/52745448) and [here](https://stackoverflow.com/questions/46158288/graphql-how-to-reuse-same-type-for-query-and-mutation/46159440)). As outlined in these threads, you need to define both an `input` and a `type ` -- the former for arguments and the latter for actual query responses. – Daniel Rearden Jan 10 '19 at 00:05
  • Thanks Daniel, let me have a read, I may be a bit tired. – Mrk Fldig Jan 10 '19 at 00:06
  • My comment here is Apollo-server is a bit different to these answers although I'm trying to figure out how to apply the logic, as you said it could work I just get my head round it. – Mrk Fldig Jan 10 '19 at 00:08
  • Can I suggest not closing this because your explanation is a lot better as is the one below. – Mrk Fldig Jan 10 '19 at 00:17

1 Answers1

2

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
}
Herku
  • 7,198
  • 27
  • 36
  • I'm such an idiot, I literally twigged that after the comments Daniel above, give me two ticks and let me test it. Thank you. – Mrk Fldig Jan 10 '19 at 00:11
  • Sorry, I think Daniel's response is great and pointing to the right direction. Saw it after posting :( – Herku Jan 10 '19 at 00:14
  • So I'm getting expected SpecialsDraft got object, one sec just checking my code. – Mrk Fldig Jan 10 '19 at 00:14
  • Ok so the query comes through as(this is logging the event body in the api: "{\"query\":\"mutation {saveNewItem(description: \\\"Something\\\", specials: [{\\\"description\\\":\\\"Goo\\\",\\\"qty\\\":\\\"1\\\",\\\"price\\\":\\\"1\\\",\\\"saved\\\":false}]){name}}\"}" – Mrk Fldig Jan 10 '19 at 00:24
  • And that gives expected Name found string description. I'll mark this as correct for now as you've answered the question I asked so Thanks! – Mrk Fldig Jan 10 '19 at 00:26