Skip to content

Commit 59d853f

Browse files
committed
Documentation changes
Signed-off-by: Alan Cha <[email protected]>
1 parent 07b522c commit 59d853f

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

packages/openapi-to-graphql/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ Resolver options:
178178

179179
- `customResolvers` (type: `object`, default: `{}`): OpenAPI-to-GraphQL, by default, creates resolver functions that make REST calls to resolve fields in the generated GraphQL interface. This option allows users to provide custom resolver functions to be used in place of said ones created by OpenAPI-to-GraphQL. The field that the custom resolver will affect is identifed first by the [title](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#infoObject) of the OAS, then the [path](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#paths-object) of the operation, and lastly the [method](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-item-object) of the operation. The `customResolvers` object is thus a triply nested object where the outer key is the title, followed by the path, and finally the method, which points to the [resolver function](https://graphql.org/learn/execution/#root-fields-resolvers) itself. The resolver function can use the parameters `obj`, `args`, `context`, and `info` in order to produce the proper data, as do standard [resolver functions](https://graphql.org/learn/execution/#root-fields-resolvers) in GraphQL. Use cases include the resolution of complex relationships between types, implementing performance improvements like caching, or dealing with non-standard authentication requirements. _Note: Because the arguments are provided by the GraphQL interface, they may look different from the [parameters](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterObject) defined by the OAS. For example, they will have [sanitized](https://github.com/Alan-Cha/openapi-to-graphql#characteristics) names. The [request body](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#requestBodyObject) will also be contained in the arguments as an [input object type](https://graphql.org/graphql-js/mutations-and-input-types/)._
180180

181-
- `createSubscriptionsFromCallbacks` (type: `boolean`, default: `false`): Allow to generate subscription fields from CallbackObjects in OpenAPI schema. Path ( runtime expression ) of the [CallbackObject](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#CallbackObject) will be interpolated as topic of publish / subscription to use with a [pubsub](https://github.com/apollographql/graphql-subscriptions) instance.
182-
Read the [doc](./docs/SUBSCRIPTIONS.md) for explanations and examples regarding its usage.
181+
- `createSubscriptionsFromCallbacks` (type: `boolean`, default: `false`): Generates subscription fields from [callback objects](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#CallbackObject). The keys ([runtime expressions](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#runtimeExpression)) of the callback objects will be interpolated as the topic of a publish/subscription connection using [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions). Read the [doc](./docs/SUBSCRIPTIONS.md) for explanations and examples regarding its usage.
182+
183183
***
184184

185185
Authentication options:

packages/openapi-to-graphql/docs/SUBSCRIPTIONS.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Subscriptions with OpenAPI-to-GraphQL
2-
Since version x.x.x, OpenAPI-to-GraphQL supports [GraphQL _subscription_ operations](http://spec.graphql.org/draft/#sec-Subscription). In GraphQL, using a subscription query, clients subscribe to updates on the data defined in the query. In this scneario, when data changes, the server publishes these changes to all clients that have active subscriptions for that data.
2+
Since version 2.1.0, OpenAPI-to-GraphQL supports [GraphQL _subscription_ operations](http://spec.graphql.org/draft/#sec-Subscription). In GraphQL, using a subscription query, clients subscribe to updates on the data defined in the query. In this scneario, when data changes, the server publishes these changes to all clients that have active subscriptions for that data.
33

44
The OpenAPI specification can define similar behavior using [callbacks](https://swagger.io/specification/#callbackObject): a callback defines a request that the server may initiate in response to receiving another request. Callbacks can thus be used to model publish/subscribe behavior. I.e., when the server receives a request to update some data, it can then itself issue callback requests (outside of the first request/response cycle) to any number of subscribed clients to inform about the new data.
55

6-
When enabling the `createSubscriptionsFromCallbacks` option, OpenAPI-to-GraphQL creates a subscription field for any operation in the given OpenAPI document that defines a `callback` object. In such cases, OpenAPI-to-GraphQL creates a [subscribe](http://spec.graphql.org/draft/#Subscribe()) function responsible to subscribing clients to receive results of callbacks being executed, and a special form of a [resolve](http://spec.graphql.org/draft/#ResolveFieldEventStream()) function, which pushes data updates to subscribed clients.
6+
When the [`createSubscriptionsFromCallbacks` option](https://github.com/IBM/openapi-to-graphql/tree/master/packages/openapi-to-graphql#options) is enabled, OpenAPI-to-GraphQL creates subscription fields from an operation's callback objects. In such cases, OpenAPI-to-GraphQL creates a [subscribe](http://spec.graphql.org/draft/#Subscribe()) function responsible to subscribing clients to receive results of callbacks being executed, and a special form of a [resolve](http://spec.graphql.org/draft/#ResolveFieldEventStream()) function, which pushes data updates to subscribed clients.
77

88
To create these two functions, OpenAPI-to-GraphQL relies on the popular [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package, which provides a unified API to support different network transports (like WebSockets, MQTT, Redis etc. - see this [list of supported transports](https://github.com/apollographql/graphql-subscriptions#pubsub-implementations)).
99

@@ -22,7 +22,7 @@ const eventEmitter = new EventEmitter2({
2222
delimiter: '/'
2323
});
2424

25-
// Create the PubSub instance ( here by wrapping an EventEmitter client )
25+
// Create the PubSub instance (here by wrapping an EventEmitter client)
2626
const pubsub = new PubSub()
2727

2828
export default pubsub
@@ -36,7 +36,7 @@ import { MQTTPubSub } = from 'graphql-mqtt-subscriptions'
3636

3737
const MQTT_PORT = 1883
3838

39-
// Create a PubSub instance ( here by wrapping a MQTT client )
39+
// Create a PubSub instance (here by wrapping a MQTT client)
4040
const client = connect(`mqtt://localhost:${MQTT_PORT}`)
4141

4242
const pubsub = new MQTTPubSub({
@@ -86,8 +86,7 @@ const init = async () => {
8686
subscribe,
8787
schema,
8888
onConnect: (params, socket, ctx) => {
89-
// adding pubsub to context
90-
// to be used by graphQL subscribe field
89+
// Add pubsub to context to be used by GraphQL subscribe field
9190
return { pubsub }
9291
}
9392
},
@@ -104,7 +103,7 @@ init()
104103

105104
## API server
106105

107-
A simple example could be the following, when an HTTP client tries to create a device ( via `post('/api/devices')` route ) an event is published by the PubSub instance.
106+
A simple example could be the following, when an HTTP client tries to create a device (via `post('/api/devices')` route) an event is published by the PubSub instance.
108107
If a callback like [#/components/callbacks/DevicesEvent](../test/fixtures/example_oas5.json) is declared in your OpenAPI schema and used in path `/devices` for the `post` Operation, a subscription field will be generated by OpenAPI-to-GraphQL.
109108

110109

@@ -147,7 +146,7 @@ const startServer = () => {
147146
payload: Buffer.from(JSON.stringify(device))
148147
}
149148

150-
// use pubsub to publish the event
149+
// Use pubsub to publish the event
151150
pubsub.publish(packet)
152151

153152
res.status(200).send(device)
@@ -192,8 +191,7 @@ const device = {
192191
}
193192

194193
const startClient = () => {
195-
// generate subscription :
196-
// via GraphQL WS API
194+
// Generate subscription via GraphQL WS API...
197195
const client = new SubscriptionClient(
198196
`ws://localhost:${GRAPHQL_HTTP_PORT}/subscriptions`
199197
)
@@ -220,15 +218,13 @@ const startClient = () => {
220218
},
221219
})
222220

223-
// or directly via PubSub instance
224-
// like OpenAPI-to-GraphQL would do
221+
// ...or directly via PubSub instance like OpenAPI-to-GraphQL would do
225222
pubsub.subscribe(`/api/${device.userName}/devices/POST/*`, (...args) => {
226223
console.log('Device created', args)
227224
})
228225

229226

230-
// trigger device creation:
231-
// via GraphQL HTTP API
227+
// Trigger device creation via GraphQL HTTP API...
232228
axios({
233229
url: `http://localhost:${GRAPHQL_HTTP_PORT}/graphql`,
234230
method: 'POST',
@@ -244,8 +240,7 @@ const startClient = () => {
244240
},
245241
})
246242

247-
// or via REST API
248-
// like OpenAPI-to-GraphQL would do
243+
// ...or via REST API like OpenAPI-to-GraphQL would do
249244
axios({
250245
url: `http://localhost:${REST_HTTP_PORT}/api/devices`,
251246
method: 'POST',
@@ -260,9 +255,9 @@ startClient()
260255
In this example, we rely on the [`subscriptions-transport-ws` package](https://github.com/apollographql/subscriptions-transport-ws) to create a `SubscriptionServer` that manages WebSockets connections between the GraphQL clients and our server. We also rely on the `graphqlExpress` server provided by the [`apollo-server-express` package](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-express) to serve GraphQL from Express.js.
261256

262257

263-
Sidenote concerning callback, as you can see in the example Callback, the path (runtime expression) `/api/{$request.body#/userName}/devices/{$request.body#/method}/+` is delimited by `/` and ends with `+`, these symbols are interpreted as delimiters and wildcard when using MQTT topics.
258+
Concerning callbacks, as you can see in the example, the path (runtime expression) `/api/{$request.body#/userName}/devices/{$request.body#/method}/+` is delimited by `/` and ends with `+`, these symbols are interpreted as delimiters and wildcard when using MQTT topics.
264259
It needs to be adapted accordingly to the client wrapped in your PubSub instance, for eventEmitter2 you can use `*` and define your own delimiter.
265-
An helper might be provided in the future, to simplify this process.
260+
A helper might be provided in the future, to simplify this process.
266261

267262
## Examples
268263

0 commit comments

Comments
 (0)