Building a .NET GraphQL Server

Albert Starreveld
6 min readNov 10, 2022

GraphQL is a query language developed by Facebook in 2012 (publicly released in 2015). In a GraphQL query, the consumer of an API specifies the structure of the data it needs. The server executes the query, collects the data from one or various data sources, filters out the fields which aren’t specified in the query, and returns the result as JSON objects.

What problem does GraphQL solve?

Imagine this Rest API. It has two endpoints:

[GET] /roomreturns: [
{
roomId: 1,
locationId: 1,
name: 'Ideation Zone',
capacity: 30
},
{
roomId: 2,
locationId: 1,
name: 'Creative Arena',
capacity: 60
}
]

And

[GET] /presentationreturns[
{
presentationId: 1,
presentorIds: [5, 24],
roomId: 1,
start: '2022-10-02T13:00:00Z',
end: '2022-10-02T15:00:00Z',
title: 'GraphQL for newbies',
description: 'Lorum ipsum...'
},
{
presentationId: 2,
presentorIds: [6],
roomId: 2,
start: '2022-10-02T14:00:00Z',
end: '2022-10-02T15:00:00Z',
title: 'Advanced Rest',
description: 'Dolar sit amet..'
}
]

Usually, the product owner asks the team to build the following table:

| Presentation        | Starts at        | Room           |
+---------------------+------------------+----------------+
| GraphQL for newbies | 2022-10-02 14:00 | Ideation Zone |
| Advanced Rest | 2022-10-02 15:00 | Creative Arena |

This is unfortunate because:

  • The API returns (much) more data than strictly necessary to render the table.
  • Rendering this table requires a lot of API calls. First, the /presentation endpoint must be queried. Then, the code will have to iterate through the results and query the /room/x API to collect the name of the room.

Instead of querying endpoints, iterating through the results, query some more, and aggregating the results, you want to tell the API to:

  • Get a list of presentations
  • Include the name, start, and room name fields in the response only

That is what you can do with a GraphQL query. When executing a GraphQL query, the GraphQL Server will collect the room- and the presentation entities…

Albert Starreveld

Passionate about cloud native software development. Only by sharing knowledge and code we can take software development to the next level!

Recommended from Medium

Lists

See more recommendations