Skip to content

Instantly share code, notes, and snippets.

View ngohungphuc's full-sized avatar
🏡
Sài Gòn & Lelystad

Tony Ngo ngohungphuc

🏡
Sài Gòn & Lelystad
View GitHub Profile
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services
.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
public class DataSchema : Schema
{
public DataSchema(IDependencyResolver resolver) : base(resolver)
{
Query = resolver.Resolve<Query>();
}
}
public class Query : ObjectGraphType
{
public Query(Repository repository)
{
Field<ListGraphType<PostType>>("posts",
arguments: new QueryArguments(new List<QueryArgument>
{
new QueryArgument<IdGraphType>
{
Name = "id"
Field<ListGraphType<CommentType>, IEnumerable<Comment>>().Name("Comment")
.ResolveAsync(ctx =>
{
return repository.GetCommentByPostId(ctx.Source.Id);
});
public class PostType : ObjectGraphType<Post>
{
public PostType(Repository repository)
{
Field(x => x.Id);
Field(x => x.Title);
Field(x => x.Body);
Field(x => x.UserId);
Field<ListGraphType<CommentType>, IEnumerable<Comment>>().Name("Comment")
.ResolveAsync(ctx =>
<PackageReference Include="GraphQL" Version="2.4.0" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.4.0" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.4.0" />
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
);
services
.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
public class Repository
{
private readonly AppDbContext _appDbContext;
public Repository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public IQueryable<Post> GetPostQuery()
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
);
services
.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
public class Post
{
public int UserId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public ICollection<Comment> Comments { get; set; }
}