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
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
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ public static FieldDefinitionNode Build(
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 FieldDefinitionNode Build(
}

// 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 @@ public static FieldDefinitionNode Build(
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 @@ Dictionary<string, EntityMetadata> entityPermissionsMap
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