Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SchemaExporter, schema.ToAST(), and schema.Print() #3649

Merged
merged 22 commits into from Jul 31, 2023
Merged

Conversation

Shane32
Copy link
Member

@Shane32 Shane32 commented Jun 19, 2023

This PR adds capability to 'export' a configured GraphQL.NET schema into an AST document suitable for printing by GraphQL.NET Parser into SDL. The new SchemaExporter class currently has no options and will export the entire schema. The GraphQL.NET Parser library will then handle sorting the schema and/or printing it, as desired. An extension method has been added to ISchema to easily export and print the schema in one step. In order to support the IncludeDeprecationReasons and IncludeDescriptions properties of the old SchemaPrinter, AST visitors have been added to remove the undesired information.
Those properties have a default value of true (previously false). The property for sorting preferences defaults to unsorted. All in, this should remove the need for the SchemaPrinter and FederatedSchemaPrinter classes.

Waiting on:

Replaces:

Closes:

@Shane32 Shane32 self-assigned this Jun 19, 2023
@Shane32 Shane32 changed the base branch from master to develop June 19, 2023 04:35
@Shane32 Shane32 added this to the 8.0 milestone Jun 19, 2023
@Shane32 Shane32 changed the base branch from develop to master June 20, 2023 03:04
@Shane32 Shane32 modified the milestones: 8.0, 7.6 Jun 20, 2023
src/GraphQL/GraphQL.csproj Outdated Show resolved Hide resolved
Comment on lines +237 to +290
if (graphType.HasExtensionAstTypes() && graphType.GetAstType<ASTNode>() == null)
{
return node switch
{
GraphQLObjectTypeDefinition otd => new GraphQLObjectTypeExtension(otd.Name)
{
Interfaces = otd.Interfaces,
Fields = otd.Fields,
Directives = otd.Directives,
Comments = otd.Comments,
},
GraphQLInterfaceTypeDefinition itd => new GraphQLInterfaceTypeExtension(itd.Name)
{
Interfaces = itd.Interfaces,
Fields = itd.Fields,
Directives = itd.Directives,
Comments = itd.Comments,
},
GraphQLUnionTypeDefinition utd => new GraphQLUnionTypeExtension(utd.Name)
{
Types = utd.Types,
Directives = utd.Directives,
Comments = utd.Comments,
},
GraphQLScalarTypeDefinition std => new GraphQLScalarTypeExtension(std.Name)
{
Directives = std.Directives,
Comments = std.Comments,
},
GraphQLEnumTypeDefinition etd => new GraphQLEnumTypeExtension(etd.Name)
{
Values = etd.Values,
Directives = etd.Directives,
Comments = etd.Comments,
},
GraphQLInputObjectTypeDefinition iotd => new GraphQLInputObjectTypeExtension(iotd.Name)
{
Fields = iotd.Fields,
Directives = iotd.Directives,
Comments = iotd.Comments,
},
GraphQLSchemaDefinition sd => new GraphQLSchemaExtension
{
OperationTypes = sd.OperationTypes,
Directives = sd.Directives,
Comments = sd.Comments,
},
_ => throw new InvalidOperationException($"Invalid node type of '{node.GetType().GetFriendlyName()}'.")
};
}
else
{
return node;
}

Check notice

Code scanning / CodeQL

Missed ternary opportunity Note

Both branches of this 'if' statement return - consider using '?' to express intent better.
@Shane32 Shane32 changed the title [WIP] Add SchemaExporter [WIP] Add SchemaExporter, schema.ToAST(), and schema.Print() Jul 18, 2023
Comment on lines +28 to +38
if (directive.Name.Value == "deprecated")
{
if (directive.Arguments != null)
{
directive.Arguments.Items.RemoveAll(a => a.Name.Value == "reason");
if (directive.Arguments.Items.Count == 0)
{
directive.Arguments = null;
}
}
}

Check notice

Code scanning / CodeQL

Nested 'if' statements can be combined Note

These 'if' statements can be combined.
@codecov-commenter
Copy link

Codecov Report

Merging #3649 (006d8c8) into master (a7b144b) will increase coverage by 0.02%.
The diff coverage is 85.62%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master    #3649      +/-   ##
==========================================
+ Coverage   84.01%   84.03%   +0.02%     
==========================================
  Files         394      399       +5     
  Lines       17099    17593     +494     
  Branches     2736     2799      +63     
==========================================
+ Hits        14366    14785     +419     
- Misses       2084     2145      +61     
- Partials      649      663      +14     
Impacted Files Coverage Δ
...hQL/Utilities/Federation/FederatedSchemaPrinter.cs 79.31% <ø> (ø)
src/GraphQL/Utilities/SchemaPrinter.cs 85.26% <ø> (ø)
src/GraphQL/Utilities/SchemaExporter.cs 82.43% <82.43%> (ø)
...Utilities/Visitors/RemoveFederationTypesVisitor.cs 89.28% <89.28%> (ø)
src/GraphQL/Execution/DocumentExecuter.cs 93.58% <100.00%> (+0.03%) ⬆️
src/GraphQL/Extensions/SchemaExtensions.cs 80.57% <100.00%> (+3.70%) ⬆️
src/GraphQL/Utilities/PrintOptions.cs 100.00% <100.00%> (ø)
...lities/Visitors/RemoveDeprecationReasonsVisitor.cs 100.00% <100.00%> (ø)
...QL/Utilities/Visitors/RemoveDescriptionsVisitor.cs 100.00% <100.00%> (ø)

... and 6 files with indirect coverage changes

@Shane32 Shane32 changed the title [WIP] Add SchemaExporter, schema.ToAST(), and schema.Print() Add SchemaExporter, schema.ToAST(), and schema.Print() Jul 23, 2023
@Shane32 Shane32 requested a review from sungam3r July 23, 2023 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants