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





