0

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

4
  • This graphql API doesn't allow passing arguments to the title field, so you can't do that. Commented Feb 5, 2020 at 16:56
  • @goto1 how would i do this in a case like this normally Commented Feb 5, 2020 at 17:42
  • 1
    just like you suggested, you'd have to handle it in your code. Commented Feb 5, 2020 at 17:43
  • @goto1 thank you ! Commented Feb 5, 2020 at 17:43

1 Answer 1

1

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
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

but isnt films a parent , how come you can have a parameter here?
it's about general graphql possibilities and rules - not this particular API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.