{
  "id": "ciq-27",
  "title": "ContX IQ v2: Read a Whole Relationship with Its Endpoints",
  "description": "A CIQ v2 (2.0-ciq) read that returns a whole relationship: it comes back as its two endpoints, each with its external_id and type, and its properties. The example captures a three-node graph with a property on OWNS, saves the 2.0-ciq policy and the query, and executes as the owner.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ",
    "CIQ v2",
    "2.0-ciq",
    "CIQ Policy",
    "Knowledge Query",
    "Relationship"
  ],
  "api": "ContX IQ",
  "language": "Json",
  "lastUpdated": "2026-11-11",
  "openApiEndpoints": [
    "/capture/v1/nodes",
    "/capture/v1/relationships",
    "/configs/v1/authorization-policies",
    "/configs/v1/knowledge-queries",
    "/contx-iq/v1/execute"
  ],
  "relatedGuides": [
    "/guides/guide-contx-iq",
    "/guides/guide-ciq-cypher"
  ],
  "commonErrors": [
    {
      "code": "400",
      "message": "requested variable owns is not in the allowed_reads.relationships list",
      "solution": "The query asks for a relationship the policy does not let it read. List the relationship variable in allowed_reads.relationships (owns for the whole relationship, owns.* or owns.<prop> for its properties)."
    },
    {
      "code": "200",
      "message": "{\"data\": []}",
      "solution": "A 2.0-ciq filter attribute without the @ prefix (\"subject.external_id\") is a literal string and matches nothing. Write \"@subject.external_id\". Also check that the token's sub is the external_id of a Person who OWNS a Bus, with GET /contx-iq/v1/whoami."
    }
  ],
  "summary": "When a Knowledge Query on a 2.0-ciq policy asks for a whole relationship (its variable alone, such as \"owns\"), the relationship comes back by its endpoints:\n\n- _startNode_external_id and _startNode_type: the node the relationship starts from.\n- _endNode_external_id and _endNode_type: the node it points to.\n- Props: the relationship's id (a GID), its timestamps and its own properties.\n\nSo a client reads who is linked to what straight from the answer, in the same identifiers it uses everywhere else (external_id and type), without asking for the endpoint nodes separately. A variable-length relationship comes back as a list of these objects, one per hop.\n\nOther ways to read a relationship:\n- \"owns.since\" returns the one property, and \"owns.*\" all of them.\n- A filter attribute on a 2.0-ciq policy carries the @ prefix, as in \"@subject.external_id\".",
  "usecase": "Scenario: a fleet dashboard lists, for the signed-in person, the buses they own and since when. It needs the ownership itself, not only the buses: who owns, what is owned, and the since property of OWNS.\n\nGraph:\n- Person(karel), an identity node\n- Bus(harmonika)\n- LicenseNumber(ln-yyy)\n- karel -[OWNS {since: 2024-05-01}]-> harmonika -[HAS]-> ln-yyy\n\nWalk:\n1. Capture the three nodes, and the relationships with since on OWNS.\n2. Save the 2.0-ciq policy: a Person reads the buses they own and the OWNS relationship, bound to the caller through $token.sub.\n3. Save the Knowledge Query: bus.external_id and the whole owns relationship.\n4. Execute as karel: one record, with owns as karel (Person) to harmonika (Bus) and its properties.",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating the policy and the Knowledge Query.\n- AppAgent credentials: For the Capture calls and the execute (X-IK-ClientKey). The agent must hold Capture and ContXIQ.\n- A user access token whose sub is \"karel\", validated by a Token Introspect configuration of the project (the policy binds the subject through $token.sub).\n\nRequired API access:\n- POST /capture/v1/nodes, POST /capture/v1/relationships\n- POST /configs/v1/authorization-policies\n- POST /configs/v1/knowledge-queries\n- POST /contx-iq/v1/execute",
  "steps": "Step 1: Capture the graph\n- Authentication: AppAgent credential.\n- Action: POST the three nodes, then the OWNS relationship with its since property and the HAS relationship.\n\nStep 2: Create the 2.0-ciq policy\n- Authentication: ServiceAccount credential.\n- Action: POST the policy. Its condition matches the buses the caller owns; the filter binds @subject.external_id to $token.sub; allowed_reads covers bus.external_id and the owns relationship.\n- Result: the policy ID.\n\nStep 3: Create the Knowledge Query\n- Action: POST the query with policy_id set to the policy from step 2: nodes bus.external_id, relationships owns.\n- Result: the query ID.\n\nStep 4: Execute as the owner\n- Authentication: AppAgent credential plus Authorization: Bearer with karel's token.\n- Action: POST /contx-iq/v1/execute with the query name and empty input_params.\n- Result: 200 with one record: bus.external_id \"harmonika\", and owns as {_startNode_external_id \"karel\", _startNode_type \"Person\", _endNode_external_id \"harmonika\", _endNode_type \"Bus\", Props with id, timestamps and since}.",
  "graph": {
    "caption": "The graph the example reads: karel OWNS the bus harmonika since 2024-05-01, and harmonika HAS the licence number ln-yyy. Hover the OWNS relationship for its property.",
    "nodes": [
      {
        "external_id": "karel",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "karel@demo.com"
          },
          {
            "type": "name",
            "value": "Karel Plihal"
          }
        ]
      },
      {
        "external_id": "harmonika",
        "type": "Bus",
        "properties": [
          {
            "type": "manufacturer",
            "value": "Ikarus"
          },
          {
            "type": "model",
            "value": "280"
          }
        ]
      },
      {
        "external_id": "ln-yyy",
        "type": "LicenseNumber",
        "properties": [
          {
            "type": "value",
            "value": "ln-yyy-value"
          }
        ]
      }
    ],
    "relationships": [
      {
        "source": "karel",
        "target": "harmonika",
        "type": "OWNS",
        "properties": [
          {
            "type": "since",
            "value": "2024-05-01"
          }
        ]
      },
      {
        "source": "harmonika",
        "target": "ln-yyy",
        "type": "HAS"
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "Capture the owner, the bus and its licence number.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/nodes",
      "language": "Json",
      "ingest": {
        "nodes": [
          {
            "external_id": "karel",
            "type": "Person",
            "is_identity": true,
            "properties": [
              {
                "type": "email",
                "value": "karel@demo.com"
              },
              {
                "type": "name",
                "value": "Karel Plihal"
              }
            ]
          },
          {
            "external_id": "harmonika",
            "type": "Bus",
            "properties": [
              {
                "type": "manufacturer",
                "value": "Ikarus"
              },
              {
                "type": "model",
                "value": "280"
              }
            ]
          },
          {
            "external_id": "ln-yyy",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "value",
                "value": "ln-yyy-value"
              }
            ]
          }
        ]
      }
    },
    {
      "description": "karel OWNS harmonika since 2024-05-01; harmonika HAS ln-yyy.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/relationships",
      "language": "Json",
      "ingest": {
        "relationships": [
          {
            "source": {
              "external_id": "karel",
              "type": "Person"
            },
            "target": {
              "external_id": "harmonika",
              "type": "Bus"
            },
            "type": "OWNS",
            "properties": [
              {
                "type": "since",
                "value": "2024-05-01"
              }
            ]
          },
          {
            "source": {
              "external_id": "harmonika",
              "type": "Bus"
            },
            "target": {
              "external_id": "ln-yyy",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          }
        ]
      }
    },
    {
      "step": "Step 2",
      "description": "The 2.0-ciq policy. The filter attribute carries the @ prefix that CIQ v2 requires; owns is listed whole in allowed_reads.relationships.",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "2.0-ciq"
        },
        "subject": {
          "type": "Person"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[owns:OWNS]->(bus:Bus)",
          "filter": [
            {
              "attribute": "@subject.external_id",
              "operator": "=",
              "value": "$token.sub"
            }
          ]
        },
        "allowed_reads": {
          "nodes": [
            "bus.external_id"
          ],
          "relationships": [
            "owns"
          ]
        }
      }
    },
    {
      "description": "Request to create the policy; the document is sent as a string in the policy field.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "name": "ciq-v2-policy-own-buses-ownership",
        "display_name": "CIQ v2 policy - own buses with the ownership relationship",
        "description": "Read the buses the caller owns and the OWNS relationship that links the caller to each.",
        "policy": "{\"meta\":{\"policy_version\":\"2.0-ciq\"},\"subject\":{\"type\":\"Person\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[owns:OWNS]->(bus:Bus)\",\"filter\":[{\"attribute\":\"@subject.external_id\",\"operator\":\"=\",\"value\":\"$token.sub\"}]},\"allowed_reads\":{\"nodes\":[\"bus.external_id\"],\"relationships\":[\"owns\"]}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 3",
      "description": "The Knowledge Query: the bus's external_id and the whole owns relationship.",
      "filename": "knowledge_query.json",
      "language": "Json",
      "ingest": {
        "nodes": [
          "bus.external_id"
        ],
        "relationships": [
          "owns"
        ]
      }
    },
    {
      "description": "Request to create the Knowledge Query.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/knowledge-queries",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "name": "kq-own-buses-with-ownership",
        "display_name": "Knowledge Query - own buses with the ownership relationship",
        "description": "Returns the buses the caller owns, each with the whole OWNS relationship: its two endpoints and its properties.",
        "policy_id": "your_policy_gid",
        "query": "{\"nodes\":[\"bus.external_id\"],\"relationships\":[\"owns\"]}",
        "status": "ACTIVE"
      }
    },
    {
      "step": "Step 4",
      "description": "Execute as karel: X-IK-ClientKey plus Authorization: Bearer <karel's token>. No input parameters; the subject comes from the token.",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "kq-own-buses-with-ownership",
        "input_params": {}
      }
    },
    {
      "description": "One record: owns named by its endpoints, karel (Person) to harmonika (Bus), with its id as a GID, its timestamps and since in Props.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "nodes": {
              "bus.external_id": "harmonika"
            },
            "relationships": {
              "owns": {
                "_startNode_external_id": "karel",
                "_startNode_type": "Person",
                "_endNode_external_id": "harmonika",
                "_endNode_type": "Bus",
                "Props": {
                  "id": "gid:AAAAF...",
                  "create_time": "2026-11-11T09:12:04.731Z",
                  "update_time": "2026-11-11T09:12:04.731Z",
                  "since": "2024-05-01"
                }
              }
            }
          }
        ]
      }
    }
  ],
  "links": {
    "html": "/resources/ciq-27",
    "markdown": "/resources/ciq-27.md",
    "openApi": "https://openapi.indykite.com/"
  }
}