data-api-builder icon indicating copy to clipboard operation
data-api-builder copied to clipboard

Enable caching stored procedure results -> REST (GET) GraphQL (Queries)

Open seantleonard opened this issue 1 year ago • 0 comments

Why make this change?

  • Closes #2299
  • Enabling DAB's cache only worked for generic table/view queries (REST: GET, GraphQL: queries). Stored procedure results were always fetched from the database.
  • This change enables caching for stored procedure results when the stored procedure is configured as such:
"entities": {
  "MyStoredProcedureEntity": {
    "source": {
      "type": "stored-procedure",
      "object": "dbo.spExample"
    },
    "graphql": {
      "operation": "query"
    },
    "rest": {
      "methods": [ "GET" ]
    },
    "cache": {
      "enabled": true,
      "ttl-seconds": 5
    },
    "permissions": [
      {
        "role": "anonymous",
        "actions": [
          "execute"
        ]
      }
    ]
  }

What is this change?

  • Updates DAB's SqlQueryEngine function private async Task<JsonDocument?> ExecuteAsync(SqlExecuteStructure structure, string dataSourceName) to utilize DAB's cache service.
  • The code now checks whether caching is enabled globally:
    • RuntimeConfig.Cache.Enabled is true
    • RuntimeConfig.DataSource.Options.SetSessionContext is false
  • The cache service is invoked with type JsonArray : _cache.GetOrSetAsync<JsonArray?>(...)

How was this tested?

  • [ ] Integration Tests
  • [x] Unit Tests: tests that cache invocation with type JsonArray succeeds. This covers how stored procedures are executed in the query engine.

Sample Request(s)

Dab config: use example entity at top of this description. Example stored procedure:

create procedure [dbo].[stp_Dummy]
@mandatoryParam int,
@optionalParam int = 10
as
select 
    @mandatoryParam as mandatoryParam,
    @optionalParam as optionalParam
GO

REST

GET http://localhost:5000/api/Dummy?mandatoryParam=123&optionalParam=2345

GraphQL

query mySP{
  executeDummy {
    mandatoryParam
    optionalParam
  }
}

seantleonard avatar Aug 01 '24 21:08 seantleonard