GraphQL custom resolver in V4

Thanks to @Convly for his response under this GitHub issue:

Our goal with the V4 was to decouple REST & GraphQL. Previously GQL resolvers were calling REST controllers which caused a lot of troubles, now both REST controllers & GraphQL resolvers are using the entity service API internally.

That’s why I would advise against calling your REST controller directly from the resolver.
Instead what you could do is smth like

strapi.entityService.findMany("api::restaurant.restaurant", transformedArgs)

Then in the response, you could do:

const { toEntityResponseCollection } = strapi.plugin('graphql').service('format').returnTypes; 
if (data) { 
    return toEntityResponseCollection(data, { args: transformedArgs, resourceUID:"api::restaurant.restaurant" }) 
}  else { 
    throw NotFoundError(); 
}

On a side note: toEntityResponseCollection is basically just a wrapper that matches the expected format for the findMany output which is { value, args, resourceUID }. For a single resource you can use toEntityResponse.

3 Likes