Kevin.a
Kevin.a

Reputation: 4306

GraphQl API select specific values

I have the following query :

{
  allPeople {
    people {
      name
      filmConnection {
        films {
          title
        }
      }
    }
  }
}

I would like to select all people that have a film connection with the title a new hope. How do i select this specific thing from the API. I could also just get it like this and handle it in the code. But surely there is a better way.

What i'd expect :

{
  allPeople {
    people {
      name
      filmConnection {
        films {
          title : "a new hope"
        }
      }
    }
  }
}

That didnt work..

Try out here in this playground :

https://graphql.org/swapi-graphql?query=%7B%0A%20%20allPeople%20%7B%0A%20%20%20%20people%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20filmConnection%20%7B%0A%20%20%20%20%20%20%20%20films%20%7B%0A%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A

Upvotes: 0

Views: 978

Answers (1)

xadm
xadm

Reputation: 8428

graphql queries ... are not for building [sql] queries ;)

this is more for defining shape of required data

parameter CAN BE passed to deeper child (f.e. can be used to filter films)

{
  allPeople {
    people {
      name {
        filmConnection {    
          films(title_eq:"a new hope") { 
            title
          }
        }
      }
    }
  }
}

... if API supports this kind of filtering for films

... but it won't filter people - you'll have all people AND filtered films (for all people) because filters won't work on parents.

You CAN have custom API that will be this kind of filtering aware, f.e.

{
  allPeople {
    people(connectedFilmTitle_eq:"a new hope") {
      name {
        filmConnection {    
          films { 
            title
          }
        }
      }
    }
  }
}

customized (not automatically gnerated) peolpe resolver can make appriopriate query [on joined tables] and return structure you're asking for.

In that case probably you don't need deeper structures - ...filmConnection { films { title - you know this data earlier (filter parameters).

... but probably you have a many2many relation and cen reverse this query:

{
  allFilms {
    films(title_eq:"a new hope") {
      title {
        peoleConnection {    
          people { 
            name
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions