apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: graphql-query
  labels:
    dagweave.io/catalog-item: graphql-query
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.2"
    dagweave.io/catalog-category: "HTTP"
spec:
  entrypoint: query
  templates:
    - name: query
      inputs:
        parameters:
          # Absolute GraphQL endpoint URL.
          - name: endpoint
          # The GraphQL query (or mutation) document.
          - name: query
          # JSON object of query variables. Defaults to an empty object.
          - name: variables-json
            default: "{}"
          # Name/key of a Kubernetes Secret holding the bearer token.
          - name: token-secret-name
            default: graphql-token
          - name: token-secret-key
            default: token
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # badouralix/curl-jq:ubuntu — provides sh, curl and jq.
        image: badouralix/curl-jq@sha256:38362e9b8ef4bd09a88c0db07941e61e0bf0f7240e9f36a3ff2c31ad75546be2
        command: [sh, -c]
        # Values are passed as env vars (below), never interpolated into the shell.
        # jq assembles the {query, variables} request body with --arg / --argjson so
        # a query containing quotes or newlines is escaped correctly.
        args:
          - |
            set -eu
            BODY=$(jq -cn --arg q "${QUERY}" --argjson v "${VARIABLES}" \
              '{query: $q, variables: $v}')
            curl -sSf -X POST "${ENDPOINT}" \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer ${TOKEN}" \
              --data-raw "${BODY}"
        env:
          - name: ENDPOINT
            value: "{{inputs.parameters.endpoint}}"
          - name: QUERY
            value: "{{inputs.parameters.query}}"
          - name: VARIABLES
            value: "{{inputs.parameters.variables-json}}"
          - name: TOKEN
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.token-secret-name}}"
                key: "{{inputs.parameters.token-secret-key}}"
