Skip to content

Commit

Permalink
Fixing nit comments from previous DW mutation support prs. (#2043)
Browse files Browse the repository at this point in the history
## Why make this change?

Fixing nit comments from earlier pr:
#1978

## What is this change?

There were some nit comments to fix while adding implementation for
mutation support for DW.

## How was this tested?

Existing tests should cover as this pr only handles nit fixes.
  • Loading branch information
rohkhann committed Feb 16, 2024
1 parent 165ba8a commit 81e42c9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Core/Services/GraphQLSchemaCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ private DocumentNode GenerateSqlGraphQLObjects(RuntimeEntities entities, Diction
}

Dictionary<string, FieldDefinitionNode> fields = new();

// Add the DBOperationResult type to the schema
NameNode nameNode = new(value: GraphQLUtils.DB_OPERATION_RESULT_TYPE);
FieldDefinitionNode field = GetDbOperationResultField();

fields.TryAdd("result", field);
fields.TryAdd(GraphQLUtils.DB_OPERATION_RESULT_FIELD_NAME, field);

objectTypes.Add(GraphQLUtils.DB_OPERATION_RESULT_TYPE, new ObjectTypeDefinitionNode(
location: null,
Expand Down Expand Up @@ -290,7 +292,7 @@ private static FieldDefinitionNode GetDbOperationResultField()
{
return new(
location: null,
name: new("result"),
name: new(GraphQLUtils.DB_OPERATION_RESULT_FIELD_NAME),
description: new StringValueNode("Contains result for mutation execution"),
arguments: new List<InputValueDefinitionNode>(),
type: new StringType().ToTypeNode(),
Expand Down
8 changes: 5 additions & 3 deletions src/Service.GraphQLBuilder/GraphQLUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static class GraphQLUtils
public const string OBJECT_TYPE_QUERY = "query";
public const string SYSTEM_ROLE_ANONYMOUS = "anonymous";
public const string DB_OPERATION_RESULT_TYPE = "DbOperationResult";
public const string DB_OPERATION_RESULT_FIELD_NAME = "result";

public static bool IsModelType(ObjectTypeDefinitionNode objectTypeDefinitionNode)
{
Expand Down Expand Up @@ -319,9 +320,11 @@ public static string GetDataSourceNameFromGraphQLContext(IMiddlewareContext cont
/// </summary>
public static string GetEntityNameFromContext(IMiddlewareContext context)
{
string entityName = context.Selection.Field.Type.TypeName();
IOutputType type = context.Selection.Field.Type;
string graphQLTypeName = type.TypeName();
string entityName = graphQLTypeName;

if (entityName is DB_OPERATION_RESULT_TYPE)
if (graphQLTypeName is DB_OPERATION_RESULT_TYPE)
{
// CUD for a mutation whose result set we do not have. Get Entity name mutation field directive.
if (GraphQLUtils.TryExtractGraphQLFieldModelName(context.Selection.Field.Directives, out string? modelName))
Expand All @@ -333,7 +336,6 @@ public static string GetEntityNameFromContext(IMiddlewareContext context)
{
// for rest of scenarios get entity name from output object type.
ObjectType underlyingFieldType;
IOutputType type = context.Selection.Field.Type;
underlyingFieldType = GraphQLUtils.UnderlyingGraphQLEntityType(type);
// Example: CustomersConnectionObject - for get all scenarios.
if (QueryBuilder.IsPaginationType(underlyingFieldType))
Expand Down
4 changes: 2 additions & 2 deletions src/Service.GraphQLBuilder/Mutations/CreateMutationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ private static NameNode GenerateInputTypeName(string typeName)
databaseType,
entities);

// Create authorize directive denoting allowed roles
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode("name", dbEntityName)) };
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode(ModelDirectiveType.ModelNameArgument, dbEntityName)) };

// Create authorize directive denoting allowed roles
if (CreateAuthorizationDirectiveIfNecessary(
rolesAllowedForMutation,
out DirectiveNode? authorizeDirective))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static class DeleteMutationBuilder
}

// Create authorize directive denoting allowed roles
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode("name", dbEntityName)) };
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode(ModelDirectiveType.ModelNameArgument, dbEntityName)) };

if (CreateAuthorizationDirectiveIfNecessary(
rolesAllowedForMutation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private static NameNode GenerateInputTypeName(string typeName)
new List<DirectiveNode>()));

// Create authorize directive denoting allowed roles
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode("name", dbEntityName)) };
List<DirectiveNode> fieldDefinitionNodeDirectives = new() { new(ModelDirectiveType.DirectiveName, new ArgumentNode(ModelDirectiveType.ModelNameArgument, dbEntityName)) };

if (CreateAuthorizationDirectiveIfNecessary(
rolesAllowedForMutation,
Expand Down
3 changes: 2 additions & 1 deletion src/Service.Tests/GraphQLBuilder/MutationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Azure.DataApiBuilder.Config.DatabasePrimitives;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Service.Exceptions;
using Azure.DataApiBuilder.Service.GraphQLBuilder;
using Azure.DataApiBuilder.Service.GraphQLBuilder.Mutations;
using Azure.DataApiBuilder.Service.Tests.GraphQLBuilder.Helpers;
using HotChocolate.Language;
Expand Down Expand Up @@ -94,7 +95,7 @@ private static Entity GenerateEmptyEntity()
FieldDefinitionNode createField =
query.Fields.Where(f => f.Name.Value == $"createFoo").First();
Assert.AreEqual(expected: isAuthorizeDirectiveExpected ? 1 : 0,
actual: createField.Directives.Where(x => x.Name.Value is "authorize").Count());
actual: createField.Directives.Where(x => x.Name.Value is GraphQLUtils.AUTHORIZE_DIRECTIVE).Count());
}

[TestMethod]
Expand Down

0 comments on commit 81e42c9

Please sign in to comment.