{
  "id": "ciq-22",
  "title": "ContX IQ: Is this resource available? OPTIONAL MATCH and CASE expressions",
  "description": "Demonstrates OPTIONAL MATCH for pattern matching that doesn't fail when no match exists, combined with CASE expressions for conditional logic. Example: Check if a subdomain is available for registration.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ Policy",
    "ContX IQ Query",
    "ContX IQ Execution",
    "OPTIONAL MATCH",
    "CASE",
    "Availability Check",
    "_Application Subject"
  ],
  "api": "ContX IQ",
  "language": "Json",
  "lastUpdated": "2026-03-20",
  "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-sandbox"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "Check credentials: ServiceAccount Bearer token for config APIs, X-IK-ClientKey for execution"
    },
    {
      "code": "404",
      "message": "NOT_FOUND",
      "solution": "Verify the query/policy ID exists and belongs to your project"
    }
  ],
  "summary": "This example demonstrates OPTIONAL MATCH with CASE expressions:\n\nWhat is OPTIONAL MATCH?\nUnlike regular MATCH, OPTIONAL MATCH continues query execution even when no match is found (returns null instead of failing).\n\nWhat is CASE expression?\nConditional logic in Cypher that returns different values based on conditions.\n\nCombined pattern (policy cypher):\nMATCH (subject:_Application) WHERE subject.external_id = $_appId\nWITH subject\nOPTIONAL MATCH (tenant:Tenant)-[:HAS]->(subdomain:Property) WHERE subdomain.type = 'subdomain' AND subdomain.value = $subdomain\nWITH subject, CASE tenant WHEN tenant THEN false ELSE true END AS is_subdomain_available\n\nUse case: Check if a subdomain is available (not already registered).",
  "usecase": "Scenario: Before registering a new tenant subdomain, check if it's already taken.\n\nGraph structure (captured properties are Property nodes linked with HAS):\nTenant(tenant1) -[HAS]-> Property(type: \"subdomain\", value: \"whocares\")\nTenant(tenant2) -[HAS]-> Property(type: \"subdomain\", value: \"whatabout\")\nTenant(tenant3) has no subdomain\n\nQuery input (recorded execution): $subdomain = \"whocares\"\n\nQuery flow:\n1. _Application authenticates (using $_appId auto-filter)\n2. OPTIONAL MATCH searches for a Tenant with a subdomain Property whose value is $subdomain\n3. Tenant(tenant1) matched\n4. CASE evaluates: tenant is not null -> false\n5. Result: {is_subdomain_available: false}\n\nIf the subdomain were free:\n- Input: $subdomain = \"newsite\"\n- No Tenant matched -> tenant = null\n- CASE evaluates: null -> true\n- Result: {is_subdomain_available: true}\n\nKey concept: _Application as subject:\n- Uses $_appId filter (auto-populated with Application's external_id)\n- No user token required - service-to-service pattern",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating policies and queries (Bearer token)\n- AppAgent credentials: For data ingestion and query execution (X-IK-ClientKey)\n\n_Application as subject:\n- $_appId is automatically populated when using Application credentials\n- No user authentication needed for this pattern\n\nRequired API access:\n- POST /capture/v1/nodes/ (capture Tenant and Property nodes)\n- POST /configs/v1/authorization-policies (create policy)\n- POST /configs/v1/knowledge-queries (create query)\n- POST /contx-iq/v1/execute (run availability check)",
  "steps": "Step 1: Ingest Tenant Nodes\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST Tenant nodes tenant1 (subdomain whocares), tenant2 (subdomain whatabout) and tenant3 (no subdomain); each captured property becomes a Property node linked with HAS\n- Result: Graph with existing subdomains registered\n\nStep 2: Create Availability Check Policy\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST policy with subject type _Application whose cypher:\n  - filters the subject with WHERE subject.external_id = $_appId\n  - uses OPTIONAL MATCH to find a Tenant with a subdomain Property equal to $subdomain (may return null)\n  - uses a CASE expression to turn match/no-match into the boolean is_subdomain_available\n- Allowed reads: subject.*, tenant.*, aggregate_values is_subdomain_available\n- Result: Policy ID returned\n\nStep 3: Create Availability Check Query\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST query with aggregate_values [\"is_subdomain_available\"]; the parameter $subdomain is consumed by the policy cypher\n- Result: Query ID returned\n\nStep 4: Execute Availability Check\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST to /contx-iq/v1/execute with input_params subdomain = whocares\n- Result: {is_subdomain_available: false} (whocares is taken by tenant1)",
  "graph": {
    "caption": "The graph the example runs on: 3 nodes (3 Tenant), no relationships. Hover a node for its properties.",
    "nodes": [
      {
        "external_id": "tenant1",
        "type": "Tenant",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Tenant1"
          },
          {
            "type": "subdomain",
            "value": "whocares"
          }
        ]
      },
      {
        "external_id": "tenant2",
        "type": "Tenant",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Tenant2"
          },
          {
            "type": "subdomain",
            "value": "whatabout"
          }
        ]
      },
      {
        "external_id": "tenant3",
        "type": "Tenant",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Tenant3"
          }
        ]
      }
    ],
    "relationships": []
  },
  "code": [
    {
      "step": "Step 1",
      "description": "Capture the nodes needed for this use case.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/nodes/",
      "language": "Json",
      "ingest": {
        "nodes": [
          {
            "external_id": "tenant1",
            "is_identity": false,
            "type": "Tenant",
            "properties": [
              {
                "type": "name",
                "value": "Tenant1"
              },
              {
                "type": "subdomain",
                "value": "whocares"
              }
            ]
          },
          {
            "external_id": "tenant2",
            "is_identity": false,
            "type": "Tenant",
            "properties": [
              {
                "type": "name",
                "value": "Tenant2"
              },
              {
                "type": "subdomain",
                "value": "whatabout"
              }
            ]
          },
          {
            "external_id": "tenant3",
            "is_identity": false,
            "type": "Tenant",
            "properties": [
              {
                "type": "name",
                "value": "Tenant3"
              }
            ]
          }
        ]
      }
    },
    {
      "step": "Step 2",
      "description": "Create a CIQ Policy which designates the Subject node, the cypher, and the nodes allowed to be agregated. ",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "1.0-ciq"
        },
        "subject": {
          "type": "_Application"
        },
        "condition": {
          "cypher": "MATCH (subject:_Application) WHERE subject.external_id = $_appId WITH subject OPTIONAL MATCH (tenant:Tenant)-[:HAS]->(subdomain:Property) WHERE subdomain.type = 'subdomain' AND subdomain.value = $subdomain WITH subject, CASE tenant WHEN tenant THEN false ELSE true END AS is_subdomain_available",
          "filter": []
        },
        "allowed_reads": {
          "nodes": [
            "subject.*",
            "tenant.*"
          ],
          "aggregate_values": [
            "is_subdomain_available"
          ]
        }
      }
    },
    {
      "description": "Request to create the CIQ Policy configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of policy",
        "display_name": "policy name",
        "name": "policy-name",
        "policy": "{\"meta\":{\"policy_version\":\"1.0-ciq\"},\"subject\":{\"type\":\"_Application\"},\"condition\":{\"cypher\":\"MATCH (subject:_Application) WHERE subject.external_id = $_appId WITH subject OPTIONAL MATCH (tenant:Tenant)-[:HAS]->(subdomain:Property) WHERE subdomain.type = 'subdomain' AND subdomain.value = $subdomain WITH subject, CASE tenant WHEN tenant THEN false ELSE true END AS is_subdomain_available\",\"filter\":[]},\"allowed_reads\":{\"nodes\":[\"subject.*\",\"tenant.*\"],\"aggregate_values\":[\"is_subdomain_available\"]}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "description": "Request to read the CIQ Policy configuration using REST.",
      "filename": "policy_request.json",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    },
    {
      "step": "Step 3",
      "description": "Create a CIQ Query in the context of the policy to retrieve the designated properties. ",
      "filename": "knowledge_query.json",
      "language": "Json",
      "ingest": {
        "aggregate_values": [
          "is_subdomain_available"
        ]
      }
    },
    {
      "description": "Request to create a CIQ Query configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/knowledge-queries",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of knowledge query",
        "display_name": "knowledge query name",
        "name": "knowledge-query-name",
        "policy_id": "your_policy_gid",
        "query": "{\"aggregate_values\":[\"is_subdomain_available\"]}",
        "status": "ACTIVE"
      }
    },
    {
      "description": "Read the CIQ Query Configuration.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/knowledge-queries/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_knowledge_query_configuration_gid"
      }
    },
    {
      "step": "Step 4",
      "description": "Run a CIQ Execution to get the designated information. ",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "knowledge_query_gid",
        "input_params": {
          "subdomain": "whocares"
        },
        "page_token": 1
      }
    },
    {
      "description": "CIQ Execution response.",
      "filename": "response.json",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "aggregate_values": {
              "is_subdomain_available": false
            }
          }
        ]
      }
    }
  ],
  "links": {
    "html": "/resources/ciq-22",
    "markdown": "/resources/ciq-22.md",
    "openApi": "https://openapi.indykite.com/"
  }
}