Skip to content

Commit 24a78e8

Browse files
authored
fix: #670 - explicit nullability for connectionPlugin (#671)
1 parent f49d0b0 commit 24a78e8

File tree

3 files changed

+107
-19
lines changed

3 files changed

+107
-19
lines changed

src/plugins/connectionPlugin.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql'
2-
import { arg, ArgsRecord, intArg } from '../definitions/args'
2+
import { ArgsRecord, intArg, stringArg } from '../definitions/args'
33
import { CommonFieldConfig, FieldOutConfig } from '../definitions/definitionBlocks'
44
import { nonNull } from '../definitions/nonNull'
55
import { ObjectDefinitionBlock, objectType } from '../definitions/objectType'
@@ -272,14 +272,10 @@ export type ConnectionFieldConfig<TypeName extends string = any, FieldName exten
272272
NexusGenPluginFieldConfig<TypeName, FieldName>
273273

274274
const ForwardPaginateArgs = {
275-
first: arg({
276-
type: 'Int',
277-
description: 'Returns the first n elements from the list.',
278-
}),
279-
after: arg({
280-
type: 'String',
281-
description: 'Returns the elements in the list that come after the specified cursor',
282-
}),
275+
first: nullable(intArg({ description: 'Returns the first n elements from the list.' })),
276+
after: nullable(
277+
stringArg({ description: 'Returns the elements in the list that come after the specified cursor' })
278+
),
283279
}
284280

285281
const ForwardOnlyStrictArgs = {
@@ -288,14 +284,10 @@ const ForwardOnlyStrictArgs = {
288284
}
289285

290286
const BackwardPaginateArgs = {
291-
last: arg({
292-
type: 'Int',
293-
description: 'Returns the last n elements from the list.',
294-
}),
295-
before: arg({
296-
type: 'String',
297-
description: 'Returns the elements in the list that come before the specified cursor',
298-
}),
287+
last: nullable(intArg({ description: 'Returns the last n elements from the list.' })),
288+
before: nullable(
289+
stringArg({ description: 'Returns the elements in the list that come before the specified cursor' })
290+
),
299291
}
300292

301293
const BackwardOnlyStrictArgs = {
@@ -503,11 +495,11 @@ export const connectionPlugin = (connectionPluginConfig?: ConnectionPluginConfig
503495
type: 'Boolean',
504496
description: `Used to indicate whether more edges exist prior to the set defined by the clients arguments.`,
505497
})
506-
t2.field('startCursor', {
498+
t2.nullable.field('startCursor', {
507499
type: 'String',
508500
description: `The cursor corresponding to the first nodes in edges. Null if the connection is empty.`,
509501
})
510-
t2.field('endCursor', {
502+
t2.nullable.field('endCursor', {
511503
type: 'String',
512504
description: `The cursor corresponding to the last nodes in edges. Null if the connection is empty.`,
513505
})

tests/plugins/__snapshots__/connectionPlugin.spec.ts.snap

+69
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,75 @@ type PageInfo {
231231
}"
232232
`;
233233

234+
exports[`field level configuration #670 should explicitly state nullability for connectionPlugin args & fields 1`] = `
235+
"type Query {
236+
users(
237+
\\"\\"\\"Returns the first n elements from the list.\\"\\"\\"
238+
first: Int
239+
240+
\\"\\"\\"Returns the elements in the list that come after the specified cursor\\"\\"\\"
241+
after: String
242+
243+
\\"\\"\\"Returns the last n elements from the list.\\"\\"\\"
244+
last: Int
245+
246+
\\"\\"\\"Returns the elements in the list that come before the specified cursor\\"\\"\\"
247+
before: String
248+
): UserConnection!
249+
}
250+
251+
type UserConnection {
252+
\\"\\"\\"
253+
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
254+
\\"\\"\\"
255+
edges: [UserEdge!]!
256+
257+
\\"\\"\\"
258+
https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
259+
\\"\\"\\"
260+
pageInfo: PageInfo!
261+
}
262+
263+
type UserEdge {
264+
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor\\"\\"\\"
265+
cursor: String!
266+
267+
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Node\\"\\"\\"
268+
node: User!
269+
}
270+
271+
\\"\\"\\"
272+
PageInfo cursor, as defined in https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
273+
\\"\\"\\"
274+
type PageInfo {
275+
\\"\\"\\"
276+
Used to indicate whether more edges exist following the set defined by the clients arguments.
277+
\\"\\"\\"
278+
hasNextPage: Boolean!
279+
280+
\\"\\"\\"
281+
Used to indicate whether more edges exist prior to the set defined by the clients arguments.
282+
\\"\\"\\"
283+
hasPreviousPage: Boolean!
284+
285+
\\"\\"\\"
286+
The cursor corresponding to the first nodes in edges. Null if the connection is empty.
287+
\\"\\"\\"
288+
startCursor: String
289+
290+
\\"\\"\\"
291+
The cursor corresponding to the last nodes in edges. Null if the connection is empty.
292+
\\"\\"\\"
293+
endCursor: String
294+
}
295+
296+
type User {
297+
id: ID!
298+
name: String!
299+
}
300+
"
301+
`;
302+
234303
exports[`field level configuration can configure the connection per-instance 1`] = `
235304
"type QueryUsers_Connection {
236305
\\"\\"\\"

tests/plugins/connectionPlugin.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -829,4 +829,31 @@ describe('field level configuration', () => {
829829
const regExp = /interface NexusGenCustomOutputMethods(?:.*) {((.|\n)*?)}/
830830
expect(regExp.exec(tsTypes)?.[1]).toMatchSnapshot()
831831
})
832+
833+
it('#670 should explicitly state nullability for connectionPlugin args & fields', async () => {
834+
const { schema } = await generateSchema.withArtifacts({
835+
outputs: false,
836+
types: [
837+
objectType({
838+
name: 'Query',
839+
definition(t) {
840+
// @ts-ignore
841+
t.connectionField('users', {
842+
type: User,
843+
nodes(root: any, args: any, ctx: any, info: any) {
844+
return userNodes
845+
},
846+
})
847+
},
848+
}),
849+
],
850+
plugins: [connectionPlugin()],
851+
nonNullDefaults: {
852+
input: true,
853+
output: true,
854+
},
855+
})
856+
857+
expect(printSchema(schema)).toMatchSnapshot()
858+
})
832859
})

0 commit comments

Comments
 (0)