-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from planetarium/feat/implement-inventory-grap…
…hql-query feat: implement "inventory" GraphQL query
- Loading branch information
Showing
20 changed files
with
667 additions
and
119 deletions.
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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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 @@ | ||
using HotChocolate.Types; | ||
using Nekoyume.Model.Elemental; | ||
|
||
namespace Lib9c.GraphQL.Types; | ||
|
||
public class ElementalTypeEnumType : EnumType<ElementalType> | ||
{ | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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>>>>(); | ||
} | ||
} |
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 @@ | ||
using HotChocolate.Types; | ||
using Nekoyume.Model.Item; | ||
|
||
namespace Lib9c.GraphQL.Types; | ||
|
||
public class ItemSubTypeEnumType : EnumType<ItemSubType> | ||
{ | ||
} |
Oops, something went wrong.