Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Core/Resolvers/Sql Query Structures/SqlQueryStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,19 @@ private SqlQueryStructure(
AddColumnsForEndCursor();
}

// if the user does a paginated query only requesting hasNextPage
// there will be no elements in Columns
if (!Columns.Any())
{
AddColumn(PrimaryKey()[0]);
}

if (PaginationMetadata.RequestedHasNextPage)
{
_limit++;
}
}

// If there are no columns, add the primary key column
// to prevent failures when executing the database query.
if (!Columns.Any())
{
AddColumn(PrimaryKey()[0]);
}

ParametrizeColumns();
}

Expand Down
71 changes: 71 additions & 0 deletions src/Service.Tests/CosmosTests/MutationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,77 @@ public async Task UpdateItemWithUnauthorizedWildCardReturnsError()
Assert.IsTrue(errorMessage.Contains(DataApiBuilderException.GRAPHQL_MUTATION_FIELD_AUTHZ_FAILURE));
}

/// <summary>
/// Validates that a create mutation with only __typename in the selection set returns the
/// right type
/// </summary>
[TestMethod]
public async Task CreateMutationWithOnlyTypenameInSelectionSet()
{
string graphQLMutation = @"
mutation ($item: CreatePlanetInput!) {
createPlanet (item: $item) {
__typename
}
}";

// Construct the inputs required for the mutation
string id = Guid.NewGuid().ToString();
var input = new
{
id,
name = "test_name",
stars = new[] { new { id = "TestStar" } }
};
JsonElement response = await ExecuteGraphQLRequestAsync("createPlanet", graphQLMutation, new() { { "item", input } });

// Validate results
string expected = @"Planet";
string actual = response.GetProperty("__typename").Deserialize<string>();
Assert.AreEqual(expected, actual);
}

/// <summary>
/// Validates that an update mutation with only __typename in the selection set returns the
/// right type
/// </summary>
[TestMethod]
public async Task UpdateMutationWithOnlyTypenameInSelectionSet()
{
// Create the item with a known id to execute an update mutation against it
string id = Guid.NewGuid().ToString();
var input = new
{
id,
name = "test_name"
};

_ = await ExecuteGraphQLRequestAsync("createPlanet", _createPlanetMutation, new() { { "item", input } });

string mutation = @"
mutation ($id: ID!, $partitionKeyValue: String!, $item: UpdatePlanetInput!) {
updatePlanet (id: $id, _partitionKeyValue: $partitionKeyValue, item: $item) {
__typename
}
}";

// Construct the inputs required for the update mutation
var update = new
{
id,
name = "new_name",
stars = new[] { new { id = "TestStar" } }
};

// Execute the update mutation
JsonElement response = await ExecuteGraphQLRequestAsync("updatePlanet", mutation, variables: new() { { "id", id }, { "partitionKeyValue", id }, { "item", update } });

// Validate results
string expected = @"Planet";
string actual = response.GetProperty("__typename").Deserialize<string>();
Assert.AreEqual(expected, actual);
}

/// <summary>
/// Runs once after all tests in this class are executed
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/Service.Tests/CosmosTests/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ public async Task GetListOfString()
CollectionAssert.AreEqual(new[] { "tag1", "tag2" }, tags);
}

/// <summary>
/// Validates that a query with only __typename in the selection set
/// returns the right type
/// </summary>
[TestMethod]
public async Task QueryWithOnlyTypenameInSelectionSet()
{
string id = _idList[0];
JsonElement response = await ExecuteGraphQLRequestAsync("planet_by_pk", @"
query ($id: ID, $partitionKeyValue: String) {
planet_by_pk (id: $id, _partitionKeyValue: $partitionKeyValue) {
__typename
}
}", new() { { "id", id }, { "partitionKeyValue", id } });

string expected = @"Planet";
string actual = response.GetProperty("__typename").Deserialize<string>();
Assert.AreEqual(expected, actual);
}

[TestMethod]
public async Task GetPaginatedWithVariables()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,111 @@ public async Task UpdateMutation(string dbQuery)
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
}

/// <summary>
/// <code>Do: </code>Update book in database and return the typename of the entity
/// <code>Check: </code>if the mutation executed successfully and returned the correct typename
/// </summary>
[TestMethod]
public async Task UpdateMutationWithOnlyTypenameInSelectionSet()
{
string graphQLMutationName = "updatebook";
string graphQLMutation = @"
mutation {
updatebook(id: 1, item: { title: ""Even Better Title"", publisher_id: 2345} ) {
__typename
}
}
";

JsonElement actual = await ExecuteGraphQLRequestAsync(graphQLMutation, graphQLMutationName, isAuthenticated: true);
string expected = @"
{
""__typename"": ""book""
}
";

SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
}

/// <summary>
/// <code>Do :</code> Delete a book from database and return the typename of the book entity
/// <code>Check :</code>if the mutation executed successfully and returned the correct typename
/// </summary>
[TestMethod]
public async Task DeleteMutationWithOnlyTypename()
{
string graphQLMutationName = "deletebook";
string graphQLMutation = @"
mutation {
deletebook(id: 1) {
__typename
}
}
";

string expected = @"
{
""__typename"": ""book""
}
";

JsonElement actual = await ExecuteGraphQLRequestAsync(graphQLMutation, graphQLMutationName, isAuthenticated: true);
SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
}

/// <summary>
/// <code>Do: </code>Inserts a new book in database and returns the typename of the book entity
/// <code>Check: </code>if the mutation executed successfully and returned the correct typename
/// </summary>
[TestMethod]
public async Task InsertMutationWithOnlyTypenameInSelectionSet()
{
string graphQLMutationName = "createbook";
string graphQLMutation = @"
mutation {
createbook(item: { title: ""Awesome Book"", publisher_id: 1234 }) {
__typename
}
}
";

JsonElement actual = await ExecuteGraphQLRequestAsync(graphQLMutation, graphQLMutationName, isAuthenticated: true);
string expected = @"
{
""__typename"": ""book""
}
";

SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
}

/// <summary>
/// <code>Do: </code> Execute a stored procedure and return the typename of the SP entity
/// <code>Check :</code>if the mutation executed successfully and returned the correct typename
/// </summary>
public virtual async Task ExecuteMutationWithOnlyTypenameInSelectionSet()
{
string graphQLMutationName = "executeCountBooks";
string graphQLMutation = @"
mutation {
executeCountBooks{
__typename
}
}
";

JsonElement actual = await ExecuteGraphQLRequestAsync(graphQLMutation, graphQLMutationName, isAuthenticated: true);
string expected = @"
[
{
""__typename"": ""CountBooks""
}
]
";

SqlTestHelper.PerformTestEqualJsonStrings(expected, actual.ToString());
}

/// <summary>
/// <code>Do: </code>Update Sales in database and return its updated fields
/// <code>Check: The calculated column has successfully been updated after updating the other fields </code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,16 @@ ORDER BY [id]
await InsertIntoInsertableComplexView(msSqlQuery);
}

/// <summary>
/// <code>Do: </code> Execute a stored procedure and return the typename of the SP entity
/// <code>Check :</code>if the mutation executed successfully and returned the correct typename
/// </summary>
[TestMethod]
public async override Task ExecuteMutationWithOnlyTypenameInSelectionSet()
{
await base.ExecuteMutationWithOnlyTypenameInSelectionSet();
}

#endregion

#region Negative Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ ORDER BY `id` LIMIT 1
await InsertIntoInsertableComplexView(mySqlQuery);
}

/// <inheritdoc/>
[TestMethod]
[Ignore]
public override Task ExecuteMutationWithOnlyTypenameInSelectionSet()
{
throw new NotImplementedException();
}

#endregion

#region Negative Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@ ORDER BY id
await InsertIntoInsertableComplexView(postgresQuery);
}

/// <inheritdoc/>
[TestMethod]
[Ignore]
public override Task ExecuteMutationWithOnlyTypenameInSelectionSet()
{
throw new NotImplementedException();
}

#endregion

#region Negative Tests
Expand Down
Loading