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)

4
  • 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 and here). 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. Commented Jan 10, 2019 at 0:05
  • Thanks Daniel, let me have a read, I may be a bit tired. Commented Jan 10, 2019 at 0: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. Commented Jan 10, 2019 at 0:08
  • Can I suggest not closing this because your explanation is a lot better as is the one below. Commented Jan 10, 2019 at 0:17

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Sorry, I think Daniel's response is great and pointing to the right direction. Saw it after posting :(
So I'm getting expected SpecialsDraft got object, one sec just checking my code.
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}}\"}"
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.