Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subscriptionField overload for builder api access #694

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/definitions/queryField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function queryField(...args: any[]) {
if (typeof args[0] === 'function') {
return args[0](t)
}

const [fieldName, config] = args as [string, QueryFieldConfig<any>]
const finalConfig = typeof config === 'function' ? config() : config
t.field(fieldName, finalConfig)
Expand Down
27 changes: 21 additions & 6 deletions src/definitions/subscriptionField.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { extendType } from './extendType'
import { SubscriptionTypeConfig } from './subscriptionType'
import { extendType, NexusExtendTypeDef } from './extendType'
import { SubscriptionBuilder, SubscriptionTypeConfig } from './subscriptionType'

export type SubscriptionFieldConfig<FieldName extends string, Event> =
| SubscriptionTypeConfig<FieldName, Event>
| (() => SubscriptionTypeConfig<FieldName, Event>)

export function subscriptionField(
fieldFn: (t: SubscriptionBuilder) => void
): NexusExtendTypeDef<'Subscription'>

export function subscriptionField<FieldName extends string, Event>(
fieldName: FieldName,
config: SubscriptionFieldConfig<FieldName, Event>
): NexusExtendTypeDef<'Subscription'>

/**
* Add one field to the Subscription type
*/
export function subscriptionField<FieldName extends string, Event>(
fieldName: FieldName,
config: SubscriptionTypeConfig<FieldName, Event> | (() => SubscriptionTypeConfig<FieldName, Event>)
) {
export function subscriptionField(...args: any[]) {
return extendType({
type: 'Subscription',
definition(t) {
if (typeof args[0] === 'function') {
return args[0](t)
}

const [fieldName, config] = args as [string, SubscriptionFieldConfig<any, any>]
const finalConfig = typeof config === 'function' ? config() : config
t.field(fieldName, finalConfig as any)
},
Expand Down
11 changes: 6 additions & 5 deletions tests/__helpers/testApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ type HookSettings = {
}

export async function generateTypegen(settings: HookSettings) {
const rootDir = settings.rootDir
const projectDir = settings.rootDir

const typegenModulePath = join(rootDir, '__typegen.ts')
const entrypointModulePath = join(rootDir, '__app.ts')
const importPath = relative(rootDir, join(__dirname, '..', '..', 'src')).replace(/\\/g, '/')
const typegenModulePath = join(projectDir, '__typegen.ts')
const sdlFilePath = join(projectDir, '__schema.graphql')
const entrypointModulePath = join(projectDir, '__app.ts')
const importPath = relative(projectDir, join(__dirname, '..', '..', 'src')).replace(/\\/g, '/')

const entrypoint = require(entrypointModulePath)
const { plugins, ...types } = entrypoint
Expand All @@ -24,7 +25,7 @@ export async function generateTypegen(settings: HookSettings) {
types: types,
outputs: {
typegen: typegenModulePath,
schema: false,
schema: sdlFilePath,
},
shouldGenerateArtifacts: true,
plugins: plugins || [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
16 changes: 16 additions & 0 deletions tests/integrations/abstractTypes/missingIsTypeOf/__schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type Query {
ok: Boolean!
}

union U1 = A

union U2 = A
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type A {
name: String
}

type B {
age: Int
}

type Query {
union: Union
}

union Union = A | B
17 changes: 17 additions & 0 deletions tests/integrations/declarativeWrappingPlugin/__schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

type DeclarativeWrappingOutput {
someList: [String]
someListOfLists(int: Int!): [[String!]!]
someNullField(input: InlineInputType): String
someRequiredField: String!
}

input InlineInputType {
abc: Int!
}

type Query {
someField: DeclarativeWrappingOutput!
}
14 changes: 13 additions & 1 deletion tests/integrations/kitchenSink/__app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
queryType,
scalarType,
stringArg,
subscriptionField,
subscriptionType,
} from '../../../src'
import { mockStream } from '../../__helpers'
Expand Down Expand Up @@ -154,7 +155,7 @@ export const Mutation = mutationType({
export const Subscription2 = extendType({
type: 'Subscription',
definition(t) {
t.boolean('someBooleanFromExtend', {
t.boolean('someBooleanFromExtendType', {
subscribe() {
return mockStream(10, true, (b) => b)
},
Expand All @@ -165,6 +166,17 @@ export const Subscription2 = extendType({
},
})

export const Subscription3 = subscriptionField((t) => {
t.boolean('someBooleanFromSubscriptionField', {
subscribe() {
return mockStream(10, true, (b) => b)
},
resolve: (event: boolean) => {
return event
},
})
})

export const Subscription = subscriptionType({
definition(t) {
// lists
Expand Down
129 changes: 129 additions & 0 deletions tests/integrations/kitchenSink/__schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
### This file was generated by Nexus Schema
### Do not make changes to this file directly

interface I {
hello: String
}

type Mutation {
createUser(firstName: String, lastName: String): User
}

"""
No-Op scalar for testing purposes only
"""
scalar MyCustomScalar

type OfI implements I {
hello: String
}

type OfI2 implements I {
hello: String
}

"""
PageInfo cursor, as defined in https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
"""
type PageInfo {
"""
The cursor corresponding to the last nodes in edges. Null if the connection is empty.
"""
endCursor: String

"""
Used to indicate whether more edges exist following the set defined by the clients arguments.
"""
hasNextPage: Boolean!

"""
Used to indicate whether more edges exist prior to the set defined by the clients arguments.
"""
hasPreviousPage: Boolean!

"""
The cursor corresponding to the first nodes in edges. Null if the connection is empty.
"""
startCursor: String
}

type Post {
body: String
title: String
}

type PostConnection {
"""
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
"""
edges: [PostEdge]

"""
https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
"""
pageInfo: PageInfo!
}

type PostEdge {
"""
https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor
"""
cursor: String!
delta: Int

"""
https://facebook.github.io/relay/graphql/connections.htm#sec-Node
"""
node: Post
}

input PostSearchInput {
body: String
title: String
}

type Query {
customScalar: MyCustomScalar
foo: String
searchPosts(input: PostSearchInput): [Post]
user(id: ID): User
}

type Subscription {
someBoolean: Boolean
someBooleanFromExtendType: Boolean
someBooleanFromSubscriptionField: Boolean
someField: Int
someFields: [Int]
someFloat: Float
someID: ID
someInt: Int
someInts: [Int]
someString: String
}

type User {
firstName: String
lastName: String
posts(
"""
Returns the elements in the list that come after the specified cursor
"""
after: String

"""
Returns the elements in the list that come before the specified cursor
"""
before: String

"""
Returns the first n elements from the list.
"""
first: Int

"""
Returns the last n elements from the list.
"""
last: Int
): PostConnection
}
6 changes: 4 additions & 2 deletions tests/integrations/kitchenSink/__typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export interface NexusGenFieldTypes {
Subscription: {
// field return type
someBoolean: boolean | null // Boolean
someBooleanFromExtend: boolean | null // Boolean
someBooleanFromExtendType: boolean | null // Boolean
someBooleanFromSubscriptionField: boolean | null // Boolean
someField: number | null // Int
someFields: Array<number | null> | null // [Int]
someFloat: number | null // Float
Expand Down Expand Up @@ -234,7 +235,8 @@ export interface NexusGenFieldTypeNames {
Subscription: {
// field return type name
someBoolean: 'Boolean'
someBooleanFromExtend: 'Boolean'
someBooleanFromExtendType: 'Boolean'
someBooleanFromSubscriptionField: 'Boolean'
someField: 'Int'
someFields: 'Int'
someFloat: 'Float'
Expand Down
Loading