liveBook · Manning

GraphQL

As explained in Digital Twins in Action

GraphQL is a query language and runtime for APIs that lets clients specify the fields and shape of the response they require.

GraphQL lets an API client name the fields and relationships it needs, rather than accept a fixed response for each resource. For a digital twin with connected spaces, sensors, and measurements, one query can retrieve selected data across those relationships. This reduces sequential client requests and over-fetching; it does not remove the need to design and maintain the API. section 5.6.2 section 4.4.3

Ask for second-floor temperatures

To find current temperatures, a REST client could first fetch second-floor rooms, then each room’s sensors, then each sensor’s latest measurement. A GraphQL client instead requests rooms filtered to floor 2 and selects each room’s name and sensor’s latest temperature in one query. OData can also traverse relationships in one request with $expand, but its expansion logic determines the response shape. section 5.6.2

query SecondFloorTemps {
  rooms(filter: {floor: "2"}) {
    name
    sensor {
      latestMeasurement {
        temperature
      }
    }
  }
}

The nested selection specifies the relationships to traverse and the fields to return: room name and latest sensor temperature. section 5.6.2

A GraphQL schema is the contract for available queries, types, and relationships. A home-twin schema can model spaces, sensors, documents, images, and timestamped measurements. Basing that schema on the RealEstateCore ontology gives those types a shared vocabulary and puts a temperature reading in the context of its sensor and space; the schema’s type system enforces that model at runtime. section 5.6.3

Listing 5.5 GraphQL query schema for a home twin
type Query {  #1
  spaces(space: String!, start_date: String!,
    end_date: String!): [Space!]!
}

type Space {  #2
  name: String!
  sensors: [Sensor!]!
  documents: [Document!]!
  images: [Image!]!
  measurements: [MeasurementGroup!]!
}

type Sensor {
  id: String!
  space: String!
}

type Document {
  id: String!
  url: String!
}

type Image {
  id: String!
  url: String!
}

type MeasurementGroup {
  name: String!
  unit: String
  values: [Measurement!]!
}

type Measurement {
  sensor_id: String!
  timestamp: String!
  value: Float!
}

The schema fragment defines a spaces query with required arguments and links each returned Space to its sensors and grouped measurements. section 5.6.3

Put a simpler interface over existing APIs

A small Python GraphQL server can expose a weather query by calling a geocoding REST API for city coordinates and then an Open-Meteo REST API for a seven-day forecast. A client requests the time, rain, and surfaceTemperature fields through that one GraphQL query. section 5.6.3

Downstream consumers still have format requirements. A Grafana Infinity data source can query the home-twin GraphQL endpoint for kitchen measurements, but its nested JSON must be flattened with jq into tabular rows for a time-series panel. Monitor external API schemas, rate limits, authentication, and versioning as dependencies: changes can break the data pipeline even when its GraphQL query remains unchanged. section 6.4.2 section 4.4.3

How Digital Twins in Action covers it

How deep it goes
Works through relationship queries, a typed home schema, a small server, and dashboard consumption; does not examine GraphQL execution internals.
Examples
Uses a second-floor temperature query, a weather API combining two REST calls, and kitchen-voltage visualization.
What it assumes
Assumes familiarity with APIs and related-data models while illustrating GraphQL queries and schemas through concrete examples.
What you can do after
Build it from scratch with Graphene and Flask, then apply it via Grafana’s Infinity data source for a home-twin dashboard.
How it connects
Connects knowledge graphs and ontologies to API schemas, REST and OData comparisons, and dashboard data transformation.

Also explained in

Compare how 6 books explain GraphQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.