Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate "ExportDirective with type argument fails to prepend @ to directive name" #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions blog/2020/2020-03-18-entity-framework/ContosoUni/ContosoUni.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
<PackageReference Include="HotChocolate.AspNetCore" Version="10.4.0-rc.1" />
<PackageReference Include="HotChocolate.Types.Filters" Version="10.4.0-rc.1" />
<PackageReference Include="HotChocolate.Types.Sorting" Version="10.4.0-rc.1" />
<PackageReference Include="HotChocolate.Types.Selections" Version="10.4.0-rc.1" />
<PackageReference Include="HotChocolate.AspNetCore.Playground" Version="10.4.0-rc.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="HotChocolate" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.AspNetCore" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Execution" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Types.Analyzers" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Data" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Diagnostics" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Types" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.Types.CursorPagination" Version="14.2.0-p.7" />
<PackageReference Include="HotChocolate.ApolloFederation" Version="14.2.0-p.7" />
</ItemGroup>

</Project>
15 changes: 6 additions & 9 deletions blog/2020/2020-03-18-entity-framework/ContosoUni/Query.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using HotChocolate;
using HotChocolate.Data;
using HotChocolate.Types;
using HotChocolate.Types.Relay;

Expand All @@ -8,21 +9,17 @@ namespace ContosoUniversity
public class Query
{
[UseFirstOrDefault]
[UseSelection]
public IQueryable<Student> GetStudentById([Service]SchoolContext context, int studentId) =>
public IQueryable<Student> GetStudentById([Service] SchoolContext context, int studentId) =>
context.Students.Where(t => t.Id == studentId);

[UseSelection]
[UsePaging]
[UseFiltering]
[UseSorting]
public IQueryable<Student> GetStudents([Service]SchoolContext context) =>
context.Students;
public IQueryable<Student> GetStudents([Service] SchoolContext context) => context.Students;

[UsePaging]
[UseSelection]
[UseFiltering]
[UseSorting]
public IQueryable<Course> GetCourses([Service]SchoolContext context) =>
context.Courses;
public IQueryable<Course> GetCourses([Service] SchoolContext context) => context.Courses;
}
}
}
99 changes: 68 additions & 31 deletions blog/2020/2020-03-18-entity-framework/ContosoUni/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@
using HotChocolate;
using HotChocolate.AspNetCore;
using HotChocolate.Execution.Configuration;

using HotChocolate.AspNetCore.Serialization;
using HotChocolate.ApolloFederation;
using HotChocolate.Types;
using HotChocolate.ApolloFederation.Types;
using HotChocolate.Execution;
using System.IO;

namespace ContosoUniversity
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
public async void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SchoolContext>();
services.AddHttpResponseFormatter<DefaultHttpResponseFormatter>();

var gqlService = services
.AddGraphQLServer()
.AddQueryType<Query>()
.AddFiltering()
.AddSorting()
.ModifyPagingOptions(opt => opt.IncludeTotalCount = true)
.AddApolloFederation(FederationVersion.Federation26)
.ExportDirective<OneOfDirectiveType>();

services.AddGraphQL(
SchemaBuilder.New()
.AddQueryType<Query>()
.Create(),
new QueryExecutionOptions { ForceSerialExecution = true });
var schema = await gqlService.BuildSchemaAsync();
await File.WriteAllTextAsync("./schema.graphql", schema.Print());
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -38,42 +50,67 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRouting();

app.UseGraphQL();
app.UsePlayground();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints
.MapGraphQL()
.WithOptions(new GraphQLServerOptions { Tool = { Enable = true } });
});
}

private static void InitializeDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
using (
var serviceScope = app.ApplicationServices
.GetService<IServiceScopeFactory>()
.CreateScope()
)
{
var context = serviceScope.ServiceProvider.GetRequiredService<SchoolContext>();
if (context.Database.EnsureCreated())
{
var course = new Course { Credits = 10, Title = "Object Oriented Programming 1" };

context.Enrollments.Add(new Enrollment
{
Course = course,
Student = new Student { FirstMidName = "Rafael", LastName = "Foo", EnrollmentDate = DateTime.UtcNow }
});
context.Enrollments.Add(new Enrollment
var course = new Course
{
Course = course,
Student = new Student { FirstMidName = "Pascal", LastName = "Bar", EnrollmentDate = DateTime.UtcNow }
});
context.Enrollments.Add(new Enrollment
{
Course = course,
Student = new Student { FirstMidName = "Michael", LastName = "Baz", EnrollmentDate = DateTime.UtcNow }
});
Credits = 10,
Title = "Object Oriented Programming 1"
};

context.Enrollments.Add(
new Enrollment
{
Course = course,
Student = new Student
{
FirstMidName = "Rafael",
LastName = "Foo",
EnrollmentDate = DateTime.UtcNow
}
}
);
context.Enrollments.Add(
new Enrollment
{
Course = course,
Student = new Student
{
FirstMidName = "Pascal",
LastName = "Bar",
EnrollmentDate = DateTime.UtcNow
}
}
);
context.Enrollments.Add(
new Enrollment
{
Course = course,
Student = new Student
{
FirstMidName = "Michael",
LastName = "Baz",
EnrollmentDate = DateTime.UtcNow
}
}
);
context.SaveChangesAsync();
}
}
Expand Down
4 changes: 2 additions & 2 deletions blog/2020/2020-03-18-entity-framework/ContosoUni/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using HotChocolate.Data;
using HotChocolate.Types;

namespace ContosoUniversity
Expand All @@ -15,8 +16,7 @@ public class Student
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }

[UseSelection]
[UseFiltering]
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
}
}
Loading