简体   繁体   中英

How I can specify GraphQL query for Apollo Client and get items which property has an occurrence of the appropriate string

I did not work with GraphQL before the last days. I use Apollo Client with graphql-tag . Now I can get the list of cities from a server with this query:

query {
    cities(country:countryName, limit:30){
      id
      name
  }    
}

But I want to get only the cities which names ( name property) have an occurrence of the string that user typed. It should work like autocomplete. For example when user type san I want to get array of objects for San Francisco, San Jose, San Juan... . Is it possible to do it on the client side, or I should to ask the backend developer for it?

The country arguments should be a variable and it'll compute from the component's props.

query ($country: String) {
  cities(country: $country, limit: 30) {
    id
    name
  }
}

Your input should sit in the component's parent which will pass down the value through the country prop.

Check out this sandbox for an example. You can see the schema and resolvers here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM