Skip to content

Commit

Permalink
Add JSON scalar support.
Browse files Browse the repository at this point in the history
  • Loading branch information
byme8 committed May 1, 2023
1 parent 1120590 commit 5c47e4a
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/TestApp/ZeroQL.TestApp/Generated/GraphQL.g.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 302dc6c36efd8d744950dd4fd73041c0
// e9a97846d5996e1ece2906a06ec5b6af
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
Expand Down Expand Up @@ -95,6 +95,13 @@ public T Author<T>(Func<User, T> selector)
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
public class JsonDocument
{
[ZeroQL.GraphQLFieldSelector("rootElement")]
public global::System.Text.Json.JsonElement RootElement { get; set; }
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
public class KeyValuePairOfStringAndString
{
Expand Down Expand Up @@ -453,6 +460,18 @@ public T Container<T>(Func<TypesContainer, T> selector)
{
return __Container is null ? throw new NullReferenceException("Container is null but it should not be null. Schema can be outdated.") : selector(__Container);
}

[ZeroQL.GraphQLFieldSelector("jsonUsersElement")]
public global::System.Text.Json.JsonElement JsonUsersElement { get; set; }

[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never), JsonPropertyName("JsonUsersDocument")]
public JsonDocument __JsonUsersDocument { get; set; }

[ZeroQL.GraphQLFieldSelector("jsonUsersDocument")]
public T JsonUsersDocument<T>(Func<JsonDocument, T> selector)
{
return __JsonUsersDocument is null ? throw new NullReferenceException("JsonUsersDocument is null but it should not be null. Schema can be outdated.") : selector(__JsonUsersDocument);
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
Expand Down
8 changes: 8 additions & 0 deletions src/TestApp/ZeroQL.TestApp/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type ImageContent {
author: User!
}

type JsonDocument {
rootElement: JSON!
}

type KeyValuePairOfStringAndString {
key: String!
value: String!
Expand Down Expand Up @@ -98,6 +102,8 @@ type Query {
userKind(id: Int!): UserKind!
admin(id: Int!): User
container: TypesContainer!
jsonUsersElement: JSON!
jsonUsersDocument: JsonDocument!
}

type Role {
Expand Down Expand Up @@ -216,6 +222,8 @@ scalar Decimal
"Represents an instant on the global timeline, with nanosecond resolution."
scalar Instant

scalar JSON

"The `Long` scalar type represents non-fractional signed whole 64-bit numeric values. Long can represent values between -(2^63) and 2^63 - 1."
scalar Long

Expand Down
3 changes: 2 additions & 1 deletion src/ZeroQL.TestServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public static async Task StartServer(ServerContext context)
.AddTypeExtension<UserGraphQLExtensions>()
.AddTypeExtension<DateMutation>()
.AddTypeExtension<UserGraphQLMutations>()
.AddTypeExtension<RoleGraphQLExtension>();
.AddTypeExtension<RoleGraphQLExtension>()
.AddTypeExtension<JSONQueryExtensions>();

if (string.IsNullOrEmpty(context.QueriesPath))
{
Expand Down
11 changes: 11 additions & 0 deletions src/ZeroQL.TestServer/Query/JSONQueryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json;

namespace ZeroQL.TestServer.Query;

[ExtendObjectType(typeof(Query))]
public class JSONQueryExtensions
{
public JsonElement GetJsonUsersElement() => JsonSerializer.SerializeToElement(UserGraphQLExtensions.Users.Values.ToArray());

public JsonDocument GetJsonUsersDocument() => JsonSerializer.SerializeToDocument(UserGraphQLExtensions.Users.Values.ToArray());
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ namespace GraphQLClient
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
internal class JsonDocument
{
[ZeroQL.GraphQLFieldSelector("rootElement")]
public global::System.Text.Json.JsonElement RootElement { get; set; }
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
internal class KeyValuePairOfStringAndString
{
Expand Down Expand Up @@ -453,6 +460,18 @@ namespace GraphQLClient
{
return __Container is null ? throw new NullReferenceException("Container is null but it should not be null. Schema can be outdated.") : selector(__Container);
}

[ZeroQL.GraphQLFieldSelector("jsonUsersElement")]
public global::System.Text.Json.JsonElement JsonUsersElement { get; set; }

[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never), JsonPropertyName("JsonUsersDocument")]
public JsonDocument __JsonUsersDocument { get; set; }

[ZeroQL.GraphQLFieldSelector("jsonUsersDocument")]
public T JsonUsersDocument<T>(Func<JsonDocument, T> selector)
{
return __JsonUsersDocument is null ? throw new NullReferenceException("JsonUsersDocument is null but it should not be null. Schema can be outdated.") : selector(__JsonUsersDocument);
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ namespace GraphQLClient
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
public class JsonDocument
{
[ZeroQL.GraphQLFieldSelector("rootElement")]
public global::System.Text.Json.JsonElement RootElement { get; set; }
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
public class KeyValuePairOfStringAndString
{
Expand Down Expand Up @@ -453,6 +460,18 @@ namespace GraphQLClient
{
return __Container is null ? throw new NullReferenceException("Container is null but it should not be null. Schema can be outdated.") : selector(__Container);
}

[ZeroQL.GraphQLFieldSelector("jsonUsersElement")]
public global::System.Text.Json.JsonElement JsonUsersElement { get; set; }

[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never), JsonPropertyName("JsonUsersDocument")]
public JsonDocument __JsonUsersDocument { get; set; }

[ZeroQL.GraphQLFieldSelector("jsonUsersDocument")]
public T JsonUsersDocument<T>(Func<JsonDocument, T> selector)
{
return __JsonUsersDocument is null ? throw new NullReferenceException("JsonUsersDocument is null but it should not be null. Schema can be outdated.") : selector(__JsonUsersDocument);
}
}

[System.CodeDom.Compiler.GeneratedCode ( "ZeroQL" , "1.0.0.0" )]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Query {
userKind(id: Int!): UserKind!
admin(id: Int!): User
container: TypesContainer!
jsonUsersElement: JSON!
jsonUsersDocument: JsonDocument!
}

type Mutation {
Expand Down Expand Up @@ -66,19 +68,19 @@ type Person implements IPerson {
lastName: String!
}

enum UserKind {
SUPPER_GOOD
GOOD
BAD
}

enum UserKindPascal {
default
SupperGood
Good
Bad
}

enum UserKind {
SUPPER_GOOD
GOOD
BAD
}

input UserFilterInput {
userKind: UserKind!
page: PageInput
Expand Down Expand Up @@ -207,6 +209,12 @@ type Role {
name: String!
}

scalar JSON

type JsonDocument {
rootElement: JSON!
}

type KeyValuePairOfStringAndString {
key: String!
value: String!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Id":1,"FirstName":"Jon","LastName":"Smith","UserKind":0},{"Id":2,"FirstName":"Ben","LastName":"Smith","UserKind":2}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Id":1,"FirstName":"Jon","LastName":"Smith","UserKind":0},{"Id":2,"FirstName":"Ben","LastName":"Smith","UserKind":2}]
28 changes: 28 additions & 0 deletions src/ZeroQL.Tests/SourceGeneration/UserScalarTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json;
using Namotion.Reflection;
using ZeroQL.Tests.Core;
using ZeroQL.Tests.Data;

Expand All @@ -23,4 +25,30 @@ public async Task InstantScalarTypeWorks()
await Verify(response)
.DontScrubDateTimes();
}

[Fact]
public async Task JsonElementTypeWorks()
{
var csharpQuery = "static q => q.JsonUsersElement";
var project = await TestProject.Project
.ReplacePartOfDocumentAsync("Program.cs", (TestProject.MeQuery, csharpQuery));

var response = await project.Execute();
var value = response.TryGetPropertyValue<JsonElement>("Data");

await Verify(value.ToString());
}

[Fact]
public async Task JsonDocumentTypeWorks()
{
var csharpQuery = "static q => q.JsonUsersDocument(o => o.RootElement)";
var project = await TestProject.Project
.ReplacePartOfDocumentAsync("Program.cs", (TestProject.MeQuery, csharpQuery));

var response = await project.Execute();
var value = response.TryGetPropertyValue<JsonElement>("Data");

await Verify(value.ToString());
}
}
3 changes: 2 additions & 1 deletion src/ZeroQL.Tools/Internal/TypeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class TypeContext
{ "UUID", "Guid" },
{ "ID", "ID" },
{ "Boolean", "bool" },
{ "Upload", "global::ZeroQL.Upload" }
{ "Upload", "global::ZeroQL.Upload" },
{ "JSON", "global::System.Text.Json.JsonElement" },
};

public TypeContext(GraphQlGeneratorOptions options, HashSet<string> enums, string[] customScalars)
Expand Down

0 comments on commit 5c47e4a

Please sign in to comment.