# Query examples

AI agent getting started? Read [Agents: start here](https://docs.openeng.io/agents/start-here.md) first.

Read lists, graphs, paths and totals with the CLI.

Source: https://docs.openeng.io/developers/query-examples/

Find the records you need, follow their relationships, then choose a list, graph, table or total as the answer.

1. Complete the [example project walkthrough](https://docs.openeng.io/get-started/quickstart.md) and [CLI setup](https://docs.openeng.io/get-started/setup.md).
2. Replace `<project-id>` in each command with your seeded project's ID. These examples read `main`; a selector's `ref` names the branch.
3. Use returned `objectId` values where a later command asks for an object ID. An identifier such as `OCGT-REQ-001` is a property, not an object ID.

The set examples name `projects` explicitly. Without it, a set reads every project you can read on the selected branch; `selector.projectId` alone does not narrow it. See the [CLI reference](https://docs.openeng.io/reference/cli.md) for full command shapes.

## List matching objects

Use `queryObjects` (the API operation for listing matching objects) for a filtered, ordered, paged object list. Find the example's net-output requirement:

```sh
openeng objects-sdk queryObjects --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"kind":"objects","typeKey":"requirement","projects":["<project-id>"],"filters":[{"kind":"property","property":"identifier","op":"=","value":"OCGT-REQ-001"}],"limit":100,"continuation":null}'
```

Keep its returned `objectId` for the next step. To follow relationships within an object query, add `steps`, each with `edgeTypeKey`, `direction` and `targetTypeKey`. Structural steps use `to-children` or `to-parent`; traceability steps use `outgoing` or `incoming`.

## Read a connected graph

Use `queryGraph` (the API operation for reading connected objects and their relationships) when you need both. Replace `<requirement-object-id>` with the ID above. This walks incoming `verifies` edges:

```sh
openeng objects-sdk queryGraph --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"kind":"graph","start":{"kind":"objects","objects":[{"objectId":"<requirement-object-id>","projectId":"<project-id>"}]},"steps":[{"edgeTypeKeys":["verifies"],"direction":"incoming","depth":1}],"objectLimit":200,"hopLimit":400}'
```

Expect the requirement, its verification objects and their hops. Find `OCGT-VER-001` in the returned properties and keep its object ID. This one-object, one-level form supports `continuation`; send the returned token with the same query for the next page.

## Group connected values and calculate totals

Use `queryRows` (the API operation for selecting fields or grouping calculations) for selected cells or grouped measures. This follows the Fuel Gas Metering Skid system's parts, groups by Make or Buy and returns a count and total recorded mass per group:

```sh
openeng objects-sdk queryRows --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"kind":"graph-query","start":{"typeKey":"system","projects":["<project-id>"],"filters":[{"kind":"property","property":"identifier","op":"=","value":"FGS-MS"}]},"path":[{"alias":"part","edgeTypeKeys":["has-part"],"direction":"to-children"}],"answer":{"kind":"groups","keys":[{"kind":"property","alias":"part","property":"makeOrBuy"}],"measures":[{"kind":"count","ends":"visible"},{"kind":"sum","alias":"part","property":"mass","ends":"visible"}]}}'
```

Each group's `keys` contains the Make or Buy value; `values` contains the count then the mass sum. This sums Part records, not BOM usage quantities. For a table instead, use `answer: {"kind":"rows","columns":[{"kind":"property","alias":"part","property":"identifier"}],"limit":100,"continuation":null}` with the same start and path.

## Find shortest paths

Use `findPaths` (the API operation for finding shortest relationship paths) to find shortest paths between two known objects. Insert the requirement and verification IDs from the previous answers:

```sh
openeng objects-sdk findPaths --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"from":{"objectId":"<verification-object-id>","projectId":"<project-id>"},"to":{"objectId":"<requirement-object-id>","projectId":"<project-id>"},"edgeTypeKeys":["verifies"],"direction":"outgoing","maxLength":6,"maxPaths":10}'
```

Expect a one-edge path for `OCGT-VER-001` to `OCGT-REQ-001`. Shortest means fewest edges. Paths only pass through objects you can read. `none-within-bound` means no path was found within the requested maximum length (`maxLength`).

## Average a numeric property

`queryRows` has no `average` measure. Use `aggregateObjects` (the API operation for calculating values across matching objects) with `operation: "average"` for a numeric property's mean and count. The operation is deprecated in favour of `queryRows` for graph aggregates, but still supplies averages:

```sh
openeng objects-sdk aggregateObjects --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"kind":"aggregate","typeKey":"part","projects":["<project-id>"],"operation":"average","property":"mass"}'
```

The answer counts non-null mass values; when none exist, it omits `mean` and returns count zero.

## Combine two object queries on one branch

Use `queryObjects` with `kind: "set"` and two plain descriptions on the selected branch. `union` returns either set, `intersection` returns their shared objects, and `difference` returns left minus right:

```sh
openeng objects-sdk queryObjects --selector '{"projectId":"<project-id>","ref":"main"}' --input '{"kind":"set","op":"union","left":{"kind":"objects","typeKey":"part","projects":["<project-id>"],"filters":[{"kind":"property","property":"makeOrBuy","op":"=","value":"Make"}]},"right":{"kind":"objects","typeKey":"part","projects":["<project-id>"],"filters":[{"kind":"property","property":"mass","op":">","value":20}]},"limit":100,"continuation":null}'
```

Change only `op` to try `intersection` or `difference`. Operands may contain filters and steps; they cannot contain nested set operations, sorting or their own pagination.

## Weight a roll-up in your own model

This example is **not for the example project**. The `weightedSum` operation adds values multiplied by relationship quantities along each path; it needs a numeric property declared on every edge type of the final hop. The example project declares no edge properties; its BOM quantity is an object property.

1. In your own model, declare a Part type, `weighted-part`, with string `identifier` and numeric `mass` properties.
2. Declare a traceability relationship, `weighted-uses`, from Part to Part, with a numeric `quantity` property.
3. Give the root Part the identifier `ASSY-1`.

The following input multiplies each reached mass by the product of relationship quantities along its path and sums the contributions:

```sh
openeng objects-sdk queryRows --selector '{"projectId":"<your-project-id>","ref":"main"}' --input '{"kind":"graph-query","start":{"typeKey":"weighted-part","projects":["<your-project-id>"],"filters":[{"kind":"property","property":"identifier","op":"=","value":"ASSY-1"}]},"path":[{"alias":"component","edgeTypeKeys":["weighted-uses"],"direction":"outgoing","repeat":{"depth":3}}],"answer":{"kind":"groups","keys":[{"kind":"identity","alias":"start"}],"measures":[{"kind":"weightedSum","alias":"component","property":"mass","weight":"quantity","over":["weighted-part"],"multiplicity":"per-path","ends":"visible"}]}}'
```

A weighted sum needs a final repeat hop and 1–8 contributor types in `over`; the start does not contribute. Repeat depth is 1–3, with no filters or `targetTypeKeys` on that hop. Repeat answers are groups only. Check `deeper` for reach beyond the requested depth, `unvalued` for missing values and `unweighted` for missing weights. A cycle with a per-path weighted sum is refused with `cycle-in-reach`, meaning the traversed relationships contain a cycle.



## Check completeness

See [Query limits](https://docs.openeng.io/lookups/query-limits.md) for page sizes, traversal bounds and incomplete-answer states.
