PM> Install-Package GraphQL - Version 2.4 .0
PM> Install-Package GraphQl.AspNetCore - Version 1.1 .4
PM> Install-Package GraphQL.AspNetCore.Graphiql - Version 1.1 .4
public void ConfigureServices ( IServiceCollection services )
{
// ..
services . AddTransient < AuthorType > ( ) ;
services . AddTransient < BookType > ( ) ;
services . AddTransient < PublisherType > ( ) ;
services . AddTransient < AuthorRootType > ( ) ;
services . AddTransient < BookRootType > ( ) ;
services . AddTransient < PublisherRootType > ( ) ;
services . AddTransient < RootQuery > ( ) ;
services . AddGraphQl ( schema =>
{
schema . SetQueryType < RootQuery > ( ) ;
} ) ;
// ..
}
public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
{
// ..
app . UseGraphQl (
path : "/graphql" ,
configure : options =>
{
options . ComplexityConfiguration = new ComplexityConfiguration { MaxDepth = 15 } ;
options . ExposeExceptions = true ;
options . FormatOutput = false ;
} ) ;
app . UseGraphiql (
path : "/graphiql" ,
configure : options =>
{
options . GraphQlEndpoint = "/graphql" ;
} ) ;
// ..
}
[
{
"id" : 1 ,
"name" : " .."
}
]
[POST ] /graphql
{
authors {
id
name
}
}
{
"data" : {
"authors" : [
{
"id" : 1 ,
"name" : " .."
}
]
}
}
{
"id" : 1 ,
"name" : " .."
}
[POST ] /graphql
{
authorById (id : 1 ) {
id
name
}
}
{
"data" : {
"authorById" : {
"id" : 1 ,
"name" : " .."
}
}
}
[
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
[POST ] /graphql
{
books {
isbn
title
subTitle
description
pages
publishedAt
author {
id
name
}
publisher {
id
name
}
}
}
{
"data" : {
"booksByPublisherId" : [
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
}
}
[GET ] /api /v1 /books /1234567890123
{
"isbn" : " 1234567890123" ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
[POST ] /graphql
{
bookByIsbn (isbn : " 1234567890123" ) {
isbn
title
subTitle
description
pages
publishedAt
author {
id
name
}
publisher {
id
name
}
}
}
{
"data" : {
"bookByIsbn" : {
"isbn" : " 1234567890123" ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " ,," ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
}
}
[GET ] /api /v1 /authors /1/books
[
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
[POST ] /graphql
{
booksByAuthorId (id : 1 ) {
isbn
title
subTitle
description
pages
publishedAt
author {
id
name
}
publisher {
id
name
}
}
}
{
"data" : {
"booksByAuthorId" : [
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
}
}
Get books by publisher ID
[GET ] /api /v1 /publishers /1/books
[
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
[POST ] /graphql
{
booksByPublisherId (id : 1 ) {
isbn
title
subTitle
description
pages
publishedAt
author {
id
name
}
publisher {
id
name
}
}
}
{
"data" : {
"booksByPublisherId" : [
{
"isbn" : " .." ,
"title" : " .." ,
"subTitle" : " .." ,
"description" : " .." ,
"pages" : 1 ,
"publishedAt" : " .." ,
"author" : {
"id" : 1 ,
"name" : " .."
},
"publisher" : {
"id" : 1 ,
"name" : " .."
}
}
]
}
}
[
{
"id" : 1 ,
"name" : " .."
}
]
[POST ] /graphql
{
publishers {
id
name
}
}
{
"data" : {
"publishers" : [
{
"id" : 1 ,
"name" : " .."
}
]
}
}
[GET ] /api /v1 /publishers /1
{
"id" : 1 ,
"name" : " .."
}
[POST ] /graphql
{
publisherById (id : 1 ) {
id
name
}
}
{
"data" : {
"publisherById" : {
"id" : 1 ,
"name" : " .."
}
}
}