Skip to content

Commit

Permalink
update hotchocolate example with latest schema changes
Browse files Browse the repository at this point in the history
See #166 for details.

Resolves #189
  • Loading branch information
dariuszkuc committed Feb 10, 2023
1 parent f3de5db commit 95e56c6
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 16 deletions.
4 changes: 2 additions & 2 deletions implementations/hotchocolate/Products.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HotChocolate.AspNetCore" Version="12.6.0" />
<PackageReference Include="HotChocolate.ApolloFederation" Version="12.6.0" />
<PackageReference Include="HotChocolate.AspNetCore" Version="13.0.0" />
<PackageReference Include="HotChocolate.ApolloFederation" Version="13.0.0" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions implementations/hotchocolate/Types/CaseStudy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Products;

public class CaseStudy
{

public CaseStudy(string caseNumber, string? description)
{
CaseNumber = caseNumber;
Description = description;
}

[ID]
public string CaseNumber { get; }
public string? Description { get; }

}
24 changes: 22 additions & 2 deletions implementations/hotchocolate/Types/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@ namespace Products;

public class Data
{
private static ProductDimension Dimension = new("small", 1, "kg");
private static User DefaultUser = new("support@apollographql.com", "Jane Smith");
private static ProductResearch FederationStudy = new(new CaseStudy("1234", "Federation Study"), null);
private static ProductResearch StudioStudy = new(new CaseStudy("1235", "Studio Study"), null);

public List<User> Users { get; } = new List<User> {
DefaultUser
};

public List<ProductResearch> ProductResearches { get; } = new List<ProductResearch>
{
FederationStudy,
StudioStudy
};

public List<Product> Products { get; } = new List<Product>
{
new("apollo-federation", "federation", "@apollo/federation", "OSS"),
new("apollo-studio", "studio", string.Empty, "platform")
new("apollo-federation", "federation", "@apollo/federation", new ProductVariation("OSS"), Dimension, DefaultUser, null, new List<ProductResearch> { FederationStudy }),
new("apollo-studio", "studio", string.Empty, new ProductVariation("platform"), Dimension, DefaultUser, null, new List<ProductResearch> { StudioStudy })
};

public List<DeprecatedProduct> DeprecatedProducts { get; } = new List<DeprecatedProduct>
{
new ("apollo-federation-v1", "@apollo/federation-v1", "Migrate to Federation V2", DefaultUser)
};
}
32 changes: 32 additions & 0 deletions implementations/hotchocolate/Types/DeprecatedProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using HotChocolate.ApolloFederation;

namespace Products;

[Key("sku package")]
public class DeprecatedProduct
{
public DeprecatedProduct(string sku, string package, string? reason, User? createdBy)
{
Sku = sku;
Package = package;
Reason = reason;
CreatedBy = createdBy;
}

public string Sku { get; }

public string Package { get; }

public string? Reason { get; }

public User? CreatedBy { get; }

[ReferenceResolver]
public static DeprecatedProduct? GetProductByPackage(
string sku,
string package,
Data repository)
=> repository.DeprecatedProducts.FirstOrDefault(
t => t.Sku.Equals(sku) &&
t.Package.Equals(package));
}
16 changes: 12 additions & 4 deletions implementations/hotchocolate/Types/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ namespace Products;
[Key("sku variation { id }")]
public class Product
{
public Product(string id, string sku, string package, string variation)
public Product(string id, string? sku, string? package, ProductVariation? variation, ProductDimension? dimensions, User? createdBy, string? notes, List<ProductResearch> research)
{
Id = id;
Sku = sku;
Package = package;
Variation = new(variation);
Variation = variation;
Dimensions = dimensions;
CreatedBy = createdBy;
Notes = notes;
Research = research;
}

[ID]
Expand All @@ -24,10 +28,14 @@ public Product(string id, string sku, string package, string variation)

public ProductVariation? Variation { get; }

public ProductDimension? Dimensions { get; } = new("small", 1);
public ProductDimension? Dimensions { get; }

[Provides("totalProductsCreated")]
public User? CreatedBy { get; } = new("support@apollographql.com", 1337);
public User? CreatedBy { get; }

public string? Notes { get; }

public List<ProductResearch> Research { get; }

[ReferenceResolver]
public static Product? GetProductById(
Expand Down
5 changes: 4 additions & 1 deletion implementations/hotchocolate/Types/ProductDimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ namespace Products;

public class ProductDimension
{
public ProductDimension(string size, double weight)
public ProductDimension(string size, double weight, string? unit)
{
Size = size;
Weight = weight;
Unit = unit;
}

public string? Size { get; }

public double? Weight { get; }

public string? Unit { get; }
}
27 changes: 27 additions & 0 deletions implementations/hotchocolate/Types/ProductResearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using HotChocolate.ApolloFederation;

namespace Products;

[Key("study { caseNumber }")]
public class ProductResearch
{

public ProductResearch(CaseStudy study, string? outcome)
{
Study = study;
Outcome = outcome;
}

public CaseStudy Study { get; }
public string? Outcome { get; }

[ReferenceResolver]
public static ProductResearch? GetProductReasearchByCaseNumber(
[Map("study.caseNumber")] string caseNumber,
Data repository)
{
Console.WriteLine("case number = {0}", caseNumber);
return repository.ProductResearches.FirstOrDefault(
r => r.Study.CaseNumber.Equals(caseNumber));
}
}
4 changes: 4 additions & 0 deletions implementations/hotchocolate/Types/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public class Query
{
public Product? GetProduct([ID] string id, Data repository)
=> repository.Products.FirstOrDefault(t => t.Id.Equals(id));

[GraphQLDeprecated("Use product query instead")]
public DeprecatedProduct? GetDeprecatedProduct(string sku, string package, Data repository)
=> repository.DeprecatedProducts.FirstOrDefault(t => t.Sku.Equals(sku) && t.Package.Equals(package));
}
59 changes: 52 additions & 7 deletions implementations/hotchocolate/Types/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,65 @@ namespace Products;
[ExtendServiceType]
public class User
{
public User()
public User(string email, string? name)
{
Email = email;
Name = name;
}

public User(string email, int? totalProductsCreated)
[Requires("totalProductsCreated yearsOfEmployment")]
public int? GetAverageProductsCreatedPerYear()
{
Email = email;
TotalProductsCreated = totalProductsCreated;
if (TotalProductsCreated != null && LengthOfEmployment != null)
{
return Convert.ToInt32((TotalProductsCreated * 1.0) / LengthOfEmployment);
}
return null;
}

[ID]
[External]
public string Email { get; set; } = default!;
public string Email { get; set; }

public string? Name { get; }

[External]
public int? TotalProductsCreated { get; set; }
}
public int? TotalProductsCreated { get; set; } = 1337;

[GraphQLIgnore]
public int? LengthOfEmployment { get; set; }

[External]
public int GetYearsOfEmployment()
{
if (LengthOfEmployment == null)
{
throw new InvalidOperationException("yearsOfEmployment should never be null - it should be populated by _entities query");
}

return (int)LengthOfEmployment;
}

[ReferenceResolver]
public static User? GetUserByEmail(
string email,
int? totalProductsCreated,
int? yearsOfEmployment,
Data repository)
{
User? user = repository.Users.FirstOrDefault(u => u.Email.Equals(email));
if (user != null)
{
if (totalProductsCreated != null)
{
user.TotalProductsCreated = totalProductsCreated;
}

if (yearsOfEmployment != null)
{
user.LengthOfEmployment = yearsOfEmployment;
}
}
return user;
}
}

0 comments on commit 95e56c6

Please sign in to comment.