Skip to content
This repository was archived by the owner on Jul 18, 2023. It is now read-only.

Use GraphQL.Client for Scaffolding #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,4 @@ ModelManifest.xml

# FAKE - F# Make
.fake/
**/.DS_Store
3 changes: 3 additions & 0 deletions src/GraphQLinq.Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<TargetFramework>net6</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<AssemblyName>GraphQLinq.Demo</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\GraphQLinq.Client\Client.csproj" />
<ProjectReference Include="..\GraphQLinq.Generated\Generated.csproj" />
Expand Down
33 changes: 25 additions & 8 deletions src/GraphQLinq.Scaffolding/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using Spectre.Console;

namespace GraphQLinq.Scaffolding
Expand Down Expand Up @@ -135,15 +139,28 @@ private static async Task HandleGenerate(Uri endpoint, string output, string @na

AnsiConsole.MarkupLine("Scaffolding GraphQL client code for [bold]{0}[/] to [bold]{1}[/]", endpoint, outputFolder);

var schema = await AnsiConsole.Status().StartAsync("Performing introspection", async ctx =>
var data = await AnsiConsole.Status().StartAsync("Performing introspection", async ctx =>
{
AnsiConsole.WriteLine("Running introspection query ...");
using var httpClient = new HttpClient();
using var responseMessage = await httpClient.PostAsJsonAsync(endpoint, new { query = IntrospectionQuery });
try
{

AnsiConsole.WriteLine("Running introspection query ...");
GraphQLHttpClient client = new GraphQLHttpClient(endpoint, new SystemTextJsonSerializer());
var request = new GraphQLRequest
{
Query = IntrospectionQuery
};
var responseMessage = await client.SendQueryAsync<Data>(request);

AnsiConsole.WriteLine("Reading and deserializing schema information ...");
var schemaJson = await responseMessage.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<RootSchemaObject>(schemaJson, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
AnsiConsole.WriteLine("Reading and deserializing schema information ...");
return responseMessage.Data;
//var schemaJson = await responseMessage;
//return JsonSerializer.Deserialize<RootSchemaObject>(schemaJson, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
catch (Exception ex)
{
throw ex;
}
});
AnsiConsole.WriteLine();

Expand All @@ -158,7 +175,7 @@ private static async Task HandleGenerate(Uri endpoint, string output, string @na
};

var graphQLClassesGenerator = new GraphQLClassesGenerator(codeGenerationOptions);
return graphQLClassesGenerator.GenerateClient(schema.Data.Schema, endpoint.AbsoluteUri);
return graphQLClassesGenerator.GenerateClient(data.Schema, endpoint.AbsoluteUri);
});

AnsiConsole.WriteLine();
Expand Down
6 changes: 6 additions & 0 deletions src/GraphQLinq.Scaffolding/RootSchemaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ public class GraphqlType : BaseInfo
[JsonConverter(typeof(JsonStringEnumMemberConverter))]
public enum TypeKind
{
[EnumMember(Value = "LIST")]
List,
[EnumMember(Value = "NON_NULL")]
NonNull,
[EnumMember(Value = "SCALAR")]
Scalar,
[EnumMember(Value = "OBJECT")]
Object,
[EnumMember(Value = "INTERFACE")]
Interface,
[EnumMember(Value = "UNION")]
Union,
[EnumMember(Value = "ENUM")]
Enum,
[EnumMember(Value = "INPUT_OBJECT")]
InputObject
Expand Down
13 changes: 12 additions & 1 deletion src/GraphQLinq.Scaffolding/Scaffolding.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<TargetFramework>net7</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>

Expand Down Expand Up @@ -31,13 +31,24 @@
<Description>Generate classes from GraphQL endpoint and write strongly typed queries with LINQ.</Description>
<PackageId>GraphQLinq.Scaffolding</PackageId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<WarningLevel>4</WarningLevel>
<AssemblyName>GraphQLinq.Scaffolding</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Macross.Json.Extensions" Version="2.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
<PackageReference Include="Spectre.Console" Version="0.38.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
<PackageReference Include="GraphQL.Client" Version="5.1.0" />
<PackageReference Include="GraphQL.Client.Serializer.SystemTextJson" Version="5.1.0" />
</ItemGroup>
<ItemGroup>
<None Remove="GraphQL.Client" />
<None Remove="GraphQL.Client.Serializer.Newtonsoft" />
<None Remove="GraphQL.Client.Serializer.SystemTextJson" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\docs\Images\Icon.png">
Expand Down
5 changes: 2 additions & 3 deletions src/GraphQLinq.TestServer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
{
"profiles": {
"GraphQLing.TestServer": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}
1 change: 1 addition & 0 deletions src/GraphQLinq.TestServer/TestServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition=" '$(RunConfiguration)' == 'GraphQLing.TestServer' " />
<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"></AssemblyAttribute>
</ItemGroup>
Expand Down