Skip to content

Commit aa5c8a3

Browse files
severussundaraaronpowellseantleonardAniruddh25abhishekkumams
authored
Fixing unintended update to GraphQL operation when updating REST methods (#1555)
## Why make this change? - Closes #1554 1. Creating a stored procedure entity with graphql configured for Query Command: `dab add getbooks --source "get_books" --source.type "stored-procedure" --permissions "anonymous:execute" --graphql.operation "query" ` Config Json (just the entities section): ```json "entities": { "getbooks": { "source": { "object": "get_books", "type": "stored-procedure", "parameters": null, "key-fields": null }, "graphql": { "enabled": true, "operation": "query", "type": { "singular": "getbooks", "plural": "getbooks" } }, "rest": { "enabled": true, "path": null, "methods": [ "post" ] }, "permissions": [ { "role": "anonymous", "actions": [ { "action": "execute", "fields": null, "policy": { "request": null, "database": null } } ] } ], "mappings": null, "relationships": null } } ``` 2. Update the rest method to GET Command: `dab update getbooks --rest.methods "GET"` Config Json: ```json "entities": { "getbooks": { "source": { "object": "get_books", "type": "stored-procedure", "parameters": null, "key-fields": null }, "graphql": { "enabled": true, "operation": "mutation", "type": { "singular": "getbooks", "plural": "getbooks" } }, "rest": { "enabled": true, "path": null, "methods": [ "get" ] }, "permissions": [ { "role": "anonymous", "actions": [ { "action": "execute", "fields": null, "policy": { "request": null, "database": null } } ] } ], "mappings": null, "relationships": null } } ``` When updating the rest.methods, the graphql operation has lost its original value of `query`. It now has the default value of `mutation`. ## What is this change? - `ConfigGenerator.ConstructUpdatedGraphQLDetails()`: When GraphQL operation is not updated, logic for preserving the existing configured graphQL operation is added - `EndToEndTests.TestUpdatingStoredProcedureWithRestMethods()`: Adds a test case with the mentioned repro steps ## How was this tested? - [x] Unit Tests - [x] Manual Tests ## Sample Request(s) - `dab add getbooks --source "getbooks" --source.type "stored-procedure" --permissions "anonymous:execute" --graphql.operation "query"` ```json "entities": { "getbooks": { "source": { "object": "getbooks", "type": "stored-procedure", "parameters": null, "key-fields": null }, "graphql": { "enabled": true, "operation": "query", "type": { "singular": "getbooks", "plural": "getbooks" } }, "rest": { "enabled": true, "path": null, "methods": [ "post" ] }, "permissions": [ { "role": "anonymous", "actions": [ { "action": "execute", "fields": null, "policy": { "request": null, "database": null } } ] } ], "mappings": null, "relationships": null } } ``` - `dab update getbooks --rest.methods "get,patch"` ```json "entities": { "getbooks": { "source": { "object": "getbooks", "type": "stored-procedure", "parameters": null, "key-fields": null }, "graphql": { "enabled": true, "operation": "query", "type": { "singular": "getbooks", "plural": "getbooks" } }, "rest": { "enabled": true, "path": null, "methods": [ "get", "patch" ] }, "permissions": [ { "role": "anonymous", "actions": [ { "action": "execute", "fields": null, "policy": { "request": null, "database": null } } ] } ], "mappings": null, "relationships": null } ``` The `rest.methods` field is updated with the right values and the existing value of `graphql.operation` is preserved --------- Co-authored-by: Aaron Powell <[email protected]> Co-authored-by: Sean Leonard <[email protected]> Co-authored-by: Aniruddh Munde <[email protected]> Co-authored-by: abhishekkumams <[email protected]>
1 parent a52f103 commit aa5c8a3

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

src/Cli.Tests/EndToEndTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,34 @@ public void TestUpdateEntity()
484484
Assert.AreEqual("Company Name", entity.Mappings["name"]);
485485
}
486486

487+
/// <summary>
488+
/// Validates the updation of REST Methods for a stored procedure entity
489+
/// </summary>
490+
[TestMethod]
491+
public Task TestUpdatingStoredProcedureWithRestMethods()
492+
{
493+
string[] initArgs = { "init", "-c", TEST_RUNTIME_CONFIG_FILE, "--database-type", "mssql",
494+
"--host-mode", "Development", "--connection-string", "testconnectionstring", "--set-session-context", "true" };
495+
Program.Execute(initArgs, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);
496+
497+
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? runtimeConfig));
498+
Assert.IsNotNull(runtimeConfig);
499+
Assert.AreEqual(0, runtimeConfig.Entities.Count()); // No entities
500+
501+
string[] addArgs = { "add", "MyEntity", "-c", TEST_RUNTIME_CONFIG_FILE, "--source", "s001.book", "--permissions", "anonymous:execute", "--source.type", "stored-procedure", "--source.params", "param1:123,param2:hello,param3:true", "--rest.methods", "post,put,patch", "--graphql.operation", "query" };
502+
Program.Execute(addArgs, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);
503+
504+
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? updatedRuntimeConfig));
505+
Assert.AreNotSame(runtimeConfig, updatedRuntimeConfig);
506+
507+
string[] updateArgs = { "update", "MyEntity", "-c", TEST_RUNTIME_CONFIG_FILE, "--rest.methods", "get" };
508+
Program.Execute(updateArgs, _cliLogger!, _fileSystem!, _runtimeConfigLoader!);
509+
510+
Assert.IsTrue(_runtimeConfigLoader!.TryLoadConfig(TEST_RUNTIME_CONFIG_FILE, out RuntimeConfig? updatedRuntimeConfig2));
511+
Assert.AreNotSame(updatedRuntimeConfig, updatedRuntimeConfig2);
512+
return Verify(updatedRuntimeConfig2);
513+
}
514+
487515
/// <summary>
488516
/// Test to validate that the engine starts successfully when --verbose and --LogLevel
489517
/// options are used with the start command
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
DataSource: {
3+
DatabaseType: MSSQL,
4+
Options: {
5+
set-session-context: {
6+
ValueKind: True
7+
}
8+
}
9+
},
10+
Runtime: {
11+
Rest: {
12+
Enabled: true,
13+
Path: /api
14+
},
15+
GraphQL: {
16+
Enabled: true,
17+
Path: /graphql,
18+
AllowIntrospection: true
19+
},
20+
Host: {
21+
Cors: {
22+
AllowCredentials: false
23+
},
24+
Authentication: {
25+
Provider: StaticWebApps,
26+
Jwt: {}
27+
}
28+
}
29+
},
30+
Entities: [
31+
{
32+
MyEntity: {
33+
Source: {
34+
Object: s001.book,
35+
Type: stored-procedure,
36+
Parameters: {
37+
param1: 123,
38+
param2: hello,
39+
param3: true
40+
}
41+
},
42+
GraphQL: {
43+
Singular: MyEntity,
44+
Plural: MyEntities,
45+
Enabled: true,
46+
Operation: Query
47+
},
48+
Rest: {
49+
Methods: [
50+
Get
51+
],
52+
Enabled: true
53+
},
54+
Permissions: [
55+
{
56+
Role: anonymous,
57+
Actions: [
58+
{
59+
Action: Execute,
60+
Policy: {}
61+
}
62+
]
63+
}
64+
]
65+
}
66+
}
67+
]
68+
}

src/Cli/ConfigGenerator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ private static EntityGraphQLOptions ConstructUpdatedGraphQLDetails(Entity entity
10941094
{
10951095
//Updated GraphQL Type
10961096
EntityGraphQLOptions graphQLType = (options.GraphQLType is not null) ? ConstructGraphQLTypeDetails(options.GraphQLType, null) : entity.GraphQL;
1097-
GraphQLOperation? graphQLOperation = null;
1097+
GraphQLOperation? graphQLOperation;
10981098

10991099
if (!IsStoredProcedureConvertedToOtherTypes(entity, options)
11001100
&& (IsStoredProcedure(entity) || IsStoredProcedure(options)))
@@ -1111,6 +1111,12 @@ private static EntityGraphQLOptions ConstructUpdatedGraphQLDetails(Entity entity
11111111
graphQLOperation = null;
11121112
}
11131113
}
1114+
else
1115+
{
1116+
// When the GraphQL operation for a SP entity has not been specified in the update command,
1117+
// assign the existing GraphQL operation.
1118+
graphQLOperation = entity.GraphQL.Operation;
1119+
}
11141120
}
11151121
else
11161122
{

0 commit comments

Comments
 (0)