Skip to content

Commit

Permalink
Fixes the bug #1680 that necessitated adding the 'name' parameter to …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
neeraj-sharma2592 and Neeraj Sharma authored Sep 14, 2023
1 parent 69f4535 commit 9b0304e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Service.GraphQLBuilder/GraphQLUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ public static bool TryExtractGraphQLFieldModelName(IDirectiveCollection fieldDir
{
if (dir.Name.Value == ModelDirectiveType.DirectiveName)
{
dir.ToObject<ModelDirectiveType>();
modelName = dir.GetArgument<string>(ModelDirectiveType.ModelNameArgument).ToString();
ModelDirectiveType modelDirectiveType = dir.ToObject<ModelDirectiveType>();

if (modelDirectiveType.Name.HasValue)
{
modelName = dir.GetArgument<string>(ModelDirectiveType.ModelNameArgument).ToString();
return modelName is not null;
}

return modelName is not null;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Service.Tests/CosmosTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 9b0304e

Please sign in to comment.