Skip to content

Commit

Permalink
fix resolving of custom fields in GraphQL module (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Jul 26, 2024
1 parent 8ee0c1c commit 0f164fc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using commercetools.Sdk.Api.Client;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Models.Types;
using commercetools.Sdk.GraphQL.Api;
using Xunit;
using static commercetools.Api.IntegrationTests.Type.TypeFixtures;
using static commercetools.Api.IntegrationTests.Cart.CartFixture;
using CustomFieldsDraft = commercetools.Sdk.Api.Models.Types.CustomFieldsDraft;
using FieldDefinition = commercetools.Sdk.Api.Models.Types.FieldDefinition;
using FieldType = commercetools.Sdk.Api.Models.Types.FieldType;
using LocalizedString = commercetools.Sdk.Api.Models.Common.LocalizedString;

namespace commercetools.Api.IntegrationTests.Type
{
Expand Down Expand Up @@ -71,6 +79,80 @@ await WithType(
});
}

[Fact]
public async Task CartWithCustomType()
{
var gqlClient = _client.GraphQLClient();

var key = $"QueryType-{TestingUtility.RandomString()}";

await WithType(
_client,
typeDraft =>
{
var draft = DefaultTypeDraftWithKey(typeDraft, key);
draft.FieldDefinitions = new List<IFieldDefinition>()
{
new FieldDefinition()
{
Type = new CustomFieldStringType()
{
},
Label = new LocalizedString()
{
{"en", "test-string"}
},
Required = false,
Name = "test-string"
},
new FieldDefinition()
{
Type = new CustomFieldSetType()
{
ElementType = new CustomFieldStringType()
},
Label = new LocalizedString()
{
{"en", "test-set"}
},
Required = false,
Name = "test-set"
}
};
return draft;
},
async type =>
{
await WithCart(_client, cartDraft =>
{
var draft = DefaultCartDraft(cartDraft);
draft.Custom = new CustomFieldsDraft()
{
Type = new TypeResourceIdentifier() { Key = type.Key },
Fields = new FieldContainer()
{
{ "test-set", new List<string>()
{
"abc",
"def"
}
},
{ "test-string", "foo"
}
}
};
return draft;
}, async cart =>
{
var cartId = cart.Id;
var t = await gqlClient.Query(o => o.Cart(id: cartId, selector: cart => new { Custom = cart.Custom(custom => new { Field = custom.CustomFieldsRaw(selector: field => field.Value) })}));
Assert.Equal("abc", t.Data.Custom.Field[0].Deserialize<List<string>>()[0]);
Assert.Equal("def", t.Data.Custom.Field[0].Deserialize<List<string>>()[1]);
Assert.Equal("foo", t.Data.Custom.Field[1].ToString());
});
});
}

[Fact]
public async Task UpdateTypeByIdChangeDescription()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<UserSecretsId>6e42aa04-1612-4e1c-8bb2-190e5c88343f</UserSecretsId>
<IsTestProject>true</IsTestProject>
Expand All @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.21" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
Expand All @@ -32,6 +33,7 @@
<ProjectReference Include="..\..\commercetools.Base.Serialization\commercetools.Base.Serialization.csproj" />
<ProjectReference Include="..\..\commercetools.Sdk.Api\commercetools.Sdk.Api.csproj" />
<ProjectReference Include="..\..\commercetools.Sdk.ImportApi\commercetools.Sdk.ImportApi.csproj" />
<ProjectReference Include="..\..\commercetools.Sdk.GraphQL.Api\commercetools.Sdk.GraphQL.Api.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.test.Development.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"BigDecimal": "System.Decimal",
"Country": "System.String",
"Locale": "System.String",
"Json": "System.String",
"Json": "System.Text.Json.Nodes.JsonValue",
"KeyReferenceInput": "System.String",
"Set": "System.String",
"Time": "System.String",
Expand Down

0 comments on commit 0f164fc

Please sign in to comment.