From 6aece0a7558ff6a1b7aae3b145c0551f69af48d3 Mon Sep 17 00:00:00 2001 From: neeraj-sharma2592 <63321305+neeraj-sharma2592@users.noreply.github.com> Date: Thu, 14 Sep 2023 06:22:54 +0530 Subject: [PATCH] Fixes the bug #1680 that necessitated adding the 'name' parameter to the @model directive, regardless of the name of the GraphQL object type's name. (#1706) ## Why make this change? - Closes #1680 - Regression introduced in 0.8.49 caused required explicitly adding "name" directive for all the model/entity irrespective of whether we are using a different name than the one it originally has. - Regression introduced in #1402 when we started using TryExtractGraphQLName as follows: ``` string typeName = GraphQLUtils.TryExtractGraphQLFieldModelName(underlyingType.Directives, out string? modelName) ? modelName : underlyingType.Name; ``` ## What is this change? Checking Directive "name" exists before accessing its value. ## How was this tested? Tested locally for now. Will be checking Test cases for it. - [x] Integration Tests - [ ] Unit Tests --------- Co-authored-by: Neeraj Sharma --- src/Service.GraphQLBuilder/GraphQLUtils.cs | 10 +++++++--- src/Service.Tests/CosmosTests/TestBase.cs | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Service.GraphQLBuilder/GraphQLUtils.cs b/src/Service.GraphQLBuilder/GraphQLUtils.cs index f1c6e0a872..a5d321e4fb 100644 --- a/src/Service.GraphQLBuilder/GraphQLUtils.cs +++ b/src/Service.GraphQLBuilder/GraphQLUtils.cs @@ -186,10 +186,14 @@ public static bool TryExtractGraphQLFieldModelName(IDirectiveCollection fieldDir { if (dir.Name.Value == ModelDirectiveType.DirectiveName) { - dir.ToObject(); - modelName = dir.GetArgument(ModelDirectiveType.ModelNameArgument).ToString(); + ModelDirectiveType modelDirectiveType = dir.ToObject(); + + if (modelDirectiveType.Name.HasValue) + { + modelName = dir.GetArgument(ModelDirectiveType.ModelNameArgument).ToString(); + return modelName is not null; + } - return modelName is not null; } } diff --git a/src/Service.Tests/CosmosTests/TestBase.cs b/src/Service.Tests/CosmosTests/TestBase.cs index a71977c3b3..e2ad31aafb 100644 --- a/src/Service.Tests/CosmosTests/TestBase.cs +++ b/src/Service.Tests/CosmosTests/TestBase.cs @@ -29,6 +29,8 @@ namespace Azure.DataApiBuilder.Service.Tests.CosmosTests; public class TestBase { internal const string DATABASE_NAME = "graphqldb"; + // Intentionally removed name attibute from Planet model to test scenario where the 'name' attribute + // is not explicitly added in the schema internal const string GRAPHQL_SCHEMA = @" type Character @model(name:""Character"") { id : ID, @@ -39,7 +41,7 @@ type Character @model(name:""Character"") { star: Star } -type Planet @model(name:""Planet"") { +type Planet @model { id : ID!, name : String, character: Character,