Reference

UsePersistedOperationsEnforcement

Package Trax.Api.GraphQL.PersistedOperationsNamespace Trax.Api.GraphQL.PersistedOperations.ExtensionsSource TraxGraphQLBuilderPersistedOperationsExtensions.cs, PersistedOperationEnforcementMiddleware.cs, PersistedOperationPolicy.cs

Kept so existing hosts compile. It adds nothing to the ASP.NET pipeline: enforcement runs inside HotChocolate's execution pipeline as soon as UsePersistedOperations is configured, whether or not this is called. It is hidden from IntelliSense.

Signature

[EditorBrowsable(EditorBrowsableState.Never)]
public static IApplicationBuilder UsePersistedOperationsEnforcement(
    this IApplicationBuilder app
);

Returns

app, unchanged.

Behavior

Throws ArgumentNullException when app is null, and otherwise does nothing.

Where enforcement runs

A request middleware placed right after HotChocolate's document parser and before validation. Every transport reaches the executor through that pipeline: a JSON POST, a GET, a multipart POST, a WebSocket subscribe, and a request built in-process.

Decision flow

For each operation:

  1. If the document came from the store (the request named a persisted id): execute it.
  2. If the request carries HotChocolate's AllowNonPersistedOperation() override, which only host code building its own request can set: execute it.
  3. Otherwise the document is inline:
    • Allowlist match (operation name in AllowOperations or matching a predicate; the document id when there is no name): execute it. Both keys are chosen by the caller and the document is not inspected, so the allowlist is a convenience for developer tools, not a control, and with RequirePersisted(true) a host that declares one starts only in Development. See Allowlist and dev carve-outs.
    • Management surface (every operation selects persistedOperations { ... } at the root and nothing else): execute it. Authorization still applies.
    • Introspection (the parsed document's top-level selections are all __schema, __type or __typename): execute it unless DisableIntrospection() was called.
    • Enforcement off (RequirePersisted(false)): execute it, logging at Information when LogNonPersistedRequests(true).
    • Otherwise: refuse it. Over HTTP the status is 400 Bad Request and the body is:
{
  "errors": [{
    "message": "Only persisted operations are accepted on this server.",
    "extensions": { "code": "PERSISTED_OPERATION_REQUIRED" }
  }]
}

Over a socket the same error arrives as an error message for that operation id. Each entry of a batched request is decided on its own.

Auth ordering

Enforcement runs inside HotChocolate's request pipeline, after the document is parsed. Authentication happens before it: Trax's HTTP request interceptor sets HttpContext.User when it builds the request, and the endpoint policy of a builder gated with RequireAuthorization(...) is evaluated in the same pipeline ahead of the document cache. An unauthenticated caller on such an endpoint is therefore refused before enforcement decides anything about its document.

Example

var app = builder.Build();
app.UsePersistedOperationsEnforcement(); // no effect: enforcement comes with UsePersistedOperations

See also