Skip to content

Commit

Permalink
Start working on a code generation tool (dagger#5422)
Browse files Browse the repository at this point in the history
Fix parse errors

Check in some intermediate stuff

Re-order fields, add readme, etc.

Document source of introspection query

Signed-off-by: Nabeel Sulieman <nabsul@users.noreply.github.com>
  • Loading branch information
nabsul committed Jul 14, 2023
1 parent f301d85 commit fb25a52
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 1 deletion.
6 changes: 6 additions & 0 deletions sdk/dotnet/DaggerSDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{134BD5AA
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTests", "Tests\IntegrationTests\IntegrationTests.csproj", "{A0C7B3A6-61C0-441C-8BD1-66517F08C14D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaggerSDKCodeGen", "DaggerSDKCodeGen\DaggerSDKCodeGen.csproj", "{E05F4484-2F33-472E-B9D3-1D8AC5C4E414}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,6 +25,10 @@ Global
{A0C7B3A6-61C0-441C-8BD1-66517F08C14D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0C7B3A6-61C0-441C-8BD1-66517F08C14D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0C7B3A6-61C0-441C-8BD1-66517F08C14D}.Release|Any CPU.Build.0 = Release|Any CPU
{E05F4484-2F33-472E-B9D3-1D8AC5C4E414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E05F4484-2F33-472E-B9D3-1D8AC5C4E414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E05F4484-2F33-472E-B9D3-1D8AC5C4E414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E05F4484-2F33-472E-B9D3-1D8AC5C4E414}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion sdk/dotnet/DaggerSDK/DaggerSDK.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down
2 changes: 2 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
introspect-api.json
introspect-parsed.json
14 changes: 14 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/DaggerSDKCodeGen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DaggerSDK\DaggerSDK.csproj" />
</ItemGroup>

</Project>
107 changes: 107 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/IntrospectionQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
namespace DaggerSDKCodeGen;

internal class IntrospectionQuery
{
// From: https://github.com/dagger/dagger/blob/db5c12d6df2eb6de73bbf5bb8c5cd5591985013e/codegen/introspection/introspection.graphql#L4
public static string Query = """
query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}

fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}

fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}

fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
""";
}
8 changes: 8 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/ArgType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DaggerSDKCodeGen.Models;

public class ArgType
{
public string? Kind { get; set; }
public string? Name { get; set; }
public TypeDef? OfType { get; set; }
}
9 changes: 9 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/EnumType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DaggerSDKCodeGen.Models;

public class EnumType
{
public string? DeprecationReason { get; set; }
public string? Description { get; set; }
public bool IsDeprecated { get; set; }
public string? Name { get; set; }
}
9 changes: 9 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/InputField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DaggerSDKCodeGen.Models;

public class InputField
{
public string? DefaultValue { get; set; }
public string? Description { get; set; }
public string? Name { get; set; }
public ArgType? Type { get; set; }
}
9 changes: 9 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/QueryArg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DaggerSDKCodeGen.Models;

public class QueryArg
{
public string? DefaultValue { get; set; }
public string? Description { get; set; }
public string? Name { get; set; }
public ArgType? Type { get; set; }
}
9 changes: 9 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/QueryDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DaggerSDKCodeGen.Models;

public class QueryDirective
{
public QueryArg[]? Args { get; set; }
public string? Description { get; set; }
public string[]? Locations { get; set; }
public string? Name { get; set; }
}
11 changes: 11 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/QueryField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DaggerSDKCodeGen.Models;

public class QueryField
{
public QueryArg[]? Args { get; set; }
public string? DeprecationReason { get; set; }
public string? Description { get; set; }
public bool IsDeprecated { get; set; }
public string? Name { get; set; }
public ArgType? Type { get; set; }
}
13 changes: 13 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/QueryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DaggerSDKCodeGen.Models;

public class QueryType
{
public string? Description { get; set; }
public EnumType[]? EnumValues { get; set; }
public QueryField[]? Fields { get; set; }
public InputField[]? InputFields { get; set; }
public string[]? Interfaces { get; set; }
public string? Kind { get; set; }
public string? Name { get; set; }
public string[]? PossibleTypes { get; set; }
}
8 changes: 8 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Models/TypeDef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DaggerSDKCodeGen.Models;

public class TypeDef
{
public string? Kind { get; set; }
public string? Name { get; set; }
public TypeDef? OfType { get; set; }
}
38 changes: 38 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using DaggerSDK.GraphQL;
using DaggerSDKCodeGen;
using DaggerSDKCodeGen.Models;
using System.Text.Json;

var opt = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};

var client = new GraphQLClient();
var resp = await client.RequestAsync(IntrospectionQuery.Query);

var body = await resp.Content.ReadAsStringAsync();
var doc = JsonDocument.Parse(body);
var schema = doc.RootElement.GetProperty("data").GetProperty("__schema");

var directives = JsonSerializer.Deserialize<List<QueryDirective>>(schema.GetProperty("directives"), opt)
?? throw new Exception("Failed to deserialize directives");
var types = JsonSerializer.Deserialize<List<QueryType>>(schema.GetProperty("types"), opt)
?? throw new Exception("Failed to deserialize types");;

Console.WriteLine("Writing introspect-api.json");
File.WriteAllText("introspect-api.json", JsonSerializer.Serialize(new {
directives = schema.GetProperty("directives"),
types = schema.GetProperty("types"),
}, opt));

Console.WriteLine("Writing introspect-resparsedult.json");
File.WriteAllText("introspect-parsed.json", JsonSerializer.Serialize(new
{
directives,
types,
}, opt));

Console.WriteLine("Directives extracted: {0}", directives.Count);
Console.WriteLine("Types extracted: {0}", types.Count);
21 changes: 21 additions & 0 deletions sdk/dotnet/DaggerSDKCodeGen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# GraphQL Schema Code Generator

This is the beginning of a tool for auto-generating the base layer of Dagger SDK.
It runs an introspection query against the Dagger enging and parses the result.
The goal (not yet implemnented) is to generate base C# classes from the GraphQL type definitions.

## What does it do now?

- Connects to Dagger engine and runs the introspection query.
- Parses the Types and Directive data from the result
- Reports number of
- Outputs two files (for debug and comparison):
- JSON serialization of the data directly from the GraphQL API
- JSON serialization of the data after being parsed into Object
- Observing that these two files are identical confirms that parsing worked correctly

## How to run

```
dagger run -- dotnet run
```

0 comments on commit fb25a52

Please sign in to comment.