forked from dagger/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start working on a code generation tool (dagger#5422)
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
Showing
15 changed files
with
265 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
introspect-api.json | ||
introspect-parsed.json |
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,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> |
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,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 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
} |
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,8 @@ | ||
namespace DaggerSDKCodeGen.Models; | ||
|
||
public class ArgType | ||
{ | ||
public string? Kind { get; set; } | ||
public string? Name { get; set; } | ||
public TypeDef? OfType { get; set; } | ||
} |
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,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; } | ||
} |
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,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; } | ||
} |
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,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; } | ||
} |
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,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; } | ||
} |
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,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; } | ||
} |
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,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; } | ||
} |
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,8 @@ | ||
namespace DaggerSDKCodeGen.Models; | ||
|
||
public class TypeDef | ||
{ | ||
public string? Kind { get; set; } | ||
public string? Name { get; set; } | ||
public TypeDef? OfType { get; set; } | ||
} |
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,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); |
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,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 | ||
``` |