Hello @thor1n
I’m not an expert in GraphQL, but I’m using a custom query & resolver for this approach.
First of all, I’ve create a file at ./api/*/config/schema.graphql.js
or for plugins in ./extensions/*/config/schema.graphql.js
.
And defined a new query and a resolver for it as shown bellow:
const { sanitizeEntity } = require('strapi-utils');
const _ = require('lodash');
module.exports = {
query: `
customOrders(where: JSON): JSON
`,
resolver: {
Query: {
customOrders: {
description: 'Find orders',
resolverOf: 'application::order.order.find', // Will apply the same policy on the custom resolver as the controller's action `find`.
resolver: async (obj, options, ctx) => {
//Get orders by using filters from 'where'
let entities = await strapi.query('order').find(options.where);
//Will return an error custom message if there are no orders
//Also here you can do a custom validation.
if (_.isEmpty(entities)) {
return {
status: '404',
message: 'No orders found'
}
}
//Will return sanitized orders
return {
status: '200',
orders: entities.map(entity =>
sanitizeEntity(entity, {
model: strapi.models.order,
})
)
};
},
},
},
},
};
Now in GraphQL, I use the newly created Query: customOrders
, it returns orders if they are found and custom error message if there are no orders: