Skip to content

.NET - GraphQL

License

Notifications You must be signed in to change notification settings

dimitrietataru/netcore-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

.NET - GraphQL

Install

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

Configure

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";
        });
    
    // ..
}

Usage

Get all authors

REST

[GET] /api/v1/authors
[
  {
    "id": 1,
    "name": ".."
  }
]

GraphQL

[POST] /graphql

{
  authors {
    id
    name
  }
}
{
  "data": {
    "authors": [
      {
        "id": 1,
        "name": ".."
      }
    ]
  }
}

Get author by ID

REST

[GET] /api/v1/authors/1
{
  "id": 1,
  "name": ".."
}

GraphQL

[POST] /graphql

{
  authorById(id: 1) {
    id
    name
  }
}
{
  "data": {
    "authorById": {
      "id": 1,
      "name": ".."
    }
  }
}

Get all books

REST

[GET] /api/v1/books
[
  {
    "isbn": "..",
    "title": "..",
    "subTitle": "..",
    "description": "..",
    "pages": 1,
    "publishedAt": "..",
    "author": {
      "id": 1,
      "name": ".."
    },
    "publisher": {
      "id": 1,
      "name": ".."
    }
  }
]

GraphQL

[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 book by ISBN

REST

[GET] /api/v1/books/1234567890123
{
  "isbn": "1234567890123",
  "title": "..",
  "subTitle": "..",
  "description": "..",
  "pages": 1,
  "publishedAt": "..",
  "author": {
    "id": 1,
    "name": ".."
  },
  "publisher": {
    "id": 1,
    "name": ".."
  }
}

GraphQL

[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 books by author ID

REST

[GET] /api/v1/authors/1/books
[
  {
    "isbn": "..",
    "title": "..",
    "subTitle": "..",
    "description": "..",
    "pages": 1,
    "publishedAt": "..",
    "author": {
      "id": 1,
      "name": ".."
    },
    "publisher": {
      "id": 1,
      "name": ".."
    }
  }
]

GraphQL

[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

REST

[GET] /api/v1/publishers/1/books
[
  {
    "isbn": "..",
    "title": "..",
    "subTitle": "..",
    "description": "..",
    "pages": 1,
    "publishedAt": "..",
    "author": {
      "id": 1,
      "name": ".."
    },
    "publisher": {
      "id": 1,
      "name": ".."
    }
  }
]

GraphQL

[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": ".."
        }
      }
    ]
  }
}

Get all publishers

REST

[GET] /api/v1/publishers
[
  {
    "id": 1,
    "name": ".."
  }
]

GraphQL

[POST] /graphql

{
  publishers {
    id
    name
  }
}
{
  "data": {
    "publishers": [
      {
        "id": 1,
        "name": ".."
      }
    ]
  }
}

Get publisher by ID

REST

[GET] /api/v1/publishers/1
{
    "id": 1,
    "name": ".."
}

GraphQL

[POST] /graphql

{
  publisherById(id: 1) {
    id
    name
  }
}
{
  "data": {
    "publisherById": {
      "id": 1,
      "name": ".."
    }
  }
}

Credits