The Resource Graph
The ResourceGraph
is a map of all the JSON:API resources and their relationships that your API serves.
It is built at app startup and available as a singleton through Dependency Injection.
Note
Prior to v4, this was called the ContextGraph
.
Constructing The Graph
There are three ways the resource graph can be created:
- Auto-discovery
- Specifying an entire DbContext
- Manually specifying each resource
It is also possible to combine the three of them at once. Be aware that some configuration might overlap, for example one could manually add a resource to the graph which is also auto-discovered. In such a scenario, the configuration is prioritized by the list above in descending order.
Auto-discovery
Auto-discovery refers to the process of reflecting on an assembly and detecting all of the JSON:API resources, resource definitions, resource services and repositories.
The following command builds the resource graph using all IIdentifiable
implementations and registers the services mentioned.
You can enable auto-discovery for the current assembly by adding the following at startup.
// Program.cs
builder.Services.AddJsonApi(discovery: discovery => discovery.AddCurrentAssembly());
Specifying an Entity Framework Core DbContext
If you are using Entity Framework Core as your ORM, you can add all the models of a DbContext
to the resource graph.
// Program.cs
builder.Services.AddJsonApi<AppDbContext>();
Be aware that this does not register resource definitions, resource services and repositories. You can combine it with auto-discovery to achieve this.
// Program.cs
builder.Services.AddJsonApi<AppDbContext>(discovery: discovery => discovery.AddCurrentAssembly());
Manual Specification
You can manually construct the graph.
// Program.cs
builder.Services.AddJsonApi(resources: resourceGraphBuilder =>
resourceGraphBuilder.Add<Person, int>());
Resource Name
The public resource name is exposed through the type
member in the JSON:API payload. This can be configured using the following approaches (in order of priority):
- The
publicName
parameter when manually adding a resource to the graph.
// Program.cs
builder.Services.AddJsonApi(resources: resourceGraphBuilder =>
{
resourceGraphBuilder.Add<Person, int>(publicName: "individuals");
});
- The
PublicName
property when a model is decorated with aResourceAttribute
.
[Resource(PublicName = "individuals")]
public class Person : Identifiable<int>
{
}
- The configured naming convention (by default this is camel-case), after pluralization.
// this will be registered as "people"
public class Person : Identifiable<int>
{
}
The default naming convention can be changed in options.