Skip to content

Commit

Permalink
Added stitching example
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jan 24, 2019
1 parent c639f1b commit 4ba9e31
Show file tree
Hide file tree
Showing 31 changed files with 814 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Stitching/ContractSchema/ContractStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Demo.Contracts
{
public class ContractStorage
{
public List<IContract> Contracts { get; } = new List<IContract>
{
new LifeInsuranceContract
{
Id = "1",
CustomerId= "1",
Premium = 123456.11
},
new LifeInsuranceContract
{
Id = "2",
CustomerId= "1",
Premium = 456789.12
},
new LifeInsuranceContract
{
Id = "3",
CustomerId = "2",
Premium = 789.12
},
new SomeOtherContract
{
Id = "1",
CustomerId= "1",
ExpiryDate = new DateTime(2015, 2, 1, 0,0,0, DateTimeKind.Utc)
},
new SomeOtherContract
{
Id = "2",
CustomerId= "2",
ExpiryDate = new DateTime(2015, 5, 1, 0,0,0, DateTimeKind.Utc)
},
new SomeOtherContract
{
Id = "3",
CustomerId= "3",
ExpiryDate = new DateTime(2017, 1, 30, 0,0,0, DateTimeKind.Utc)
},
new SomeOtherContract
{
Id = "4",
CustomerId= "3",
ExpiryDate = new DateTime(2020, 1, 1, 0,0,0, DateTimeKind.Utc)
}
};
}
}
14 changes: 14 additions & 0 deletions Stitching/ContractSchema/ContractType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HotChocolate.Types;

namespace Demo.Contracts
{
public class ContractType
: InterfaceType
{
protected override void Configure(IInterfaceTypeDescriptor descriptor)
{
descriptor.Name("Contract");
descriptor.Field("id").Type<NonNullType<IdType>>();
}
}
}
9 changes: 9 additions & 0 deletions Stitching/ContractSchema/IContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Demo.Contracts
{
public interface IContract
{
string Id { get; }

string CustomerId { get; }
}
}
12 changes: 12 additions & 0 deletions Stitching/ContractSchema/LifeInsuranceContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Demo.Contracts
{
public class LifeInsuranceContract
: IContract
{
public string Id { get; set; }

public string CustomerId { get; set; }

public double Premium { get; set; }
}
}
16 changes: 16 additions & 0 deletions Stitching/ContractSchema/LifeInsuranceContractType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HotChocolate.Types;

namespace Demo.Contracts
{
public class LifeInsuranceContractType
: ObjectType<LifeInsuranceContract>
{
protected override void Configure(
IObjectTypeDescriptor<LifeInsuranceContract> descriptor)
{
descriptor.Interface<ContractType>();
descriptor.Field(t => t.Id).Type<NonNullType<IdType>>();
descriptor.Field(t => t.CustomerId).Ignore();
}
}
}
25 changes: 25 additions & 0 deletions Stitching/ContractSchema/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Demo.Contracts
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5051")
.UseStartup<Startup>();
}
}
45 changes: 45 additions & 0 deletions Stitching/ContractSchema/Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Types.Relay;

namespace Demo.Contracts
{
public class Query
{
private readonly IdSerializer _idSerializer = new IdSerializer();
private readonly ContractStorage _contractStorage;

public Query(ContractStorage contractStorage)
{
_contractStorage = contractStorage
?? throw new ArgumentNullException(nameof(contractStorage));
}

public IContract GetContract(string contractId)
{
IdValue value = _idSerializer.Deserialize(contractId);

if (value.TypeName == nameof(LifeInsuranceContract))
{
return _contractStorage.Contracts
.OfType<LifeInsuranceContract>()
.FirstOrDefault(t => t.Id.Equals(value.Value));
}
else
{
return _contractStorage.Contracts
.OfType<SomeOtherContract>()
.FirstOrDefault(t => t.Id.Equals(value.Value));
}
}

public IEnumerable<IContract> GetContracts(string customerId)
{
IdValue value = _idSerializer.Deserialize(customerId);

return _contractStorage.Contracts
.Where(t => t.CustomerId.Equals(value.Value));
}
}
}
20 changes: 20 additions & 0 deletions Stitching/ContractSchema/QueryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HotChocolate.Types;

namespace Demo.Contracts
{
public class QueryType
: ObjectType<Query>
{
protected override void Configure(
IObjectTypeDescriptor<Query> descriptor)
{
descriptor.Field(t => t.GetContract(default))
.Argument("contractId", a => a.Type<NonNullType<IdType>>())
.Type<ContractType>();

descriptor.Field(t => t.GetContracts(default))
.Argument("customerId", a => a.Type<NonNullType<IdType>>())
.Type<ListType<NonNullType<ContractType>>>();
}
}
}
14 changes: 14 additions & 0 deletions Stitching/ContractSchema/SomeOtherContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Demo.Contracts
{
public class SomeOtherContract
: IContract
{
public string Id { get; set; }

public string CustomerId { get; set; }

public DateTime ExpiryDate { get; set; }
}
}
23 changes: 23 additions & 0 deletions Stitching/ContractSchema/SomeOtherContractType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HotChocolate.Types;

namespace Demo.Contracts
{
public class SomeOtherContractType
: ObjectType<SomeOtherContract>
{
protected override void Configure(
IObjectTypeDescriptor<SomeOtherContract> descriptor)
{
descriptor.Interface<ContractType>();

descriptor.Field(t => t.Id)
.Type<NonNullType<IdType>>();

descriptor.Field(t => t.CustomerId)
.Ignore();

descriptor.Field(t => t.ExpiryDate)
.Type<NonNullType<DateTimeType>>();
}
}
}
39 changes: 39 additions & 0 deletions Stitching/ContractSchema/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using HotChocolate;
using HotChocolate.AspNetCore;
using Demo.Contracts;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Demo.Contracts
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ContractStorage>();

// Add GraphQL Services
services.AddGraphQL(Schema.Create(c =>
{
c.RegisterQueryType<QueryType>();
c.RegisterType<LifeInsuranceContractType>();
c.RegisterType<SomeOtherContractType>();

c.UseGlobalObjectIdentifier();
}));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseGraphQL();
app.UsePlayground();
}
}
}
27 changes: 27 additions & 0 deletions Stitching/ContractSchema/contract.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.2</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="HotChocolate.AspNetCore" Version="0.7.0-preview.35" />
<PackageReference Include="HotChocolate.AspNetCore.GraphiQL" Version="0.7.0-preview.35" />
<PackageReference Include="HotChocolate.AspNetCore.Playground" Version="0.7.0-preview.35" />
<PackageReference Include="HotChocolate.AspNetCore.Authorization" Version="0.7.0-preview.35" />
<PackageReference Include="HotChocolate.Subscriptions.InMemory" Version="0.7.0-preview.35" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions Stitching/CustomerSchema/Consultant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Demo.Customers
{
public class Consultant
: ICustomerOrConsultant
{
public string Id { get; set; }
public string Name { get; set; }
}
}
15 changes: 15 additions & 0 deletions Stitching/CustomerSchema/ConsultantType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HotChocolate.Types;

namespace Demo.Customers
{
public class ConsultantType
: ObjectType<Consultant>
{
protected override void Configure(
IObjectTypeDescriptor<Consultant> descriptor)
{
descriptor.Field(t => t.Id).Type<NonNullType<IdType>>();
descriptor.Field(t => t.Name).Type<NonNullType<StringType>>();
}
}
}
10 changes: 10 additions & 0 deletions Stitching/CustomerSchema/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Demo.Customers
{
public class Customer
: ICustomerOrConsultant
{
public string Id { get; set; }
public string Name { get; set; }
public string ConsultantId { get; set; }
}
}
15 changes: 15 additions & 0 deletions Stitching/CustomerSchema/CustomerOrConsultantType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HotChocolate.Types;

namespace Demo.Customers
{
public class CustomerOrConsultantType
: UnionType
{
protected override void Configure(IUnionTypeDescriptor descriptor)
{
descriptor.Name("CustomerOrConsultant");
descriptor.Type<CustomerType>();
descriptor.Type<ConsultantType>();
}
}
}
Loading

0 comments on commit 4ba9e31

Please sign in to comment.