Skip to content

Commit 4ff9aae

Browse files
authored
Implement the GET verb for FindById operation for MsSql (#98)
## Summary Alongwith graphql, DataGateway Service should support REST api requests too. This change introduces the necessary classes that help add REST support and implements the GET verb for the FindById operation for **MsSql** only. ## How - Adds a `RestController` to accept routes matching `https://localhost:5001/users/id/1?_f=id,username` where `users` is the `entityName`, `id` is the primary key field name, `1` is the value for the primary key `_f` in the query string is the keyword used for the selected field names. - For composite primary keys, the route would be `https://localhost:5001/users/id/1/partition_key/200?_f=id,username` where `id` and `partition_key` together form the primary key. - Adds a `RestService` to handle the request by invoking the `RequestParser` to parse the request and populate the `FindQueryStructure` class which holds the major components of the query to be generated - `MsSqlQueryBuilder` `PostgresQueryBuilder` use the `FindQueryStructure` class to build the required query for the FindById operation. ## Testing - Tested using PostMan that the route for FindById returns expected Json document when given no fields and also with specific fields. - Added `MsSqlRestApiTests` to test the same. Moved some common test code to `MsSqlTestBase`. ## Motivation and future thoughts - This change uses some of the request parsing logic from existing work done here [SqlRestApi](https://msdata.visualstudio.com/DefaultCollection/Database%20Systems/_git/SqlRestApi). In future, for addition of filter clause etc, we can similarly reuse that parsing logic. - It is also inspired by the draft PR(#55) for query generation for GraphQL. The `FindQueryStructure` is similar to the `SqlQueryStructure` class from that PR(#55). `FindQueryStructure` could be useful for CosmosDb query generation so the class `SqlQueryStructure` should be derived from `FindQueryStructure`. Once this and the draft PR are merged in, queries for SQL like databases will be autogenerated for both REST and GraphQL. - When support for CosmosDb query generation #71 is added to the mix, its possible to further redesign the query generation class structure. - For other REST verbs like POST, PUT, DELETE, we would need similar classes like `FindQueryStructure`, at which point again more abstraction would be needed. - [Routing](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0) ## Issues to be addressed in future - Schema Validation - verify PrimaryKey matches the one obtained from the schema tracked by issue : #99 - Tested against PostgreSQL but it needs parameter names in the query to be type casted to the right type. This would need reading schema from the database and add the parameter accordingly. This is tracked by issue : #103 - Do we need streaming ? #102
1 parent d1a0ce1 commit 4ff9aae

File tree

16 files changed

+798
-193
lines changed

16 files changed

+798
-193
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System.Text.Json;
2+
using System.Threading.Tasks;
3+
using Azure.DataGateway.Service.Controllers;
4+
using Azure.DataGateway.Services;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace Azure.DataGateway.Service.Tests.MsSql
8+
{
9+
/// <summary>
10+
/// Test GraphQL Queries validating proper resolver/engine operation.
11+
/// </summary>
12+
[TestClass, TestCategory(TestCategory.MSSQL)]
13+
public class MsSqlGraphQLQueryTests : MsSqlTestBase
14+
{
15+
#region Test Fixture Setup
16+
private static GraphQLService _graphQLService;
17+
private static GraphQLController _graphQLController;
18+
private static readonly string _integrationTableName = "character";
19+
20+
/// <summary>
21+
/// Sets up test fixture for class, only to be run once per test run, as defined by
22+
/// MSTest decorator.
23+
/// </summary>
24+
/// <param name="context"></param>
25+
[ClassInitialize]
26+
public static void InitializeTestFixture(TestContext context)
27+
{
28+
InitializeTestFixture(context, _integrationTableName);
29+
30+
// Setup GraphQL Components
31+
//
32+
_graphQLService = new GraphQLService(_queryEngine, mutationEngine: null, _metadataStoreProvider);
33+
_graphQLController = new GraphQLController(_graphQLService);
34+
}
35+
36+
/// <summary>
37+
/// Cleans up querying table used for Tests in this class. Only to be run once at
38+
/// conclusion of test run, as defined by MSTest decorator.
39+
/// </summary>
40+
[ClassCleanup]
41+
public static void CleanupTestFixture()
42+
{
43+
CleanupTestFixture(_integrationTableName);
44+
}
45+
46+
#endregion
47+
48+
#region Tests
49+
/// <summary>
50+
/// Gets result of quering singular object
51+
/// </summary>
52+
/// <returns></returns>
53+
[TestMethod]
54+
public async Task SingleResultQuery()
55+
{
56+
string graphQLQueryName = "characterById";
57+
string graphQLQuery = "{\"query\":\"{\\n characterById(id:2){\\n name\\n primaryFunction\\n}\\n}\\n\"}";
58+
string msSqlQuery = $"SELECT name, primaryFunction FROM { _integrationTableName } WHERE id = 2 FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER";
59+
60+
string actual = await GetGraphQLResultAsync(graphQLQuery, graphQLQueryName);
61+
string expected = await GetDatabaseResultAsync(msSqlQuery);
62+
63+
Assert.AreEqual(actual, expected);
64+
}
65+
66+
/// <summary>
67+
/// Gets array of results for querying more than one item.
68+
/// </summary>
69+
/// <returns></returns>
70+
[TestMethod]
71+
public async Task MultipleResultQuery()
72+
{
73+
string graphQLQueryName = "characterList";
74+
string graphQLQuery = "{\"query\":\"{\\n characterList {\\n name\\n primaryFunction\\n }\\n}\\n\"}";
75+
string msSqlQuery = $"SELECT name, primaryFunction FROM character FOR JSON PATH, INCLUDE_NULL_VALUES";
76+
77+
string actual = await GetGraphQLResultAsync(graphQLQuery, graphQLQueryName);
78+
string expected = await GetDatabaseResultAsync(msSqlQuery);
79+
80+
Assert.AreEqual(actual, expected);
81+
}
82+
83+
#endregion
84+
85+
#region Query Test Helper Functions
86+
/// <summary>
87+
/// Sends graphQL query through graphQL service, consisting of gql engine processing (resolvers, object serialization)
88+
/// returning JSON formatted result from 'data' property.
89+
/// </summary>
90+
/// <param name="graphQLQuery"></param>
91+
/// <param name="graphQLQueryName"></param>
92+
/// <returns>string in JSON format</returns>
93+
public static async Task<string> GetGraphQLResultAsync(string graphQLQuery, string graphQLQueryName)
94+
{
95+
_graphQLController.ControllerContext.HttpContext = MsSqlTestBase.GetHttpContextWithBody(graphQLQuery);
96+
JsonDocument graphQLResult = await _graphQLController.PostAsync();
97+
JsonElement graphQLResultData = graphQLResult.RootElement.GetProperty("data").GetProperty(graphQLQueryName);
98+
return graphQLResultData.ToString();
99+
}
100+
101+
#endregion
102+
}
103+
}

DataGateway.Service.Tests/MsSqlTests/MsSqlQueryTests.cs

Lines changed: 0 additions & 180 deletions
This file was deleted.

0 commit comments

Comments
 (0)