Skip to content

Commit a972cec

Browse files
author
Jason Kuhrt
authored
feat(schema): add subscriptionType (#1191)
closes #447
1 parent eb54c3c commit a972cec

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"dependencies": {
3232
"@nexus/logger": "^0.2.0",
33-
"@nexus/schema": "^0.14.0",
33+
"@nexus/schema": "^0.15.0-next.1",
3434
"@types/express": "^4.17.2",
3535
"@types/node-fetch": "^2.5.5",
3636
"@types/prompts": "^2.0.3",

src/lib/nexus-schema-stateful/stateful-nexus-schema.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as NexusSchema from '@nexus/schema'
22
import * as GraphQL from 'graphql'
3-
import * as CustomTypes from './custom-types'
43
import * as Scalars from '../scalars'
4+
import * as CustomTypes from './custom-types'
55

66
// todo use this as return type of constructor
77
export interface StatefulNexusSchema {
@@ -16,6 +16,7 @@ export interface StatefulNexusSchema {
1616
export interface NexusSchemaStatefulBuilders {
1717
queryType: typeof NexusSchema.queryType
1818
mutationType: typeof NexusSchema.mutationType
19+
subscriptionType: typeof NexusSchema.subscriptionType
1920
objectType: ReturnType<typeof createNexusSchemaStateful>['builders']['objectType']
2021
enumType: ReturnType<typeof createNexusSchemaStateful>['builders']['enumType']
2122
scalarType: ReturnType<typeof createNexusSchemaStateful>['builders']['scalarType']
@@ -80,6 +81,9 @@ type NexusSchemaTypeDef =
8081
| NexusSchema.core.NexusExtendInputTypeDef<any>
8182
| NexusSchema.core.NexusExtendTypeDef<any>
8283

84+
/**
85+
* Create an instance of Stateful Nexus Schema
86+
*/
8387
export function createNexusSchemaStateful() {
8488
const state: StatefulNexusSchema['state'] = {
8589
types: [],
@@ -145,6 +149,12 @@ export function createNexusSchemaStateful() {
145149
return typeDef
146150
}
147151

152+
const subscriptionType: typeof NexusSchema.subscriptionType = (config) => {
153+
const typeDef = NexusSchema.subscriptionType(config)
154+
state.types.push(typeDef)
155+
return typeDef
156+
}
157+
148158
const extendType: typeof NexusSchema.extendType = (config) => {
149159
const typeDef = NexusSchema.extendType(config)
150160
state.types.push(typeDef)
@@ -172,17 +182,23 @@ export function createNexusSchemaStateful() {
172182
}
173183

174184
const arg = NexusSchema.arg
185+
175186
const intArg = NexusSchema.intArg
187+
176188
const stringArg = NexusSchema.stringArg
189+
177190
const idArg = NexusSchema.idArg
191+
178192
const floatArg = NexusSchema.floatArg
193+
179194
const booleanArg = NexusSchema.booleanArg
180195

181196
return {
182197
state: state,
183198
builders: {
184199
queryType,
185200
mutationType,
201+
subscriptionType,
186202
objectType,
187203
inputObjectType,
188204
unionType,

website/content/04-api/01-nexus/01-schema.mdx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,45 @@ Refer to `objectType`. This is a shorthand where `config.name` is assigned `Muta
557557

558558
## `subscriptionType`
559559

560-
Not implemented, please see [#447](https://github.com/graphql-nexus/nexus/issues/447).
560+
[GraphQL Spec](https://spec.graphql.org/June2018/#sec-Subscription-Operation-Definitions)
561+
562+
Similar to [`objectType`](#objecttype). However, there are a few differences with regard to field configuration. Here is an overview. For details see below.
563+
564+
```ts
565+
schema.subscriptionType({
566+
// rootTyping // There is no rootTyping configuration
567+
definition(t) {
568+
t.field({ // This and all other field builders...
569+
subscribe() {} // ... uniquely have a subscribe method
570+
resolve(event) {} // ... have an adjustment to the Backing type
571+
})
572+
}
573+
})
574+
```
575+
576+
### Signature
577+
578+
<!-- prettier-ignore -->
579+
```ts
580+
(config: {
581+
definition: SubscriptionDefinitionBlock
582+
description?: string
583+
nonNullDefaults?: NonNullConfig
584+
}) => NexusInputObjectType
585+
```
586+
587+
### Field Configuration
588+
589+
Extends field configurations of those found in [`objectType`](#objecttype) like so:
590+
591+
```ts
592+
subscribe(root, args, ctx, info): MaybePromise<AsyncIterator<...>>
593+
resolve(event, args, ctx, info): MaybePromise<...>
594+
```
595+
596+
- `subscribe` Called once when the initial subscription request is sent by the client. It must return an `AsyncIterator`. The values emitted via it flow into the peer method `resolve`. The point of this method is to setup the data link between your streaming data source and a client subscription. It is not concerned with shaping the data coming off the streaming data source into something comforming to the GraphQL type assigned to the field. that is the job of the peer method `resolve`.
597+
598+
- `resolve` Called for every value emitted by the `AsyncIterator` returned by `subscribe`. The value is the first argument to `resolve`, replacing the backing data parameter that is usuaully passed there. The point of this method is to receive the raw event data from your data source and make it conform to the GraphQL type assigned to the field––just like all resolvers do. The only difference is that the backing data aspect.
561599

562600
## `inputObjectType`
563601

@@ -570,9 +608,10 @@ Defines an object which can be passed as an input value.
570608
<!-- prettier-ignore -->
571609
```ts
572610
(config: {
611+
name: string
612+
definition: InputObjectDefinitionBlock
573613
description?: string
574614
nonNullDefaults?: NonNullConfig
575-
definition: InputObjectDefinitionBlock
576615
}) => NexusInputObjectType
577616
```
578617

website/content/06-components-standalone/01-schema/01-api/10-api-queryField.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ codeStyle: true
55

66
## queryField
77

8-
Often times you want to split up query fields into different domains of your application, and like [`mutationField`](api-mutationField) are another one of the most common use-cases for `extendType`. `queryField` exists as a shorthand for this common case:
8+
Often times you want to split up query fields into different domains of your application, and like [`mutationField`](/components-standalone/schema/api/api-mutationField) are another one of the most common use-cases for `extendType`. `queryField` exists as a shorthand for this common case:
99

1010
```ts
1111
import { stringArg } from '@nexus/schema'

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,10 @@
529529
lodash "^4.17.15"
530530
strip-ansi "^6.0.0"
531531

532-
"@nexus/schema@^0.14.0":
533-
version "0.14.0"
534-
resolved "https://registry.yarnpkg.com/@nexus/schema/-/schema-0.14.0.tgz#3242f4d02ec9458914deb638b434bcfbd5888d2e"
535-
integrity sha512-niJFcOm1F2s3kwj+j3ips4sIIJaTNh5Auq+zcHwOsRBqSYsFJnA/8QUKMb1wjVIEqS+Jl71Sj4xFdg3/njlm9A==
532+
"@nexus/schema@^0.15.0-next.1":
533+
version "0.15.0-next.1"
534+
resolved "https://registry.yarnpkg.com/@nexus/schema/-/schema-0.15.0-next.1.tgz#39def5322a6cc821914a9f2dee40e1333257e9f8"
535+
integrity sha512-PBml7hY5DgqTEEEk9jwczUvBcyMtX9HKR8qfbU1tc+kZ+8uRodnSOatMqN6Haz6QuZhkZBLw4gh2gnpOUYWjPw==
536536
dependencies:
537537
iterall "^1.2.2"
538538
tslib "^1.9.3"

0 commit comments

Comments
 (0)