0
votes

My GraphQL schema looks like this:

type Outer {
   id: ID
   name: String,
   inner: [Inner]
}

type Inner {
   id: ID
   name: String
}

input OuterInput {
   name: String,
   inner: InnerInput
}

Input InnerInput {
   name: String
}

type Query {
   getOuter(id: ID): Outer
}

type Mutation {
   createOuter(outer: OuterInput): Outer
}

In database, Outer object is store this way:

{
   id: 1,
   name: ‘test’,
   inner: [5, 6] //IDs of inner objects. Not the entire inner object
}

Outer and Inner are stored in separate database collections/tables (I’m using DynamoDB)

My resolvers look like this:

Query: {
   getOuter: (id) => {
      //return ‘Outer’ object with the ‘inner’ IDs from the database since its stored that way
   }
}


Outer: {
   inner: (outer) => {
      //return ‘Inner’ objects with ids from ‘outer.inner’ is returned
   }
}

Mutation: {
   createOuter: (outer) => {
      //extract out the IDs out of the ‘inner’ field in ‘outer’ and store in DB.
   }
}

OuterInput: {
   inner: (outer) => {
      //Take the full ‘Inner’ objects from ‘outer’ and store in DB.
     // This is the cause of the error
   }
}

But I’m not able to write a resolver for the ‘inner’ field in ‘OuterInput’ similar to what I did for ‘Outer’. GraphQL throws an error saying

Error: OuterInput was defined in resolvers, but it's not an object

How can I handle such a scenario if writing resolvers for Input types are not allowed?

1

1 Answers

0
votes

Resolvers are not necessary for input types. Input types are only passed in as values to field arguments, which means their value is already know -- it does not need to be resolved by the server, unlike values for fields being returned in the query.

The signature for every resolver is the same and includes 4 parameters: 1) the parent object, 2) the arguments for the field being resolved, 3) the context and 4) an object containing more detailed information about the request as a whole.

So to access the outer argument for your createOuter mutation:

Mutation: {
   createOuter: (root, args, ctx, info) => {
      console.log('Outer: ', args.outer)
      console.log('Outer name: ', args.outer.name)
      console.log('Inner: ', args.outer.inner)
      console.log('Inner name: ', args.outer.inner.name)
      // resolver logic here
   }
}