Azure / data-api-builder

Data API builder provides modern REST and GraphQL endpoints to your Azure Databases and on-prem stores.
https://aka.ms/dab/docs
MIT License
786 stars 142 forks source link

Failure of read database policy for related entity in multiple create for N:1 and 1:1 relationships returns error #2196

Open ayush3797 opened 2 months ago

ayush3797 commented 2 months ago

For N:1 and 1:1 relationships where the read policy prohibits returning the result of the insertion in the related entity, we get an exception:

image

In the config, the policy for read operation is defined as:

image

severussundar commented 1 month ago

This is an existing behavior and not something that is introduced as part of multiple create. The same behavior is observed even for queries (not just mutations) when processing read policy.

DAB version: 0.12.0-rc

In the config file, Publisher entity has a role Anonymous defined with the following read policy

{
          "role": "authenticated",
          "actions": [
            {
              "action": "create"
            },
            {
              "action": "read",
              "policy": {
                "database": "@item.id ne 1234"
              }
            }
          ]
        }

The following graphQL query results in an exception

{
  book_by_pk(id: 1) {
    id
    title
    publisher_id
    publishers {
      id
      name
    }
  }
}

Response:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field.",
      "locations": [
        {
          "line": 6,
          "column": 5
        }
      ],
      "path": [
        "book_by_pk",
        "publishers"
      ],
      "extensions": {
        "code": "HC0018"
      }
    }
  ],
  "data": {
    "book_by_pk": null
  }
}

Mutation:

mutation{
  createbook(item: {
    publisher_id: 1234,
    title: "Book #1"
  }){
    id
    title
    publishers{
      id
      name
    }
  }
}

Response:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field.",
      "locations": [
        {
          "line": 8,
          "column": 5
        }
      ],
      "path": [
        "createbook",
        "publishers"
      ],
      "extensions": {
        "code": "HC0018"
      }
    }
  ],
  "data": {
    "createbook": null
  }
}

Reason for the exception:

The return type for both the operations book_by_pk and createbook : book.

Snip of the book object type:

image

In the schema, publishers field is created as a non-null/required field with type Publisher! ~ this makes hotchocolate throw an error when a null response is to be returned.