|
| 1 | +// |
| 2 | +// Copyright (c) 2020 Contributors to the Eclipse Foundation |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +[[subscriptions]] |
| 18 | +=== Subscriptions |
| 19 | + |
| 20 | +Subscriptions enable clients to receive real-time updates when events occur. Unlike queries and mutations which return |
| 21 | +a single response, subscriptions establish a long-lived connection that sends multiple values over time. |
| 22 | + |
| 23 | +==== API Annotation |
| 24 | + |
| 25 | +For classes that are annotated with `@GraphQLApi`, implementations must create a subscription in the schema for every |
| 26 | +method that is annotated with `@Subscription`. |
| 27 | + |
| 28 | +The subscription's name can be specified in the value parameter of the `@Subscription` annotation, or is generated from |
| 29 | +the method name if no annotation value is provided. |
| 30 | + |
| 31 | +==== Return Types |
| 32 | + |
| 33 | +Subscription methods must return a reactive type that emits a stream of values over time. The MicroProfile GraphQL |
| 34 | +specification requires implementations to support `java.util.concurrent.Flow.Publisher<T>` as the return type. |
| 35 | +Implementations may provide support for additional reactive types (such as reactive streams `Publisher<T>` or |
| 36 | +framework-specific types). |
| 37 | + |
| 38 | +==== Basic POJO Example |
| 39 | + |
| 40 | +.Example |
| 41 | +[source,java,numbered] |
| 42 | +---- |
| 43 | +@Subscription |
| 44 | +public Flow.Publisher<SuperHero> heroUpdates() { |
| 45 | + // Return a publisher that emits SuperHero updates |
| 46 | + return publisherService.getHeroUpdateStream(); |
| 47 | +} |
| 48 | +---- |
| 49 | + |
| 50 | +.Example with Argument |
| 51 | +[source,java,numbered] |
| 52 | +---- |
| 53 | +@Subscription |
| 54 | +public Flow.Publisher<SuperHero> heroByName(@Name("name") String heroName) { |
| 55 | + return publisherService.getHeroUpdateStream(heroName); |
| 56 | +} |
| 57 | +---- |
| 58 | + |
| 59 | +Note that generic types other than subtypes of `java.util.Collection` (such as `java.util.List` or `java.util.Set`) |
| 60 | +are not allowed to be specified within the publisher's generic type parameter. Implementations may allow additional |
| 61 | +types (such as `java.util.Map`), but the behavior for these return types are undefined. |
| 62 | + |
| 63 | +==== Name |
| 64 | + |
| 65 | +The name of a subscription in the schema is obtained using the following order: |
| 66 | + |
| 67 | +* if the method is annotated with a `@Subscription` annotation containing a non-empty String for it's value, that |
| 68 | +String value is used as the subscription name. |
| 69 | +* if the method is annotated with a `@Name` annotation containing a non-empty String for it's value, that String value |
| 70 | +is used as the subscription name. |
| 71 | +* if the method is annotated with a `@JsonbProperty` annotation containing a non-empty String for it's value, that |
| 72 | +String value is used as the subscription name. |
| 73 | +* if no other name can be determined, the Java method name is used as the subscription name. (with the get/is removed |
| 74 | +if this is a getter) |
| 75 | + |
| 76 | +Note that it is considered a deployment error if more than one subscription method has the same name with the same |
| 77 | +arguments. |
| 78 | + |
| 79 | +==== Description |
| 80 | + |
| 81 | +Subscriptions may be documented with descriptions in the schema by adding a `@Description` annotation with |
| 82 | +documentation text as the annotation value to the subscription method. For example: |
| 83 | + |
| 84 | +.DescriptionExample |
| 85 | +[source,java,numbered] |
| 86 | +---- |
| 87 | +@Subscription |
| 88 | +@Description("Get real-time stock price updates") |
| 89 | +public Flow.Publisher<Stock> stockQuote(@Name("stockCode") String code) { |
| 90 | + return stockService.getPriceUpdates(code); |
| 91 | +} |
| 92 | +---- |
| 93 | + |
| 94 | +This would generate a schema that would include: |
| 95 | + |
| 96 | +.DescriptionSchemaExample |
| 97 | +[source,numbered] |
| 98 | +---- |
| 99 | +type Subscription { |
| 100 | + ... |
| 101 | + "Get real-time stock price updates" |
| 102 | + stockQuote(stockCode: String): Stock |
| 103 | + #... |
| 104 | +---- |
| 105 | + |
| 106 | +The `@Description` annotation can also be placed on parameters of a subscription method to provide documentation for |
| 107 | +the arguments. For example: |
| 108 | + |
| 109 | +.ArgumentDescriptionExample |
| 110 | +[source,java,numbered] |
| 111 | +---- |
| 112 | +@Subscription |
| 113 | +@Description("Get real-time stock price updates") |
| 114 | +public Flow.Publisher<Stock> stockQuote(@Name("stockCode") @Description("Stock symbol code") String code) { |
| 115 | + return stockService.getPriceUpdates(code); |
| 116 | +} |
| 117 | +---- |
| 118 | + |
| 119 | +This would generate a schema that would include: |
| 120 | + |
| 121 | +.ArgumentDescriptionSchemaExample |
| 122 | +[source,numbered] |
| 123 | +---- |
| 124 | +type Subscription { |
| 125 | + ... |
| 126 | + "Get real-time stock price updates" |
| 127 | + stockQuote( |
| 128 | + "Stock symbol code" |
| 129 | + stockCode: String |
| 130 | + ): Stock |
| 131 | + #... |
| 132 | +---- |
| 133 | + |
| 134 | +==== Void Subscriptions |
| 135 | + |
| 136 | +By its very nature, subscription methods must return some value stream, thus it is considered a deployment error for a |
| 137 | +method with a `void` return type to be annotated with `@Subscription`. If a void method is annotated with the |
| 138 | +`@Subscription` annotation, the implementation must prevent the application from starting and should provide a log |
| 139 | +message indicating that this is not allowed. |
| 140 | + |
| 141 | +==== Transport Mechanism |
| 142 | + |
| 143 | +Unlike queries and mutations which can be executed over HTTP (see <<queries>> and <<mutations>>), subscriptions require |
| 144 | +a persistent connection to stream multiple values over time. To ensure portability across implementations, this |
| 145 | +specification mandates support for WebSocket as the transport protocol. |
| 146 | + |
| 147 | +Implementations must support the graphql-ws protocol version 6.0.x |
| 148 | +(https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) for subscription operations over WebSocket. |
| 149 | +This protocol provides a standardized way to: |
| 150 | + |
| 151 | +* Establish and maintain subscription connections |
| 152 | +* Send subscription requests with operation, variables, and extensions |
| 153 | +* Receive streamed subscription events |
| 154 | +* Handle errors and connection lifecycle |
| 155 | + |
| 156 | +Implementations may optionally support additional transport mechanisms such as Server-Sent Events (SSE), chunked |
| 157 | +transfer encoding, or other streaming protocols, but WebSocket support using the graphql-ws protocol version 6.0.x |
| 158 | +is required as the minimum baseline for interoperability. |
0 commit comments