Skip to content

Commit

Permalink
Merge pull request #79 from planetarium/feat/implement-inventory-grap…
Browse files Browse the repository at this point in the history
…hql-query

feat: implement "inventory" GraphQL query
  • Loading branch information
boscohyun authored May 27, 2024
2 parents f643efa + 56a8d23 commit e8dae2a
Show file tree
Hide file tree
Showing 20 changed files with 667 additions and 119 deletions.
21 changes: 21 additions & 0 deletions Lib9c.GraphQL/Objects/InventoryObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Lib9c.GraphQL.Objects;

public class InventoryObject
{
public ItemObject[] Consumables { get; set; }
public ItemObject[] Costumes { get; set; }
public ItemObject[] Equipments { get; set; }
public ItemObject[] Materials { get; set; }

public InventoryObject(
ItemObject[] consumables,
ItemObject[] costumes,
ItemObject[] equipments,
ItemObject[] materials)
{
Consumables = consumables;
Costumes = costumes;
Equipments = equipments;
Materials = materials;
}
}
94 changes: 94 additions & 0 deletions Lib9c.GraphQL/Objects/ItemObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Security.Cryptography;
using Libplanet.Common;
using Nekoyume.Model.Elemental;
using Nekoyume.Model.Item;

namespace Lib9c.GraphQL.Objects;

public class ItemObject
{
public int ItemSheetId { get; set; }
public int Grade { get; set; }
public ItemType ItemType { get; set; }
public ItemSubType ItemSubType { get; set; }
public ElementalType ElementalType { get; set; }

public int Count { get; set; }

/// <summary>
/// Level of the Equipment.
/// </summary>
public int? Level { get; set; }

public long? RequiredBlockIndex { get; set; }

/// <summary>
/// ID of the IFungibleItem.
/// </summary>
public HashDigest<SHA256>? FungibleId { get; set; }

/// <summary>
/// ID of the INonFungibleItem.
/// </summary>
public Guid? NonFungibleId { get; set; }

/// <summary>
/// ID of the TradableItem.
/// </summary>
public Guid? TradableId { get; set; }

public ItemObject()
{
}

public ItemObject(ItemBase itemBase, int count)
{
ItemSheetId = itemBase.Id;
Grade = itemBase.Grade;
ItemType = itemBase.ItemType;
ItemSubType = itemBase.ItemSubType;
ElementalType = itemBase.ElementalType;
Count = count;
}

public ItemObject(Consumable consumable, int count) : this((ItemBase)consumable, count)
{
Level = null;
RequiredBlockIndex = consumable.RequiredBlockIndex;
FungibleId = null;
NonFungibleId = consumable.NonFungibleId;
TradableId = consumable.TradableId;
}

public ItemObject(Costume costume, int count) : this((ItemBase)costume, count)
{
Level = null;
RequiredBlockIndex = costume.RequiredBlockIndex;
FungibleId = null;
NonFungibleId = costume.NonFungibleId;
TradableId = costume.TradableId;
}

public ItemObject(Equipment equipment, int count) : this((ItemBase)equipment, count)
{
var tradableItem = equipment as ITradableItem;

Level = equipment.level;
RequiredBlockIndex = equipment.RequiredBlockIndex;
FungibleId = null;
NonFungibleId = equipment.NonFungibleId;
TradableId = tradableItem?.TradableId;
}

public ItemObject(Material material, int count) : this((ItemBase)material, count)
{
var tradableItem = material as ITradableItem;

Level = null;
RequiredBlockIndex = tradableItem?.RequiredBlockIndex;
FungibleId = material.FungibleId;
NonFungibleId = null;
TradableId = tradableItem?.TradableId;
}
}
58 changes: 58 additions & 0 deletions Lib9c.GraphQL/Types/AddressType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;
using Libplanet.Crypto;

namespace Lib9c.GraphQL.Types;

public class AddressType : ScalarType<Address, StringValueNode>
{
public AddressType() : base("Address")
{
}

public override IValueNode ParseResult(object? resultValue)
{
return resultValue switch
{
Address address => ParseValue(address),
string s => ParseValue(s),
_ => throw new SerializationException(
ErrorBuilder.New()
.SetMessage("Invalid runtime type. Expected: Address or string.")
.SetCode(ErrorCodes.Scalars.InvalidRuntimeType)
.Build(),
this)
};
}

protected override Address ParseLiteral(StringValueNode valueSyntax) =>
new(valueSyntax.Value);

protected override StringValueNode ParseValue(Address runtimeValue) =>
new(runtimeValue.ToString());

public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
if (runtimeValue is Address address)
{
resultValue = address.ToString();
return true;
}

resultValue = null;
return false;
}

public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
if (resultValue is string s)
{
runtimeValue = ParseValue(s);
return true;
}

runtimeValue = null;
return false;
}
}
8 changes: 8 additions & 0 deletions Lib9c.GraphQL/Types/ElementalTypeEnumType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using HotChocolate.Types;
using Nekoyume.Model.Elemental;

namespace Lib9c.GraphQL.Types;

public class ElementalTypeEnumType : EnumType<ElementalType>
{
}
59 changes: 59 additions & 0 deletions Lib9c.GraphQL/Types/GuidType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Globalization;
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;

namespace Lib9c.GraphQL.Types;

public class GuidType : ScalarType<Guid, StringValueNode>
{
public GuidType() : base("Guid")
{
}

public override IValueNode ParseResult(object? resultValue)
{
return resultValue switch
{
Guid guid => ParseValue(guid),
string s => ParseValue(s),
_ => throw new SerializationException(
ErrorBuilder.New()
.SetMessage("Invalid runtime type. Expected: Guid or string.")
.SetCode(ErrorCodes.Scalars.InvalidRuntimeType)
.Build(),
this)
};
}

protected override Guid ParseLiteral(StringValueNode valueSyntax) =>
Guid.Parse(valueSyntax.Value);

protected override StringValueNode ParseValue(Guid runtimeValue) =>
new(runtimeValue.ToString("D", CultureInfo.InvariantCulture));

public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
if (runtimeValue is Guid guid)
{
resultValue = guid.ToString("D", CultureInfo.InvariantCulture);
return true;
}

resultValue = null;
return false;
}

public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
if (resultValue is string s)
{
runtimeValue = ParseValue(s);
return true;
}

runtimeValue = null;
return false;
}
}
59 changes: 59 additions & 0 deletions Lib9c.GraphQL/Types/HashDigestSHA256Type.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Security.Cryptography;
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;
using Libplanet.Common;

namespace Lib9c.GraphQL.Types;

public class HashDigestSHA256Type : ScalarType<HashDigest<SHA256>, StringValueNode>
{
public HashDigestSHA256Type() : base("HashDigestSHA256")
{
}

public override IValueNode ParseResult(object? resultValue)
{
return resultValue switch
{
HashDigest<SHA256> hashDigest => ParseValue(hashDigest),
string s => ParseValue(s),
_ => throw new SerializationException(
ErrorBuilder.New()
.SetMessage("Invalid runtime type. Expected: HashDigest<SHA256> or string.")
.SetCode(ErrorCodes.Scalars.InvalidRuntimeType)
.Build(),
this)
};
}

protected override HashDigest<SHA256> ParseLiteral(StringValueNode valueSyntax) =>
HashDigest<SHA256>.FromString(valueSyntax.Value);

protected override StringValueNode ParseValue(HashDigest<SHA256> runtimeValue) =>
new(runtimeValue.ToString());

public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
if (runtimeValue is HashDigest<SHA256> hd)
{
resultValue = hd.ToString();
return true;
}

resultValue = null;
return false;
}

public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
if (resultValue is string s)
{
runtimeValue = ParseValue(s);
return true;
}

runtimeValue = null;
return false;
}
}
31 changes: 31 additions & 0 deletions Lib9c.GraphQL/Types/InventoryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using HotChocolate.Types;
using Lib9c.GraphQL.Objects;

namespace Lib9c.GraphQL.Types;

public class InventoryType : ObjectType<InventoryObject>
{
protected override void Configure(IObjectTypeDescriptor<InventoryObject> descriptor)
{
descriptor
.Field(f => f.Consumables)
.Name("consumables")
.Description("The consumables in the inventory.")
.Type<NonNullType<ListType<NonNullType<ItemType>>>>();
descriptor
.Field(f => f.Costumes)
.Name("costumes")
.Description("The costumes in the inventory.")
.Type<NonNullType<ListType<NonNullType<ItemType>>>>();
descriptor
.Field(f => f.Equipments)
.Name("equipments")
.Description("The equipments in the inventory.")
.Type<NonNullType<ListType<NonNullType<ItemType>>>>();
descriptor
.Field(f => f.Materials)
.Name("materials")
.Description("The materials in the inventory.")
.Type<NonNullType<ListType<NonNullType<ItemType>>>>();
}
}
8 changes: 8 additions & 0 deletions Lib9c.GraphQL/Types/ItemSubTypeEnumType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using HotChocolate.Types;
using Nekoyume.Model.Item;

namespace Lib9c.GraphQL.Types;

public class ItemSubTypeEnumType : EnumType<ItemSubType>
{
}
Loading

0 comments on commit e8dae2a

Please sign in to comment.