forked from ChilliCream/hotchocolate-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c639f1b
commit 4ba9e31
Showing
31 changed files
with
814 additions
and
0 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,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) | ||
} | ||
}; | ||
} | ||
} |
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,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>>(); | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace Demo.Contracts | ||
{ | ||
public interface IContract | ||
{ | ||
string Id { get; } | ||
|
||
string CustomerId { get; } | ||
} | ||
} |
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,12 @@ | ||
namespace Demo.Contracts | ||
{ | ||
public class LifeInsuranceContract | ||
: IContract | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string CustomerId { get; set; } | ||
|
||
public double Premium { get; set; } | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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,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>(); | ||
} | ||
} |
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,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)); | ||
} | ||
} | ||
} |
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,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>>>(); | ||
} | ||
} | ||
} |
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,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; } | ||
} | ||
} |
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,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>>(); | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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,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> |
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,9 @@ | ||
namespace Demo.Customers | ||
{ | ||
public class Consultant | ||
: ICustomerOrConsultant | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
} |
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,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>>(); | ||
} | ||
} | ||
} |
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,10 @@ | ||
namespace Demo.Customers | ||
{ | ||
public class Customer | ||
: ICustomerOrConsultant | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string ConsultantId { get; set; } | ||
} | ||
} |
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,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>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.