Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
        }
    },


//Quary
query($city: String!){
  getByCity(city: $city) {
    id
  }
}


//Quary Variable
{
  "city": "london"
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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})

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading