{
  "id": "authz-7",
  "title": "KBAC 3.0: Raw-Cypher Policies with CALL { } Subqueries and USE Routing",
  "description": "Author 3.0-kbac policies using the Cypher keywords that only this version accepts: USE graph.byName() database routing and CALL { } subqueries with inner RETURNs. The condition runs as raw Cypher - the platform only pins the subject/resource and appends the projection; the USE routing shown here targets a composite IKG.",
  "category": "KBAC",
  "tags": [
    "KBAC Policy",
    "3.0-kbac",
    "Raw Cypher",
    "USE graph.byName",
    "CALL Subquery",
    "Composite Database",
    "authZEN"
  ],
  "api": "KBAC",
  "language": "Json",
  "lastUpdated": "2026-07-14",
  "openApiEndpoints": [
    "/configs/v1/authorization-policies",
    "/access/v1/evaluation"
  ],
  "relatedGuides": [
    "/guides/guide-data-residency",
    "/guides/guide-authzen",
    "/guides/guide-dynamic-authz"
  ],
  "commonErrors": [
    {
      "code": "422",
      "message": "USE clause is not allowed / Cypher contains forbidden clauses: [CALL]",
      "solution": "The policy was created with policy_version 2.0-kbac. USE and CALL { } require meta.policy_version \"3.0-kbac\""
    },
    {
      "code": "422",
      "message": "graph.byName() argument must be a string literal or a single parameter",
      "solution": "Do not compute the routing target (concatenation, coalesce(...), etc.). Use 'composite.alias' as a literal, or a single $parameter (see authz-8)"
    },
    {
      "code": "422",
      "message": "parameter \"$subject_id\" is reserved and cannot be referenced",
      "solution": "3.0-kbac identifies subjects by type and external ID only - remove the $subject_id reference; the subject is already pinned automatically"
    },
    {
      "code": "422",
      "message": "Cypher parse error near RETURN",
      "solution": "A top-level RETURN is still rejected on 3.0-kbac - the platform appends the projection. RETURN is only legal inside a CALL { } subquery"
    }
  ],
  "summary": "This example is 3.0-kbac ONLY - every policy shown here is rejected on 2.0-kbac.\n\nWhat 3.0-kbac changes:\nA 2.0-kbac condition is rewritten by the platform and always runs against the default database. A 3.0-kbac condition is RAW Cypher: it runs exactly as authored, and you write the composite-database routing yourself.\n\nNewly accepted Cypher keywords (rejected on 2.0-kbac, legal on 3.0-kbac):\n1. USE graph.byName(...) - routes the match to a constituent database of the composite IKG. The argument must be a string literal or a single parameter; expressions are rejected at creation.\n2. CALL { } subqueries - each subquery can carry its own USE clause, so one condition can combine matches from several constituents. The examples write them as CALL () { ... }, the explicit variable-scope form: the parentheses list the outer variables imported into the subquery (empty = import none).\n3. RETURN inside a CALL { } subquery - required to hand rows back to the enclosing query. A top-level RETURN is still rejected: the platform appends the projection itself.\n\nStill blocked in both versions: mutating clauses (CREATE, DELETE, DETACH, DROP, FOREACH, MERGE, REMOVE, SET).\n\nAuthoring rules specific to 3.0-kbac:\n- The condition must still bind the variables subject and resource.\n- $subject_id must not be referenced (composite node IDs are not stable across locations); rejected at creation.\n- External (resolver-backed) properties must not be referenced; rejected at creation (\"external properties cannot be used in data-residency policies\").\n- $subject_external_id, $subject_type, $resource_external_id, $resource_type are bound by the platform - never supplied via input_params.\n- The subject does not need is_identity: 3.0-kbac matches it by type + external ID (2.0-kbac requires an identity-node subject). With a bearer token, the token's subject must match the requested subject (403 Forbidden otherwise).\n- Schema is otherwise identical to 2.0-kbac: any valid 2.0 condition is also a valid 3.0 condition (unless it references $subject_id or external properties).\n\nNote: 3.0-kbac itself does not require a composite database - only USE routing does. The policies in this example route, so they need one; a 3.0-kbac policy without USE evaluates against the default database as plain raw Cypher.\n\nFor dynamic, per-request location routing with USE graph.byName($param), see authz-8.",
  "usecase": "Scenario: A car-sharing platform runs a composite IKG (see the data residency guide): the default/global constituent holds proxy nodes and cross-location relationships, while full node data lives in per-location constituents such as ikcomposite.db2.\n\nThe authorization team wants decisions evaluated against a location constituent - not just the global proxies - because that is where the full nodes live:\n\nPolicy 1 (static USE): the whole condition runs inside ikcomposite.db2. \"Can alice drive KITT?\" is answered from the east database's full data.\n\nPolicy 2 (CALL subquery): the same routing expressed as a CALL { } subquery with an inner RETURN - the shape to build on when a condition needs to combine matches from more than one constituent (each subquery can carry its own USE clause).\n\nBoth policies answer the same AuthZEN calls as any other KBAC policy: nothing changes for the calling application.",
  "requirements": "Prerequisites:\n- A customer-hosted composite-database project (composite_db_name + alias_mapping configured; see the data residency guide and resource residency-1)\n- Nodes ingested with locations, e.g. Person(person-alice) and Car(car-kitt) with an OWNS relationship in the east constituent (ikcomposite.db2)\n- ServiceAccount credentials: For creating policies (Bearer token)\n- AppAgent credentials: For authorization queries (X-IK-ClientKey)\n\nRequired API access:\n- POST /configs/v1/authorization-policies (create policy)\n- POST /access/v1/evaluation (authorization check)\n\nVersion requirement: meta.policy_version must be \"3.0-kbac\". The same Cypher fails on 2.0-kbac with \"USE clause is not allowed\" / \"Cypher contains forbidden clauses: [CALL]\".",
  "steps": "Step 1: Create a 3.0-kbac policy with a static USE clause\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST the policy; the condition starts with USE graph.byName('ikcomposite.db2')\n- Result: The Cypher is validated as raw Cypher, not rewritten; policy ID returned\n\nStep 2: Create a 3.0-kbac policy with a CALL { } subquery\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST the policy; the condition wraps the match in CALL () { USE ... MATCH ... RETURN subject, resource }\n- Result: CALL, per-subquery USE, and the inner RETURN are all accepted - each would be rejected on 2.0-kbac\n\nStep 3: Evaluate\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST to /access/v1/evaluation with subject, action, resource - the request shape is unchanged\n- Result: {decision: true} evaluated inside the routed constituent\n\nStep 4: Cleanup\n- Action: DELETE the policy configurations",
  "graph": {
    "caption": "The graph the example runs on: 9 nodes (4 Person, 2 Car, 1 Bus, 1 Ticket, 1 Laptop) and 8 relationships (DRIVES, HAS, FOR, OWNS). Hover a node for its properties, click it to highlight its neighbourhood.",
    "nodes": [
      {
        "external_id": "alice",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "alice@email.com"
          },
          {
            "type": "given_name",
            "value": "Alice"
          },
          {
            "type": "last_name",
            "value": "Smith"
          }
        ]
      },
      {
        "external_id": "knightrider",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "knightrider@demo.com"
          },
          {
            "type": "name",
            "value": "Michael Knight"
          }
        ]
      },
      {
        "external_id": "satchmo",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "satchmo@demo.com"
          },
          {
            "type": "name",
            "value": "Louis Armstrong"
          }
        ]
      },
      {
        "external_id": "karel",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "karel@demo.com"
          },
          {
            "type": "name",
            "value": "Karel Plihal"
          }
        ]
      },
      {
        "external_id": "kitt",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "pontiac"
          },
          {
            "type": "model",
            "value": "Firebird"
          }
        ]
      },
      {
        "external_id": "cadillacv16",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Cadillac"
          },
          {
            "type": "model",
            "value": "V-16"
          }
        ]
      },
      {
        "external_id": "harmonika",
        "type": "Bus",
        "is_identity": false,
        "properties": [
          {
            "type": "manufacturer",
            "value": "Ikarus"
          },
          {
            "type": "model",
            "value": "280"
          }
        ]
      },
      {
        "external_id": "listek",
        "type": "Ticket",
        "is_identity": false,
        "properties": []
      },
      {
        "external_id": "airbook-xyz",
        "type": "Laptop",
        "is_identity": false,
        "properties": []
      }
    ],
    "relationships": [
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "satchmo",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      },
      {
        "source": "karel",
        "target": "listek",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "listek",
        "target": "harmonika",
        "type": "FOR",
        "properties": []
      },
      {
        "source": "karel",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "airbook-xyz",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "knightrider",
        "target": "kitt",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "cadillacv16",
        "type": "DRIVES",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "3.0-kbac policy with a static USE clause. The condition is raw Cypher: the platform keeps it verbatim, pins subject/resource by type + external_id, and replaces the projection. On 2.0-kbac this exact Cypher fails with 'USE clause is not allowed'.",
      "filename": "policy_static_use.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "3.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_DRIVE"
        ],
        "resource": {
          "type": "Car"
        },
        "condition": {
          "cypher": "USE graph.byName('ikcomposite.db2') MATCH (subject:Person)-[:OWNS]->(resource:Car)"
        }
      }
    },
    {
      "description": "Request to create the static-USE policy using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "3.0-kbac raw-Cypher policy. The condition is executed as authored: the USE graph.byName() clause pins the whole match to the ikcomposite.db2 constituent database. Rejected on 2.0-kbac (USE clause is not allowed).",
        "display_name": "policy - can drive (static USE routing)",
        "name": "policy-can-drive-static-use",
        "policy": "{\"meta\":{\"policy_version\":\"3.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_DRIVE\"],\"resource\":{\"type\":\"Car\"},\"condition\":{\"cypher\":\"USE graph.byName('ikcomposite.db2') MATCH (subject:Person)-[:OWNS]->(resource:Car)\"}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 2",
      "description": "3.0-kbac policy using a CALL { } subquery with its own USE clause and an inner RETURN - all three newly accepted keywords in one condition. On 2.0-kbac the CALL keyword alone fails with 'Cypher contains forbidden clauses: [CALL]'. Note the subquery RETURNs subject and resource so the platform can pin them.",
      "filename": "policy_call_subquery.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "3.0-kbac"
        },
        "subject": {
          "type": "Person"
        },
        "actions": [
          "CAN_DRIVE"
        ],
        "resource": {
          "type": "Car"
        },
        "condition": {
          "cypher": "CALL () { USE graph.byName('ikcomposite.db2') MATCH (subject:Person)-[:OWNS]->(resource:Car) RETURN subject, resource }"
        }
      }
    },
    {
      "description": "Request to create the CALL-subquery policy using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "3.0-kbac policy demonstrating the newly accepted keywords: a CALL { } subquery with a per-subquery USE graph.byName() clause and an inner RETURN. The example is written CALL () { ... } - the explicit variable-scope form, where the parentheses list the outer variables imported into the subquery (empty = none). Each of the three (CALL, USE, inner RETURN) is rejected on 2.0-kbac; on 3.0-kbac they are legal composite-database routing. Mutating clauses (CREATE, MERGE, SET, DELETE, ...) and a top-level RETURN remain blocked.",
        "display_name": "policy - can drive (CALL subquery routing)",
        "name": "policy-can-drive-call-subquery",
        "policy": "{\"meta\":{\"policy_version\":\"3.0-kbac\"},\"subject\":{\"type\":\"Person\"},\"actions\":[\"CAN_DRIVE\"],\"resource\":{\"type\":\"Car\"},\"condition\":{\"cypher\":\"CALL () { USE graph.byName('ikcomposite.db2') MATCH (subject:Person)-[:OWNS]->(resource:Car) RETURN subject, resource }\"}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 3",
      "description": "AuthZEN evaluation - the request shape is identical to any other KBAC evaluation; the routing lives entirely in the policy.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "ingest": {
        "subject": {
          "type": "Person",
          "id": "person-alice"
        },
        "resource": {
          "type": "Car",
          "id": "car-kitt"
        },
        "action": {
          "name": "CAN_DRIVE"
        }
      }
    },
    {
      "description": "Response - the decision was evaluated inside the ikcomposite.db2 constituent.",
      "filename": "Response 200",
      "language": "Json",
      "ingest": {
        "decision": true
      }
    },
    {
      "step": "Step 4",
      "description": "Delete the policies when done.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/authorization-policies/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    }
  ],
  "links": {
    "html": "/resources/authz-7",
    "markdown": "/resources/authz-7.md",
    "openApi": "https://openapi.indykite.com/"
  }
}