← Writing

GraphQL basics — Fetch only the fields an order screen needs

On this page

A mobile order card needs only the order number and status. The order detail screen also needs the products, quantities, and shipment status. With GraphQL, both screens use the same order field and select the response fields they need. The server defines available fields and types in a schema and uses resolvers to fetch field values. GraphQL schemas · GraphQL queries

Define the API contract in the schema

In this schema, the order ID is required, and order can return null. Even when the order exists, shipment can be null if it has yet to ship. With items: [OrderItem!]!, both the list and each item in it must be non-null. An empty list is valid. GraphQL type system specification

type Query {
  order(id: ID!): Order
}

type Order {
  id: ID!
  status: OrderStatus!
  items: [OrderItem!]!
  shipment: Shipment
}

enum OrderStatus {
  PAID
  SHIPPED
  CANCELLED
}

type OrderItem {
  sku: String!
  quantity: Int!
}

type Shipment {
  status: String!
}

The example defines only a read-only Query. A state change such as cancelling an order can go in Mutation, and a stream of shipment status updates can go in Subscription. GraphQL mutations · GraphQL subscriptions

Select the fields the screen needs

The detail screen sends the query below with {"id":"9001"} as its variables. A small card in the order list can use the same order(id: $id) field and select just id and status. GraphQL queries and variables

query OrderDetail($id: ID!) {
  order(id: $id) {
    id
    status
    items {
      sku
      quantity
    }
    shipment {
      status
    }
  }
}

The detail screen sends OrderDetail with order ID 9001. The server resolves Query.order from the schema and returns the selected fields, including PAID status and a null shipment.

The diagram follows the detail screen’s request through to the response for an order that has yet to ship.

Here is an example response for a paid order that has yet to ship. The response includes the selected shipment field as null because there is no shipment value. When the request omits shipment, the response omits it too.

{
  "data": {
    "order": {
      "id": "9001",
      "status": "PAID",
      "items": [
        {"sku": "TOTE-NVY", "quantity": 1}
      ],
      "shipment": null
    }
  }
}

The client should check both data and errors. If the shipping service call fails, for example, the server can return the order information, set shipment to null, and describe the failure in errors. This lets the screen distinguish an order that has yet to ship from a failed shipment lookup. An error in a field marked ! can propagate up to an enclosing nullable field. GraphQL execution error handling

Map fields in Spring Boot

In Spring Boot, put the schema file under src/main/resources/graphql/ and use Spring for GraphQL annotations to map Java methods to fields. Spring Boot GraphQL configuration · Spring for GraphQL controllers

Schema field How Spring resolves it
Query.order A method annotated with @QueryMapping accepts the order ID and returns an order the current user is authorized to view.
Order.id, Order.status Spring reads the corresponding properties of the returned order object.
Order.shipment If shipment data lives in another service, use @SchemaMapping to look it up when this field is requested.
Order.shipment in an order list Use @BatchMapping if you can fetch shipment status for several orders at once.

Check the number of response fields and database queries separately. Omitting items does not reduce database work if the order lookup already reads the items. If a list requests shipment status and each order triggers its own lookup, the result can be an N+1 pattern, with one extra lookup per order after the initial list query. After batching, measure whether the number of SQL statements and external calls actually drops. Spring batch mapping · GraphQL performance

When GraphQL is worth considering

Consider GraphQL if the mobile app, website, and admin screens regularly need different combinations of the same order data. With few screens and a stable response shape, a REST endpoint such as GET /orders/{id} can also keep the contract simple.

Start by comparing order detail and list requests. Check that users cannot view another customer’s orders, measure how many database and external API calls a new field adds, and confirm that the screen still shows order information if only the shipment lookup fails. Cap the page size on lists, and consider depth or cost limits for APIs with deeply nested queries. GraphQL authorization · GraphQL pagination · GraphQL security

The order schema and response illustrate the behavior. They were not run against a Spring Boot server or a database.

Discussion

View on GitHub ↗