hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.19k stars 2.77k forks source link

Relationship predicates not working on Commands but working for Models #10606

Open TristenHarr opened 6 days ago

TristenHarr commented 6 days ago

When using a predicate filter that has a relationship, the engine metadata fails to build.

For example, this works properly for models:

---
kind: Model
version: v1
definition:
  name: ProgramStaffs
  objectType: ProgramStaffs
  source:
    dataConnectorName: blendx_pg
    collection: program_staffs
  filterExpressionType: ProgramStaffsBoolExp
  orderableFields:
    - fieldName: id
      orderByDirections:
        enableAll: true
    - fieldName: programId
      orderByDirections:
        enableAll: true
    - fieldName: role
      orderByDirections:
        enableAll: true
    - fieldName: userId
      orderByDirections:
        enableAll: true
  graphql:
    selectMany:
      queryRootField: programStaffs
    selectUniques:
      - queryRootField: programStaffsById
        uniqueIdentifier:
          - id
      - queryRootField: programStaffsByProgramStaffsUserIdProgramIdRoleKey
        uniqueIdentifier:
          - programId
          - role
          - userId
    orderByExpressionType: ProgramStaffsOrderBy
    filterInputTypeName: ProgramStaffsFilterInput
    aggregate:
      queryRootField: programStaffsAggregate
  description: This table keeps track of the staffs of programs
  aggregateExpression: ProgramStaffsAggExp

---
kind: ModelPermissions
version: v1
definition:
  modelName: ProgramStaffs
  permissions:
    - role: admin
      select:
        filter: null
    - role: staff
      select:
        filter:
          and:
            - fieldComparison:
                field: userId
                operator: _eq
                value:
                  sessionVariable: x-hasura-user-id
            - relationship:
                name: program
                predicate:
                  fieldComparison:
                    field: organisationId
                    operator: _eq
                    value:
                      sessionVariable: x-hasura-org-id

However, if you need to do something similar to commands it does not work:

---
kind: Command
version: v1
definition:
  name: V2DeleteProgramStaffsById
  outputType: V2DeleteProgramStaffsByIdResponse!
  arguments:
    - name: keyId
      type: Uuid!
    - name: preCheck
      type: ProgramStaffsBoolExp!
      description: Delete permission predicate over the 'program_staffs' collection
  source:
    dataConnectorName: blendx_pg
    dataConnectorCommand:
      procedure: v2_delete_program_staffs_by_id
    argumentMapping:
      keyId: key_id
      preCheck: pre_check
  graphql:
    rootFieldName: v2DeleteProgramStaffsById
    rootFieldKind: Mutation
  description: Delete any row on the 'program_staffs' collection using the 'id' key

---
kind: CommandPermissions
version: v1
definition:
  commandName: V2DeleteProgramStaffsById
  permissions:
    - role: admin
      allowExecution: true
    - role: super_admin
      allowExecution: true
      argumentPresets:
        - argument: preCheck
          value: 
            booleanExpression: 
              and:
                - fieldComparison:
                    field: userId
                    operator: _eq
                    value: 
                      sessionVariable: x-hasura-user-id
    - role: staff
      allowExecution: true
      argumentPresets:
        - argument: preCheck
          value: 
            booleanExpression: 
              and:
                - fieldComparison:
                    field: userId
                    operator: _eq
                    value: 
                      sessionVariable: x-hasura-user-id
                - relationship:
                    name: program
                    predicate: 
                      fieldComparison:
                        field: organisationId
                        operator: _eq
                        value: 
                          sessionVariable: x-hasura-org-id

Specifically, the relationship check in the boolean expression causes the metadata to fail to build. ddn context set-current-context dev 6:23PM INF Current context set to "dev". 6:23PM INF Checking for update... HINT A new version (v2.14.0) is available for the DDN CLI, to update run: ddn update-cli ddn supergraph build local --output-dir engine/build/dev 6:23PM INF Using Supergraph config file "supergraph.dev.yaml" found in context. 6:23PM INF Using localEnvFile ".env.dev" found in context. 6:23PM ERR Code=opendds-validation Message="invalid metadata: error building schema: invalid metadata: Internal Error: Unknown type ProgramStaffsBoolExp (in subgraph app) when collecting type mappings in type ProgramStaffsBoolExp (in subgraph app)" 6:23PM ERR Building Supergraph failed. make: *** [Makefile:21: ddn-engine-local] Error 1