Skip to content

Commit

Permalink
fix: #588, #384 non-null list items & connection nullability (#598)
Browse files Browse the repository at this point in the history
Fixes #588 by ensuring that list items follow `nonNullDefaults`, which was lost in the #508 refactor

Fixes #384 by removing explicit nullability config from the connection `edges` definition, and also allowing `nonNullDefaults` to be supplied for the connection "types" generated by the creation of a connection field. Allows you to configure both globally in the connection plugin field config, and when the connection field is defined.
  • Loading branch information
tgriesser committed Nov 2, 2020
1 parent 2de6f89 commit 0064dc9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/builder.ts
Expand Up @@ -1217,15 +1217,22 @@ export class SchemaBuilder {
isNonNull: boolean
): T {
if (list) {
type = this.decorateList(type, list)
type = this.decorateList(type, list, isNonNull)
}
return (isNonNull ? GraphQLNonNull(type) : type) as T
}

protected decorateList<T extends GraphQLOutputType | GraphQLInputType>(type: T, list: true | boolean[]): T {
protected decorateList<T extends GraphQLOutputType | GraphQLInputType>(
type: T,
list: true | boolean[],
isNonNull: boolean
): T {
let finalType = type
if (!Array.isArray(list)) {
return GraphQLList(type) as T
if (isNonNull) {
finalType = GraphQLNonNull(finalType) as T
}
return GraphQLList(finalType) as T
}
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
Expand Down
17 changes: 14 additions & 3 deletions src/plugins/connectionPlugin.ts
Expand Up @@ -23,6 +23,7 @@ import {
pathToArray,
printedGenTypingImport,
} from '../utils'
import { NonNullConfig } from '../definitions/_types'

export interface ConnectionPluginConfig {
/**
Expand Down Expand Up @@ -126,6 +127,11 @@ export interface ConnectionPluginConfig {
* direct dependency at the application level.
*/
nexusSchemaImportId?: string
/**
* Configures the default "nonNullDefaults" settings for any connection types
* created globally by this config / connection field.
*/
nonNullDefaults?: NonNullConfig
}

// Extract the node value from the connection for a given field.
Expand Down Expand Up @@ -201,6 +207,11 @@ export type ConnectionFieldConfig<TypeName extends string = any, FieldName exten
* so as not to conflict with any non-extended connections.
*/
extendEdge?: (def: ObjectDefinitionBlock<any>) => void
/**
* Configures the default "nonNullDefaults" for connection type generated
* for this connection
*/
nonNullDefaults?: NonNullConfig
} & (
| {
/**
Expand Down Expand Up @@ -401,11 +412,9 @@ export const connectionPlugin = (connectionPluginConfig?: ConnectionPluginConfig
objectType({
name: connectionName as any,
definition(t2) {
t2.field('edges', {
t2.list.field('edges', {
type: edgeName as any,
description: `https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types`,
nullable: true,
list: [false],
})
t2.field('pageInfo', {
type: 'PageInfo' as any,
Expand All @@ -430,6 +439,7 @@ export const connectionPlugin = (connectionPluginConfig?: ConnectionPluginConfig
fieldConfig.extendConnection(t2)
}
},
nonNullDefaults: fieldConfig.nonNullDefaults ?? pluginConfig.nonNullDefaults,
})
)
}
Expand Down Expand Up @@ -460,6 +470,7 @@ export const connectionPlugin = (connectionPluginConfig?: ConnectionPluginConfig
fieldConfig.extendEdge(t2)
}
},
nonNullDefaults: fieldConfig.nonNullDefaults ?? pluginConfig.nonNullDefaults,
})
)
}
Expand Down
4 changes: 4 additions & 0 deletions tests/__snapshots__/nonNullDefaults.spec.ts.snap
Expand Up @@ -3,27 +3,31 @@
exports[`nonNullDefaults false/false on schema 1`] = `
"type Query {
test(test: Int): Boolean
stringList: [String]
}
"
`;

exports[`nonNullDefaults false/false on type 1`] = `
"type Query {
test(test: Int): Boolean
stringList: [String]
}
"
`;

exports[`nonNullDefaults true/true on schema 1`] = `
"type Query {
test(test: Int!): Boolean!
stringList: [String!]!
}
"
`;

exports[`nonNullDefaults true/true on type 1`] = `
"type Query {
test(test: Int!): Boolean!
stringList: [String!]!
}
"
`;
3 changes: 3 additions & 0 deletions tests/nonNullDefaults.spec.ts
Expand Up @@ -49,6 +49,9 @@ function makeQuery(config?: Partial<core.NexusObjectTypeConfig<string>>) {
test: intArg(),
},
})
t.list.field('stringList', {
type: 'String',
})
},
})
}
2 changes: 1 addition & 1 deletion tests/typegenPrinter.spec.ts
Expand Up @@ -15,7 +15,7 @@ describe('typegenPrinter', () => {
},
shouldGenerateArtifacts: true,
types: [buildSchema(EXAMPLE_SDL)],
prettierConfig: path.join(__dirname, '../package.json'),
prettierConfig: path.join(__dirname, '../.prettierrc'),
}) as core.NexusGraphQLSchema
metadata = new TypegenMetadata({
outputs: {
Expand Down

0 comments on commit 0064dc9

Please sign in to comment.