Publicera händelse till GraphQL-prenumeration

GÄLLER FÖR: Alla API Management-nivåer

Principen publish-event publicerar en händelse till en eller flera prenumerationer som anges i ett GraphQL API-schema. Konfigurera principen i en GraphQL-matchare för ett relaterat fält i schemat för en annan åtgärdstyp, till exempel en mutation. Vid körning publiceras händelsen till anslutna GraphQL-klienter. Läs mer om GraphQL-API:er i API Management.

Kommentar

Ange principens element och underordnade element i den ordning som anges i principbeskrivningen. Läs mer om hur du anger eller redigerar API Management-principer.

Principuttryck

<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>

Element

Name beskrivning Obligatoriskt
Mål En eller flera prenumerationer i GraphQL-schemat, som anges i target underelement, till vilka händelsen publiceras. Ja

Användning

Användningsanteckningar

  • Den här principen anropas endast när en relaterad GraphQL-fråga eller mutation körs.

Exempel

Följande exempelprincipdefinition konfigureras i en lösning för mutationen createUser . Den publicerar en händelse till prenumerationen onUserCreated .

Exempelschema

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


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

type Subscription {
    onUserCreated: User!
}

Exempelprincip

<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>

Mer information om hur du arbetar med principer finns i: