33.9 Code-first: mapping an endpoint in Program.cs
MapRelatudeDBGraphQL maps one route that exposes the whole datamodel in GraphQL-style names,
with its options set in code. An interface IEvent gives the root fields iEvent and iEvents
(§33.3).
using Relatude.DB.Datamodels; // QueryContext
using Relatude.DB.GraphQL;
using Relatude.DB.NodeServer;
app.UseRelatudeDB();
app.MapRelatudeDBGraphQL("/graphql", o => {
// the store to answer from
o.StoreResolver = _ => RelatudeDBRuntime.Database.Datastore;
// read as an anonymous visitor, in the culture the request names
o.QueryContextFactory = http =>
QueryContext.Anonymous.Culture(http.Request.Query["culture"].FirstOrDefault());
// for mutations: run them through the NodeStore, so transaction plugins see them
o.TransactionExecutor = (_, transaction, _) => RelatudeDBRuntime.Database.Execute(transaction);
o.TypeFilter = type => type.CodeName != "IAttendee"; // keep a type out of the schema
o.EnableExplorer = true;
o.EnableFacetSearch = true;
o.FacetSearch = RelatudeDBRuntime.Server.GraphQL.FacetSearch;
});| Option | Default | What it does |
|---|---|---|
StoreResolver |
IDataStore from the request's services |
The store to answer from. AddRelatudeDB registers no IDataStore, so set this, as above. While it returns null or throws, such as before the database opens, the route answers 503. |
QueryContextFactory |
an anonymous visitor | The query context per request: user, culture, unpublished or hidden nodes (§29). A request reads as an anonymous visitor when there is no factory or it returns null. |
TransactionExecutor |
IDataStore.Execute |
Runs a mutation's transaction. The default skips transaction plugins. |
TypeFilter |
none | Return false to keep a node type out of the schema. |
FacetSearch |
none | Answers the facet page's requests. |
AllowMutations, EnableIntrospection, EnableGetRequests, EnableExplorer, EnableFacetSearch, MaxFacetCards, IncludeSystemTypes, MaxQueryDepth, MaxIncludeDepth, DefaultPageSize, MaxPageSize |
as in §33.2 | The same settings as the definition file. |
An endpoint mapped this way has no API keys. Protect its path with the application's own middleware or a reverse proxy. The route builds its schema on the first request, and again when the store is replaced, for example by a soft restart.
Without HTTP. RelatudeGraphQL runs requests from your own code. It takes a data store and
options, or a definition:
var gql = new RelatudeGraphQL(db.Datastore, new GraphQLOptions { AllowMutations = true });
var request = new GraphQLRequest { Query = "{ iEvents { totalCount } }" };
GraphQLResult result = gql.Execute(request, QueryContext.Anonymous); // without one: the store's default context
string json = result.ToJson();
string sdl = gql.ToSDL();
IReadOnlyList<string> warnings = gql.Warnings; // what the schema left out or named otherwiseAn instance is immutable and safe to share. Its schema is the one the datamodel had when it was built.