Skip to content

Commit

Permalink
Updated "Persisted operations" documentation (#7734)
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 authored Nov 18, 2024
1 parent bf43444 commit b684147
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,50 @@ AddSha256DocumentHashProvider(HashFormat.Base64)
## Blocking regular operations

If you want to disallow any dynamic operations, you can enable `OnlyAllowPersistedOperations`:
If you want to disallow any dynamic operations, you can enable `OnlyAllowPersistedDocuments`:

```csharp
builder.Services
.AddGraphQLServer()
// Omitted for brevity
.ModifyRequestOptions(o => o.OnlyAllowPersistedOperations = true);
.ModifyRequestOptions(
options => options
.PersistedOperations
.OnlyAllowPersistedDocuments = true);
```

This will block any dynamic operations that do not contain the `id` of a persisted operation.

You might still want to allow the execution of dynamic operations in certain circumstances. You can override the `OnlyAllowPersistedOperations` rule on a per-request basis, using the `AllowNonPersistedOperation` method on the `OperationRequestBuilder`. Simply implement a custom [IHttpRequestInterceptor](/docs/hotchocolate/v14/server/interceptors#ihttprequestinterceptor) and call `AllowNonPersistedOperation` if a certain condition is met:
You might still want to allow the execution of dynamic operations in certain circumstances. You can override the `OnlyAllowPersistedDocuments` rule on a per-request basis, using the `AllowNonPersistedOperation` method on the `OperationRequestBuilder`. Simply implement a custom [IHttpRequestInterceptor](/docs/hotchocolate/v14/server/interceptors#ihttprequestinterceptor) and call `AllowNonPersistedOperation` if a certain condition is met:

```csharp
builder.Services
.AddGraphQLServer()
// Omitted for brevity
.AddHttpRequestInterceptor<CustomHttpRequestInterceptor>()
.ModifyRequestOptions(o => o.OnlyAllowPersistedOperations = true);
.ModifyRequestOptions(
options => options
.PersistedOperations
.OnlyAllowPersistedDocuments = true);

public class CustomHttpRequestInterceptor : DefaultHttpRequestInterceptor
public class CustomHttpRequestInterceptor
: DefaultHttpRequestInterceptor
{
public override ValueTask OnCreateAsync(HttpContext context,
IRequestExecutor requestExecutor, OperationRequestBuilder requestBuilder,
public override ValueTask OnCreateAsync(
HttpContext context,
IRequestExecutor requestExecutor,
OperationRequestBuilder requestBuilder,
CancellationToken cancellationToken)
{
if (context.Request.Headers.ContainsKey("X-Developer"))
{
requestBuilder.AllowNonPersistedOperation();
}

return base.OnCreateAsync(context, requestExecutor, requestBuilder,
return base.OnCreateAsync(
context,
requestExecutor,
requestBuilder,
cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,50 @@ AddSha256DocumentHashProvider(HashFormat.Base64)
## Blocking regular operations

If you want to disallow any dynamic operations, you can enable `OnlyAllowPersistedOperations`:
If you want to disallow any dynamic operations, you can enable `OnlyAllowPersistedDocuments`:

```csharp
builder.Services
.AddGraphQLServer()
// Omitted for brevity
.ModifyRequestOptions(o => o.OnlyAllowPersistedOperations = true);
.ModifyRequestOptions(
options => options
.PersistedOperations
.OnlyAllowPersistedDocuments = true);
```

This will block any dynamic operations that do not contain the `id` of a persisted operation.

You might still want to allow the execution of dynamic operations in certain circumstances. You can override the `OnlyAllowPersistedOperations` rule on a per-request basis, using the `AllowNonPersistedOperation` method on the `OperationRequestBuilder`. Simply implement a custom [IHttpRequestInterceptor](/docs/hotchocolate/v15/server/interceptors#ihttprequestinterceptor) and call `AllowNonPersistedOperation` if a certain condition is met:
You might still want to allow the execution of dynamic operations in certain circumstances. You can override the `OnlyAllowPersistedDocuments` rule on a per-request basis, using the `AllowNonPersistedOperation` method on the `OperationRequestBuilder`. Simply implement a custom [IHttpRequestInterceptor](/docs/hotchocolate/v15/server/interceptors#ihttprequestinterceptor) and call `AllowNonPersistedOperation` if a certain condition is met:

```csharp
builder.Services
.AddGraphQLServer()
// Omitted for brevity
.AddHttpRequestInterceptor<CustomHttpRequestInterceptor>()
.ModifyRequestOptions(o => o.OnlyAllowPersistedOperations = true);
.ModifyRequestOptions(
options => options
.PersistedOperations
.OnlyAllowPersistedDocuments = true);

public class CustomHttpRequestInterceptor : DefaultHttpRequestInterceptor
public class CustomHttpRequestInterceptor
: DefaultHttpRequestInterceptor
{
public override ValueTask OnCreateAsync(HttpContext context,
IRequestExecutor requestExecutor, OperationRequestBuilder requestBuilder,
public override ValueTask OnCreateAsync(
HttpContext context,
IRequestExecutor requestExecutor,
OperationRequestBuilder requestBuilder,
CancellationToken cancellationToken)
{
if (context.Request.Headers.ContainsKey("X-Developer"))
{
requestBuilder.AllowNonPersistedOperation();
}

return base.OnCreateAsync(context, requestExecutor, requestBuilder,
return base.OnCreateAsync(
context,
requestExecutor,
requestBuilder,
cancellationToken);
}
}
Expand Down

0 comments on commit b684147

Please sign in to comment.