Search⌘ K

Dynamic Query Using Directives

Learn about dynamic GraphQL queries that use directives.

GraphQL directive

In the last lesson, we learned how to utilize GraphQL variables to filter pizzas based on a keyword entered by the user. Imagine now that our application has grown in popularity. Each pizza now requires a summary and a more thorough description , which also needs additional fields of data. We need a method to dynamically modify the structure and layout of our queries.

GraphQL has a feature called directive that allows this. A directive can be appended to a field or fragment and can influence the query’s execution in any way the server chooses. The GraphQL standard contains two default directives that each GraphQL server implementation must support:

  • @include(if: Boolean): We include this field ...