|
| 1 | +--- |
| 2 | +'graphql-ws': major |
| 3 | +--- |
| 4 | + |
| 5 | +`onSubscribe`, `onOperation`, `onError`, `onNext` and `onComplete` hooks don't have the full accompanying message anymore, only the ID and the relevant part from the message |
| 6 | + |
| 7 | +There is really no need to pass the full `SubscribeMessage` to the `onSubscribe` hook. The only relevant parts from the message are the `id` and the `payload`, the `type` is useless since the hook inherently has it (`onNext` is `next` type, `onError` is `error` type, etc). |
| 8 | + |
| 9 | +The actual techincal reason for not having the full message is to avoid serialising results and errors twice. Both `onNext` and `onError` allow the user to augment the result and return it to be used instead. `onNext` originally had the `NextMessage` argument which already has the `FormattedExecutionResult`, and `onError` originally had the `ErrorMessage` argument which already has the `GraphQLFormattedError`, and they both also returned `FormattedExecutionResult` and `GraphQLFormattedError` respectivelly - meaning, if the user serialised the results - the serialisation would happen **twice**. |
| 10 | + |
| 11 | +### Migrating from v5 to v6 |
| 12 | + |
| 13 | +#### `onSubscribe` |
| 14 | + |
| 15 | +```diff |
| 16 | +import { ServerOptions, SubscribePayload } from 'graphql-ws'; |
| 17 | + |
| 18 | +const opts: ServerOptions = { |
| 19 | +- onSubscribe(ctx, message) { |
| 20 | +- const messageId = message.id; |
| 21 | +- const messagePayload: SubscribePayload = message.payload; |
| 22 | +- }, |
| 23 | ++ onSubscribe(ctx, id, payload) { |
| 24 | ++ const messageId = id; |
| 25 | ++ const messagePayload: SubscribePayload = payload; |
| 26 | ++ }, |
| 27 | +}; |
| 28 | +``` |
| 29 | + |
| 30 | +#### `onOperation` |
| 31 | + |
| 32 | +The `SubscribeMessage.payload` is not useful here at all, the `payload` has been parsed to ready-to-use graphql execution args and should be used instead. |
| 33 | + |
| 34 | +```diff |
| 35 | +import { ExecutionArgs } from 'graphql'; |
| 36 | +import { ServerOptions, SubscribePayload } from 'graphql-ws'; |
| 37 | + |
| 38 | +const opts: ServerOptions = { |
| 39 | +- onOperation(ctx, message) { |
| 40 | +- const messageId = message.id; |
| 41 | +- const messagePayload: SubscribePayload = message.payload; |
| 42 | +- }, |
| 43 | ++ onOperation(ctx, id, args) { |
| 44 | ++ const messageId = id; |
| 45 | ++ const executionArgs: ExecutionArgs = args; |
| 46 | ++ }, |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +#### `onError` |
| 51 | + |
| 52 | +The `ErrorMessage.payload` (`GraphQLFormattedError[]`) is not useful here at all, the user has access to `GraphQLError[]` that are true instances of the error containing object references to `originalError`s and other properties. The user can always convert and return `GraphQLFormattedError[]` by using the `.toJSON()` method. |
| 53 | + |
| 54 | +```diff |
| 55 | +import { GraphQLError, GraphQLFormattedError } from 'graphql'; |
| 56 | +import { ServerOptions } from 'graphql-ws'; |
| 57 | + |
| 58 | +const opts: ServerOptions = { |
| 59 | +- onError(ctx, message, errors) { |
| 60 | +- const messageId = message.id; |
| 61 | +- const graphqlErrors: readonly GraphQLError[] = errors; |
| 62 | +- const messagePayload: readonly GraphQLFormattedError[] = message.payload; |
| 63 | +- }, |
| 64 | ++ onError(ctx, id, errors) { |
| 65 | ++ const messageId = id; |
| 66 | ++ const graphqlErrors: readonly GraphQLError[] = errors; |
| 67 | ++ const messagePayload: readonly GraphQLFormattedError[] = errors.map((e) => e.toJSON()); |
| 68 | ++ }, |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +#### `onNext` |
| 73 | + |
| 74 | +The `NextMessage.payload` (`FormattedExecutionResult`) is not useful here at all, the user has access to `ExecutionResult` that contains actual object references to error instances. The user can always convert and return `FormattedExecutionResult` by serialising the errors with `GraphQLError.toJSON()` method. |
| 75 | + |
| 76 | +```diff |
| 77 | +import { ExecutionResult, FormattedExecutionResult } from 'graphql'; |
| 78 | +import { ServerOptions } from 'graphql-ws'; |
| 79 | + |
| 80 | +const opts: ServerOptions = { |
| 81 | +- onNext(ctx, message, result) { |
| 82 | +- const messageId = message.id; |
| 83 | +- const graphqlResult: ExecutionResult = result; |
| 84 | +- const messagePayload: FormattedExecutionResult = message.payload; |
| 85 | +- }, |
| 86 | ++ onNext(ctx, id, result) { |
| 87 | ++ const messageId = id; |
| 88 | ++ const graphqlResult: ExecutionResult = result; |
| 89 | ++ const messagePayload: FormattedExecutionResult = { ...result, errors: result.errors?.map((e) => e.toJSON()) }; |
| 90 | ++ }, |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +#### `onComplete` |
| 95 | + |
| 96 | +```diff |
| 97 | +import { ServerOptions } from 'graphql-ws'; |
| 98 | + |
| 99 | +const opts: ServerOptions = { |
| 100 | +- onComplete(ctx, message) { |
| 101 | +- const messageId = message.id; |
| 102 | +- }, |
| 103 | ++ onComplete(ctx, id) { |
| 104 | ++ const messageId = id; |
| 105 | ++ }, |
| 106 | +}; |
| 107 | +``` |
0 commit comments