GraphQL MongoDB embed payload type within a custom type

Hi all,

I was wondering if it’s possible embed an existing type into a payload type for a custom resolver.

So, atm I’m using an existing payload type in the following setup:

  • Existing Type (List)
  • [collection_name].

This works great, as I’m able to output my aggregation pipeline and use GraphQL to specify the fields I’m after. However, now I want to also output the total count, so I was wondering what the best way of doing this would be.

It would be like, all within 1 output:

  • embed → Existing Type (List) for my [collection_name]
  • int → totalCount

Hi Joseph,

You can define a new custom payload type that includes both the existing list type and an additional field for the total count.

In the Payload Type dropdown of your custom resolver, select ‘Custom Type’. Here’s how you can define the new payload type:

type CustomPayload {
  items: [ExistingType]
  totalCount: Int!

For more guidance on what this looks like in the form of a JSON schema, take a look through our Docs here.

1 Like