The project uses the library Hotchocolate as GraphQL server implementation. It is not the only .Net implementation of a GraphQL server, but its ease and power are remarkable and the performances are much better than the Microsoft version.
Let's begin to explore the scheme made.
To do this it is possible to begin to discover some of the tools of the GraphQL ecosystem, eg. Banana Cake Pop, which is incorporated in the HotChocolate library and made available at startup with the line:
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL();
});
which allows you to access Banana Cake Pop via the /graphql
route. BCP allows you to query our graphQL server.
"QL" in GraphQL obviously stands for Query Language and this is another relevant feature of this technology, in fact it allows you to manage in a declarative way even complex logics such as filters, variables or code reuse through fragment
.
In the following query you can see how to extend our types by allowing our query methods to handle arguments
query editori {
publishers(skip:0, limit: 10) {
id
name
}
}
query autori {
authors(limit: 5) {
id
firstName
surnName
}
}
query autore {
a1: authorById(id: "2d2cbbee-5a3a-4102-bc41-04679bfa2968") {
firstName
surnName
webSite
email
}
a2: authorById(id: "520163be-6122-47d3-91a4-56a203a5c2aa") {
firstName
surnName
webSite
email
}
}
There are different types of directives including:
@include(if: Boolean)
@skip(if: Boolean)
used in the following query:
query autoriFragment($limit: Int, $shouldInclude: Boolean!) {
authors(limit: $limit) @include(if:$shouldInclude) {
... Autore
}
}
fragment Autore on Author {
id
firstName
surnName
}
There are two types of fragments: fragment
(as in the example above) which allow you to reuse the same block of information returned between different queries and inline fragment
which allow you to manage fragments directly in queries:
query autoriFragment($limit: Int, $shouldInclude: Boolean!) {
authors(limit: $limit) @include(if:$shouldInclude) {
... on Author {
id
firstName
surnName
}
}
}
This second mode is very useful when a query can return different kinds of types (very useful implementing Global Object Identification, as we will see later).