Skip to content

Commit

Permalink
fix: #670 - explicit nullability for connectionPlugin (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Nov 25, 2020
1 parent f49d0b0 commit 24a78e8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/plugins/connectionPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql'
import { arg, ArgsRecord, intArg } from '../definitions/args'
import { ArgsRecord, intArg, stringArg } from '../definitions/args'
import { CommonFieldConfig, FieldOutConfig } from '../definitions/definitionBlocks'
import { nonNull } from '../definitions/nonNull'
import { ObjectDefinitionBlock, objectType } from '../definitions/objectType'
Expand Down Expand Up @@ -272,14 +272,10 @@ export type ConnectionFieldConfig<TypeName extends string = any, FieldName exten
NexusGenPluginFieldConfig<TypeName, FieldName>

const ForwardPaginateArgs = {
first: arg({
type: 'Int',
description: 'Returns the first n elements from the list.',
}),
after: arg({
type: 'String',
description: 'Returns the elements in the list that come after the specified cursor',
}),
first: nullable(intArg({ description: 'Returns the first n elements from the list.' })),
after: nullable(
stringArg({ description: 'Returns the elements in the list that come after the specified cursor' })
),
}

const ForwardOnlyStrictArgs = {
Expand All @@ -288,14 +284,10 @@ const ForwardOnlyStrictArgs = {
}

const BackwardPaginateArgs = {
last: arg({
type: 'Int',
description: 'Returns the last n elements from the list.',
}),
before: arg({
type: 'String',
description: 'Returns the elements in the list that come before the specified cursor',
}),
last: nullable(intArg({ description: 'Returns the last n elements from the list.' })),
before: nullable(
stringArg({ description: 'Returns the elements in the list that come before the specified cursor' })
),
}

const BackwardOnlyStrictArgs = {
Expand Down Expand Up @@ -503,11 +495,11 @@ export const connectionPlugin = (connectionPluginConfig?: ConnectionPluginConfig
type: 'Boolean',
description: `Used to indicate whether more edges exist prior to the set defined by the clients arguments.`,
})
t2.field('startCursor', {
t2.nullable.field('startCursor', {
type: 'String',
description: `The cursor corresponding to the first nodes in edges. Null if the connection is empty.`,
})
t2.field('endCursor', {
t2.nullable.field('endCursor', {
type: 'String',
description: `The cursor corresponding to the last nodes in edges. Null if the connection is empty.`,
})
Expand Down
69 changes: 69 additions & 0 deletions tests/plugins/__snapshots__/connectionPlugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,75 @@ type PageInfo {
}"
`;

exports[`field level configuration #670 should explicitly state nullability for connectionPlugin args & fields 1`] = `
"type Query {
users(
\\"\\"\\"Returns the first n elements from the list.\\"\\"\\"
first: Int
\\"\\"\\"Returns the elements in the list that come after the specified cursor\\"\\"\\"
after: String
\\"\\"\\"Returns the last n elements from the list.\\"\\"\\"
last: Int
\\"\\"\\"Returns the elements in the list that come before the specified cursor\\"\\"\\"
before: String
): UserConnection!
}
type UserConnection {
\\"\\"\\"
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
\\"\\"\\"
edges: [UserEdge!]!
\\"\\"\\"
https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
\\"\\"\\"
pageInfo: PageInfo!
}
type UserEdge {
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor\\"\\"\\"
cursor: String!
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Node\\"\\"\\"
node: User!
}
\\"\\"\\"
PageInfo cursor, as defined in https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
\\"\\"\\"
type PageInfo {
\\"\\"\\"
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
\\"\\"\\"
The cursor corresponding to the last nodes in edges. Null if the connection is empty.
\\"\\"\\"
endCursor: String
}
type User {
id: ID!
name: String!
}
"
`;

exports[`field level configuration can configure the connection per-instance 1`] = `
"type QueryUsers_Connection {
\\"\\"\\"
Expand Down
27 changes: 27 additions & 0 deletions tests/plugins/connectionPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,31 @@ describe('field level configuration', () => {
const regExp = /interface NexusGenCustomOutputMethods(?:.*) {((.|\n)*?)}/
expect(regExp.exec(tsTypes)?.[1]).toMatchSnapshot()
})

it('#670 should explicitly state nullability for connectionPlugin args & fields', async () => {
const { schema } = await generateSchema.withArtifacts({
outputs: false,
types: [
objectType({
name: 'Query',
definition(t) {
// @ts-ignore
t.connectionField('users', {
type: User,
nodes(root: any, args: any, ctx: any, info: any) {
return userNodes
},
})
},
}),
],
plugins: [connectionPlugin()],
nonNullDefaults: {
input: true,
output: true,
},
})

expect(printSchema(schema)).toMatchSnapshot()
})
})

0 comments on commit 24a78e8

Please sign in to comment.