-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added automatic GraphQL wrapper for open api specification. (#6398)
- Loading branch information
1 parent
5982df1
commit 892bbf8
Showing
52 changed files
with
6,801 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(Library3TargetFrameworks)</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F2C06745-5F7C-4672-A905-17C8319CE67A}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{2D2DC25A-E937-418B-945F-F8C07246D6B9}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.OpenApi", "src\HotChocolate.OpenApi\HotChocolate.OpenApi.csproj", "{235A80D3-0696-403B-BB06-94E839C2F174}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.OpenApi.Tests", "tests\HotChocolate.OpenApi.Tests\HotChocolate.OpenApi.Tests.csproj", "{D525025A-E91A-43F8-9322-EA0B6C835208}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{235A80D3-0696-403B-BB06-94E839C2F174} = {F2C06745-5F7C-4672-A905-17C8319CE67A} | ||
{D525025A-E91A-43F8-9322-EA0B6C835208} = {2D2DC25A-E937-418B-945F-F8C07246D6B9} | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{235A80D3-0696-403B-BB06-94E839C2F174}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{235A80D3-0696-403B-BB06-94E839C2F174}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{235A80D3-0696-403B-BB06-94E839C2F174}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{235A80D3-0696-403B-BB06-94E839C2F174}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{D525025A-E91A-43F8-9322-EA0B6C835208}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{D525025A-E91A-43F8-9322-EA0B6C835208}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{D525025A-E91A-43F8-9322-EA0B6C835208}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{D525025A-E91A-43F8-9322-EA0B6C835208}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" /> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Nullable.props', '$(MSBuildThisFileDirectory)..\'))" /> | ||
</Project> |
80 changes: 80 additions & 0 deletions
80
src/HotChocolate/OpenApi/src/HotChocolate.OpenApi/Helpers/ObjectTypeFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using HotChocolate.OpenApi.Properties; | ||
using HotChocolate.Skimmed; | ||
using HotChocolate.Types; | ||
using Microsoft.OpenApi.Models; | ||
using INamedType = HotChocolate.Skimmed.INamedType; | ||
using ObjectType = HotChocolate.Skimmed.ObjectType; | ||
|
||
namespace HotChocolate.OpenApi.Helpers; | ||
|
||
internal static class ObjectTypeFactory | ||
{ | ||
/// <summary> | ||
/// Parses the <paramref name="schema"/> and it fields and adds new object types | ||
/// to context if necessary | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="typeName"></param> | ||
/// <param name="schema"></param> | ||
/// <returns></returns> | ||
public static INamedType ParseType(OpenApiWrapperContext context, string typeName, OpenApiSchema schema) | ||
{ | ||
if (context.MutableSchema.Types.ContainsName(typeName)) | ||
{ | ||
return context.MutableSchema.Types[typeName]; | ||
} | ||
|
||
if (Scalars.IsBuiltIn(typeName)) | ||
{ | ||
return new ObjectType(typeName); | ||
} | ||
|
||
var type = new ObjectType(typeName) | ||
{ | ||
Description = schema.Description | ||
}; | ||
|
||
var typeInfo = context.GetSchemaTypeInfo(schema); | ||
|
||
foreach (var property in schema.Properties) | ||
{ | ||
var field = CreateField(context, typeInfo.RootSchema, property); | ||
type.Fields.Add(field); | ||
} | ||
|
||
foreach (var allOf in schema.AllOf) | ||
{ | ||
foreach (var allOfProperty in allOf.Properties) | ||
{ | ||
var field = CreateField(context, typeInfo.RootSchema, allOfProperty); | ||
type.Fields.Add(field); | ||
} | ||
} | ||
|
||
if (!context.MutableSchema.Types.ContainsName(typeName)) | ||
{ | ||
context.MutableSchema.Types.Add(type); | ||
} | ||
|
||
return type; | ||
} | ||
|
||
private static OutputField CreateField(OpenApiWrapperContext context, OpenApiSchema schema, KeyValuePair<string, OpenApiSchema> property) | ||
{ | ||
var typeInfo = context.GetSchemaTypeInfo(property.Value); | ||
var isRequired = schema.Required.Contains(property.Key); | ||
var fieldType = typeInfo.GetGraphQLTypeNode(isRequired); | ||
var field = new OutputField(OpenApiNamingHelper.GetFieldName(property.Key)) | ||
{ | ||
Type = fieldType, | ||
Description = property.Value.Description, | ||
ContextData = | ||
{ | ||
[OpenApiResources.OpenApiPropertyName] = property.Key | ||
} | ||
}; | ||
|
||
ParseType(context, fieldType.NamedType().Name, typeInfo.RootSchema); | ||
return field; | ||
} | ||
} |
Oops, something went wrong.