Skip to content

Commit

Permalink
Merge pull request #931 from clement911/grapqhl-types
Browse files Browse the repository at this point in the history
Added generated file with all GraphQL types
  • Loading branch information
clement911 committed Sep 14, 2023
2 parents 6a5b8b6 + 9c26a0b commit d139d78
Show file tree
Hide file tree
Showing 7 changed files with 50,201 additions and 13 deletions.
87 changes: 87 additions & 0 deletions ShopifySharp.Tests/GenerateGraphQLSchema_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#if NET7_0_OR_GREATER
using ShopifySharp.GraphQL;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Wish.GraphQLSchemaGenerator;
using Xunit;

namespace ShopifySharp.Tests
{
[Trait("Category", "GraphQL")]
public class GenerateGraphQLSchema_Test
{
[Fact(Skip = "This test should be run manually to re-generate the GraphQL types whenever the API version is upgraded")]
public async Task GenerateGraphQLTypes()
{
var scalarNameToTypeName = new Dictionary<string, string>
{
{ "UnsignedInt64", "ulong" },
{ "Money", "decimal" },
{ "Decimal", "decimal" },
{ "DateTime", "DateTime" },//GraphQL datetimes are always UTC
{ "Date", "DateOnly" },
{ "UtcOffset", "TimeSpan" },
{ "URL", "string" },
{ "HTML", "string" },
{ "JSON", "string" },
{ "FormattedString", "string" },
{ "ARN", "string" },
{ "StorefrontID", "string" }
};
string csharpCode = await new GraphQLTypeGenerator().GenerateTypesAsync("ShopifySharp.GraphQL", scalarNameToTypeName, async query =>
{
var res = await new GraphService(Utils.MyShopifyUrl, Utils.AccessToken).PostAsync(query);
var doc = JsonDocument.Parse(res.ToString());
return doc;
});

var strCode = new StringBuilder()
.AppendLine("#if NET6_0_OR_GREATER")
.AppendLine(csharpCode)
.AppendLine("#endif");

File.WriteAllText(@"../../../../ShopifySharp/Entities/GraphQL/GraphQLSchema.generated.cs", strCode.ToString());
}

[Fact]
public async Task GetOrders()
{
var res = await new GraphService(Utils.MyShopifyUrl, Utils.AccessToken).Post2Async(@"
{
orders(first:10)
{
nodes
{
id
createdAt
name
phone
lineItems(first: 10)
{
nodes
{
title
quantity
}
}
}
}
}
");
var orders = res.GetProperty("orders").Deserialize<OrderConnection>();
Assert.True(orders.nodes.Length > 0);
var o = orders.nodes[0];
Assert.True(o.name != null);
Assert.True(o.lineItems.nodes.First().quantity != null);
var commentEventEmbed = o as ICommentEventEmbed;
Assert.NotNull(commentEventEmbed);
Assert.NotNull(commentEventEmbed.AsOrder());
Assert.Null(commentEventEmbed.AsCustomer());
}
}
}
#endif
2 changes: 1 addition & 1 deletion ShopifySharp.Tests/Graph_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task ListsOrdersUsingGraphService()
// will assume we are using a GraphQL string and send with the wrong content type.
var serializerSettings = Infrastructure.Serializer.CreateNewtonsoftSettings();
var serializer = JsonSerializer.Create(serializerSettings);
var requestBody = JToken.FromObject(new
var requestBody = JToken.FromObject(new
{
query = query,
variables = variables
Expand Down
7 changes: 6 additions & 1 deletion ShopifySharp.Tests/ShopifySharp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net472</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net472</TargetFrameworks>
<LangVersion>8</LangVersion>
</PropertyGroup>
<ItemGroup>
Expand All @@ -25,4 +25,9 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Wish.GraphQLSchemaGenerator">
<Version>1.2.0</Version>
</PackageReference>
</ItemGroup>
</Project>

0 comments on commit d139d78

Please sign in to comment.