Skip to content

Latest commit

 

History

History
103 lines (77 loc) · 3.25 KB

File metadata and controls

103 lines (77 loc) · 3.25 KB
title Azure API Management policy reference - publish-event | Microsoft Docs
description Reference for the publish-event policy available for use in Azure API Management. Provides policy usage, settings, and examples.
services api-management
author dlepow
ms.service azure-api-management
ms.topic reference
ms.date 07/23/2024
ms.author danlep

Publish event to GraphQL subscription

[!INCLUDE api-management-availability-all-tiers]

The publish-event policy publishes an event to one or more subscriptions specified in a GraphQL API schema. Configure the policy in a GraphQL resolver for a related field in the schema for another operation type such as a mutation. At runtime, the event is published to connected GraphQL clients. Learn more about GraphQL APIs in API Management.

[!INCLUDE api-management-policy-generic-alert]

Policy statement

<http-data-source>
<!-- http-data-source is an example resolver policy -->
    <http-request>
    [...]
    </http-request>
    <http-response>
        [...]
        <publish-event>
            <targets>
                <graphql-subscription id="subscription field" />
            </targets>
        </publish-event>
    </http-response>
</http-data-source>

Elements

Name Description Required
targets One or more subscriptions in the GraphQL schema, specified in target subelements, to which the event is published. Yes

Usage

Usage notes

  • This policy is invoked only when a related GraphQL query or mutation is executed.
  • Resolver should not be defined for the corresponding subscription. Defining a publish-event policy on a source query or mutation is sufficient to trigger subscription events.

Example

The following example policy definition is configured in a resolver for the createUser mutation. It publishes an event to the onUserCreated subscription.

Example schema

type User {
  id: Int!
  name: String!
}


type Mutation {
    createUser(id: Int!, name: String!): User
}

type Subscription {
    onUserCreated: User!
}

Example policy

<http-data-source>
    <http-request>
        <set-method>POST</set-method>
        <set-url>https://contoso.com/api/user</set-url>
        <set-body template="liquid">{ "id" : {{body.arguments.id}}, "name" : "{{body.arguments.name}}"}</set-body>
    </http-request>
    <http-response>
        <publish-event>
            <targets>
                <graphql-subscription id="onUserCreated" />
            </targets>
        </publish-event>
    </http-response>
</http-data-source>

Related policies

[!INCLUDE api-management-policy-ref-next-steps]