Home > Software design >  How to get data from GraphQL by filtering particular variable?
How to get data from GraphQL by filtering particular variable?

Time:02-17

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

CodePudding user response:

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

  • Related