REST, OData, GraphQL, SignalR

The Problem with RESTful APIs

Practical RESTful APIs

  • Design APIs around business entities
    • Customers, Orders, Products, etc.
  • Uses many concepts of HTTP
    • URL for addressing and joining, query parameters, headers for authentication, methods for operations, etc.
  • Schema and documentation using Open API Specification
https://api.acme.com/customers
https://api.acme.com/customers/ALFKI
https://api.acme.com/customers/ALFKI/orders
https://api.acme.com/customers/ALFKI/orders/4711
https://api.acme.com/orders?skip=100&take=10
...

Problem: Overfetching, Underfetching

  • Overfetching
    • https://api.acme.com/customers/ALFKIΒ returns an entire customer
    • What if the client only needs specific fields?
  • Underfetching
    • https://api.acme.com/ordersΒ returns order headers
    • What if the client needs order details for all orders (N+1 problem)
  • Logic on the client
    • https://api.acme.com/customers returns customers ordered by ID
    • What if the client needs data ordered by customer name?

Solutions

  • Add more endpoints
    • https://api.acme.com/customers/orderedByName
    • πŸ‘ simple to implement for only a few options
    • πŸ‘Ž confusing for larger APIs, a lot of work, inflexible
  • Use query parameters
    • https://api.acme.com/customers?orderBy=name
    • πŸ‘ flexible, arbitrarily powerful
    • πŸ‘Ž not standardized

OData

What is OData?

Pros/Cons

  • Solves over/underfetching problem
    • Gives client a lot of flexibility
  • Standardized
    • OASIS standard
    • Widely used in Microsoft products (clients, services)
    • Covered in Microsoft's own API Guidelines πŸ”—
    • No so widely used outside the MS ecosystem
  • Good integration with .NET
    • Largely delivered by Microsoft
  • Well-suited for use with (relational) databases
    • OData queries can be relatively easy translated
      into DB queries (e.g. SQL)
  • Not the newest and freshest protocol

.NET and OData

GraphQL

What is GraphQL?

What is GraphQL?

Pros/Cons

  • Solves/moves over/underfetching problem
    • Gives client a lot of flexibility
  • Popular
    • Widely used in modern distributed systems πŸ”—
    • Support in many popular tools (e.g. PostmanΒ πŸ”—)
  • No .NET support provided by Microsoft, but from 3rd parties
  • Not so well-suited for use with (relational) databases

.NET and GraphQL

  • Two popular GraphQL server implementations
  • Decent integration with Entity Framework Core
    • πŸ‘ Integration with EFCore πŸ”—, support for IQueryable πŸ”—
    • πŸ‘Ž GraphQL not designed primarily with DBs in mind
  • Implementation of custom GraphQL providers is possible
    • πŸ‘ Flexibility
    • πŸ‘Ž A lot of work to do it right

SignalR

What is SignalR?

SignalR without SignalR πŸ˜•

.NET and SignalR

Other Options

Invent Your Own QL

  • Invent your own query language
    • Based on existing grammar (e.g. JSON, YAML)
    • Custom grammar (DSL)
  • Frequently done in COTS SaaS applications, examples:
  • Pros/Cons
    • πŸ‘ Flexibility, can be tailored to specific needs
    • πŸ‘Ž Complexity, a lot of (interesting) work (done it before)

gRPC

The Ideal Option

Data Access Layer

Business Logic Layer

OData

GraphQL

gRPC

...

Q&A

REST, OData, GraphQL, SignalR, gRPC

By Rainer Stropek

REST, OData, GraphQL, SignalR, gRPC

  • 304