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
954 stars 197 forks source link

[Bug]: Unable to create new entity and leave ID generation up to the DB, when using `UNIQUEIDENTIFIER DEFAULT NEWID()` #2129

Open pingu2k4 opened 8 months ago

pingu2k4 commented 8 months ago

What happened?

Hey. I have a table in a minimal repro, which you can create with the following:

CREATE TABLE [dbo].[NewTable](
    [id] UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
    [test] INT NOT NULL,
 CONSTRAINT [PK_NewTable] PRIMARY KEY ([id])
)

Go ahead and dab add this entity into your config.

The schema that DAB will be using has a CreateNewTableInput where the id field is nullable, which is accurate as the DB can handle generating the id for us.

However, if we run the mutation by passing the item in as a variable, then it fails with an internal server error.

Example mutation:

mutation CreateNewTable($item: CreateNewTableInput!) {
    createNewTable(item: $item) {
        id
    }
}

variables:

{
    "item": {
        "test": 9001
    }
}

The response I get back from DAB is the following:

{
    "errors": [
        {
            "message": "UUID cannot parse the given literal of type `StringValueNode`.",
            "path": [
                "item",
                "id"
            ],
            "extensions": {
                "field": "CreateNewTableInput.id",
                "fieldType": "UUID"
            }
        }
    ]
}

Version

0.9.7+e560142426d1c080b9fd7b7fabff51a276f6bf61

What database are you using?

Azure SQL

What hosting model are you using?

Local (including CLI), Static Web Apps (SWA)

Which API approach are you accessing DAB through?

GraphQL

Relevant log output

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST https://localhost:5001/graphql application/json 193
dbug: Azure.DataApiBuilder.Core.AuthenticationHelpers.ClientRoleHeaderAuthenticationMiddleware[0]
      792550a5-ee74-4788-9e50-27e8c72f0c3a Request authentication state: Anonymous.
dbug: Azure.DataApiBuilder.Core.AuthenticationHelpers.ClientRoleHeaderAuthenticationMiddleware[0]
      792550a5-ee74-4788-9e50-27e8c72f0c3a The request will be executed in the context of the role: Anonymous
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Hot Chocolate GraphQL Pipeline'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Hot Chocolate GraphQL Pipeline'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 POST https://localhost:5001/graphql application/json 193 - 500 - application/json;+charset=utf-8 19.2080ms


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
Aniruddh25 commented 3 days ago

PR #1883 might fix this issue. We will investigate why that wasnt merged and fix this issue.

abhishekkumams commented 3 days ago

@pingu2k4, I tried with the latest release.

  1. passing the value in query itself. (Working fine). Image

  2. passing in the variables (issue) Image

We will see what's causing the second approach to fail. For now you can use the first approach with the latest release and it should work fine.

pingu2k4 commented 3 days ago

Hi @abhishekkumams - Thanks for taking a look.

We are using strawberry shake to generate our classes for querying GQL, and whilst I imagine we could set things up like that, the reality of our actual use cases is that most models we are creating etc like this have dozens of properties, so needing to specify each of them individually would become quite a pain rather than using a single object containing all the properties.

The same issue exists when using a DateTime in the same way also.