Skip to content

Commit fad4c0c

Browse files
Block execution of multiple mutation operations
1 parent 028ec90 commit fad4c0c

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

ndc-lambda-sdk/src/execution.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ export async function executeQuery(queryRequest: sdk.QueryRequest, functionsSche
4141
}
4242

4343
export async function executeMutation(mutationRequest: sdk.MutationRequest, functionsSchema: schema.FunctionsSchema, runtimeFunctions: RuntimeFunctions): Promise<sdk.MutationResponse> {
44-
const operationResults: sdk.MutationOperationResults[] = [];
44+
if (mutationRequest.operations.length > 1)
45+
throw new sdk.NotSupported("Transactional mutations (multiple operations) are not supported");
46+
if (mutationRequest.operations.length <= 0)
47+
throw new sdk.BadRequest("One mutation operation must be provided")
4548

46-
for (const mutationOperation of mutationRequest.operations) {
47-
const result = await executeMutationOperation(mutationOperation, functionsSchema, runtimeFunctions);
48-
operationResults.push(result);
49-
}
49+
const mutationOperation = mutationRequest.operations[0]!;
50+
const result = await executeMutationOperation(mutationOperation, functionsSchema, runtimeFunctions);
5051

5152
return {
52-
operation_results: operationResults
53+
operation_results: [result]
5354
};
5455
}
5556

ndc-lambda-sdk/test/execution/execute-mutation.test.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("execute mutation", function() {
4242
"String": {}
4343
}
4444
};
45-
const queryRequest: sdk.MutationRequest = {
45+
const mutationRequest: sdk.MutationRequest = {
4646
operations: [
4747
{
4848
type: "procedure",
@@ -56,7 +56,7 @@ describe("execute mutation", function() {
5656
collection_relationships: {}
5757
};
5858

59-
const result = await executeMutation(queryRequest, functionSchema, runtimeFunctions);
59+
const result = await executeMutation(mutationRequest, functionSchema, runtimeFunctions);
6060
assert.deepStrictEqual(result, {
6161
operation_results: [
6262
{
@@ -70,6 +70,70 @@ describe("execute mutation", function() {
7070
assert.equal(functionCallCount, 1);
7171
});
7272

73+
it("blocks execution of multiple operations", async function() {
74+
let functionCallCount = 0;
75+
const runtimeFunctions = {
76+
"theFunction": (param: string) => {
77+
functionCallCount++;
78+
return `First function called with '${param}'`;
79+
}
80+
};
81+
const functionSchema: FunctionsSchema = {
82+
functions: {
83+
"theFunction": {
84+
ndcKind: FunctionNdcKind.Procedure,
85+
description: null,
86+
parallelDegree: null,
87+
arguments: [
88+
{
89+
argumentName: "param",
90+
description: null,
91+
type: {
92+
type: "named",
93+
kind: "scalar",
94+
name: "String"
95+
}
96+
},
97+
],
98+
resultType: {
99+
type: "named",
100+
kind: "scalar",
101+
name: "String"
102+
}
103+
}
104+
},
105+
objectTypes: {},
106+
scalarTypes: {
107+
"String": {}
108+
}
109+
};
110+
const mutationRequest: sdk.MutationRequest = {
111+
operations: [
112+
{
113+
type: "procedure",
114+
name: "theFunction",
115+
fields: {},
116+
arguments: {
117+
"param": "test"
118+
}
119+
},
120+
{
121+
type: "procedure",
122+
name: "theFunction",
123+
fields: {},
124+
arguments: {
125+
"param": "test2"
126+
}
127+
}
128+
],
129+
collection_relationships: {}
130+
};
131+
132+
await expect(executeMutation(mutationRequest, functionSchema, runtimeFunctions))
133+
.to.be.rejectedWith(sdk.NotSupported, "Transactional mutations (multiple operations) are not supported");
134+
assert.equal(functionCallCount, 0);
135+
});
136+
73137
describe("function error handling", function() {
74138
const functionSchema: FunctionsSchema = {
75139
functions: {

0 commit comments

Comments
 (0)