'How to get data from GraphQL by filtering particular variable?
Below you can see my graphql method. From this, I can get the first data of the database that city is equal to 'London'. But I want to view all the data related to the city of London. Can anyone help?
//Graphql method
async getByCity(_,{city}){
try{
const ByCity = await Employee.findOne({city});
if(ByCity ){
return ByCity ;
}else{
throw new Error('Employee not found');
}
}catch(err){
throw new Error(err);
}
},
//Query
query($city: String!){
getByCity(city: $city) {
id
}
}
//Query Variable
{
"city": "london"
}
Solution 1:[1]
Employee.findOne({city}) seems like you are fetching only one entry here. I am not sure what library you are using. If you can specify the library can give a more comprehensive answer
try:
Employee.find({city})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lakshan Bandara |
