3 Customizing and organizing GraphQL operations

 

This chapter covers

  • Using arguments to customize what a request field returns
  • Customizing response property names with aliases
  • Describing runtime executions with directives
  • Reducing duplicated text with fragments
  • Composing queries and separating data requirement responsibilities

In any nontrivial application, you have to do many things beyond asking the server a direct, simple, single-value question. Data fetching is usually coupled with variables and meta-questions about the structure of the response. You often need to modify the data returned by the server to make it suitable for your application. Sometimes you have to remove parts of the data or go back to the server and ask for other parts that are required based on conditions in your application. Sometimes you need a way to organize big requests and categorize them to know which part of your application is responsible for each part of your requests. Luckily, the GraphQL language offers many built-in features you can use to do all of this and much more. These customizing and organizing features are what this chapter is all about.

3.1 Customizing fields with arguments

3.1.1 Identifying a single record to return

3.1.2 Limiting the number of records returned by a list field

3.1.3 Ordering records returned by a list field

3.1.4 Paginating through a list of records

3.1.5 Searching and filtering

3.1.6 Providing input for mutations

3.2 Renaming fields with aliases

3.3 Customizing responses with directives

3.3.1 Variables and input values

3.3.2 The @include directive

3.3.3 The @skip directive

3.3.4 The @deprecated directive

3.4 GraphQL fragments

3.4.1 Why fragments?

3.4.2 Defining and using fragments

3.4.3 Fragments and DRY

3.4.4 Fragments and UI components

3.4.5 Inline fragments for interfaces and unions

Summary