You can have a single end point that returns a GraphQLList
Type. This list can contain either one object or however many.
In your case, that single end point will be clients
. You just have to use your backend to see if the consumer of your GraphQL API has supplied any arguments i.e. clientId
. If the clientId
has been supplied, filter your clientRepo
by that supplied clientId
. Otherwise return the whole list (repo) of clients
.
clients: {
type: new GraphQLList(clientType), <--- Note this is a GraphQLList type
args: {
id: {
type: GraphQLInt
},
},
resolve: (parent, args) => {
if (args.id) {
return clientRepo.find(args.id);
}
return clientRepo.findAll();
}
}
You might want to visit the following links:
https://jaketrent.com/post/return-array-graphql/
https://stackoverflow.com/a/52773152/4195803