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
921 stars 189 forks source link

[Feature] Allow one way relationships between stored procedures and tables #2094

Open pingu2k4 opened 7 months ago

pingu2k4 commented 7 months ago

Hi,

We have a need for relationships between a stored proc and other tables.

To try and explain in a minimal way exactly what we are trying to achieve:

It feels as though a one-way relationship from stored proc to table should be able to work, so long as the --relationship.fields param is provided so that DAB knows how to link from SP to table... If this was a feature, then it would allow for far simpler stored procs, as we wouldn't be dealing with the join ourselves, or having to page based on repeated data.... and it would remove our need for mapping on the app's end too.

Here is the relevant log output when attempting to start DAB with a relationship defined on a stored proc:

Logs ``` dbug: Azure.DataApiBuilder.Core.Configurations.RuntimeConfigValidator[0] Employee_Role: gosmarter.Employees_Roles(EmployeeId) is related to one Employee: gosmarter.Employees(EmployeeId). fail: Azure.DataApiBuilder.Service.Startup[0] Unable to complete runtime initialization. Refer to exception for error details. Azure.DataApiBuilder.Service.Exceptions.DataApiBuilderException: Cannot define relationship for entity: Entity { Source = EntitySource { Object = gosmarter.sp_GetEmployees, Type = StoredProcedure, Parameters = System.Collections.Generic.Dictionary`2[System.String,System.Object], KeyFields = }, GraphQL = EntityGraphQLOptions { Singular = GetEmployees, Plural = GetEmployees, Enabled = True, Operation = Mutation }, Rest = EntityRestOptions { Methods = Azure.DataApiBuilder.Config.ObjectModel.SupportedHttpVerb[], Path = , Enabled = True }, Permissions = Azure.DataApiBuilder.Config.ObjectModel.EntityPermission[], Mappings = , Relationships = System.Collections.Generic.Dictionary`2[System.String,Azure.DataApiBuilder.Config.ObjectModel.EntityRelationship] } at Azure.DataApiBuilder.Core.Configurations.RuntimeConfigValidator.ValidateRelationshipsInConfig(RuntimeConfig runtimeConfig, ISqlMetadataProvider sqlMetadataProvider) at Azure.DataApiBuilder.Service.Startup.PerformOnConfigChangeAsync(IApplicationBuilder app) fail: Azure.DataApiBuilder.Service.Startup[0] Could not initialize the engine with the runtime config file: .\staticwebapp.database.config.json info: Microsoft.Hosting.Lifetime[0] Application is shutting down... Unable to launch the Data API builder engine. Error: Failed to start the engine. ```
Benjiiim commented 7 months ago

Hello @pingu2k4 Adding this use case in #1903 may be interesting.

pingu2k4 commented 7 months ago

@Benjiiim Cheers, have added to that issue. :)

FelixZY commented 7 months ago

Are stored procedures (especially with parameters) supported at all at this point?

seantleonard commented 7 months ago

Are stored procedures (especially with parameters) supported at all at this point?

Stored procedures in DAB don't support relationships. They are supported for querying and mutations in GraphQL and REST see https://learn.microsoft.com/azure/data-api-builder/views-and-stored-procedures#stored-procedures

FelixZY commented 7 months ago

Adding a note for future googlers:

Stored procedures are not supported by all database types yet. See also #1023 , #1024 , #1500