From a4bb75a0a71df26f47855a1e2028be11d6fa0014 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 31 May 2021 13:06:27 +0300 Subject: [PATCH] connection: allow `nodeType` to any named type with or without non-null Fixes #160 #167 --- src/connection/__tests__/connection-test.js | 9 +++++---- src/connection/connection.d.ts | 8 +++++--- src/connection/connection.js | 12 +++++++----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/connection/__tests__/connection-test.js b/src/connection/__tests__/connection-test.js index 54548ed..a71b68b 100644 --- a/src/connection/__tests__/connection-test.js +++ b/src/connection/__tests__/connection-test.js @@ -2,6 +2,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; import { GraphQLInt, + GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, @@ -54,7 +55,7 @@ const userType = new GraphQLObjectType({ const { connectionType: friendConnection } = connectionDefinitions({ name: 'Friend', - nodeType: userType, + nodeType: new GraphQLNonNull(userType), resolveNode: (edge) => allUsers[edge.node], edgeFields: () => ({ friendshipTime: { @@ -71,7 +72,7 @@ const { connectionType: friendConnection } = connectionDefinitions({ }); const { connectionType: userConnection } = connectionDefinitions({ - nodeType: userType, + nodeType: new GraphQLNonNull(userType), resolveNode: (edge) => allUsers[edge.node], }); @@ -246,7 +247,7 @@ describe('connectionDefinition()', () => { """An edge in a connection.""" type FriendEdge { """The item at the end of the edge""" - node: User + node: User! """A cursor for use in pagination""" cursor: String! @@ -265,7 +266,7 @@ describe('connectionDefinition()', () => { """An edge in a connection.""" type UserEdge { """The item at the end of the edge""" - node: User + node: User! """A cursor for use in pagination""" cursor: String! diff --git a/src/connection/connection.d.ts b/src/connection/connection.d.ts index 8a57e3a..b6a0e45 100644 --- a/src/connection/connection.d.ts +++ b/src/connection/connection.d.ts @@ -1,9 +1,11 @@ import type { + GraphQLNonNull, + GraphQLNamedType, + GraphQLScalarType, + GraphQLObjectType, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFieldResolver, - GraphQLObjectType, - GraphQLScalarType, Thunk, } from 'graphql'; @@ -58,7 +60,7 @@ export interface ConnectionArguments { export interface ConnectionConfig { name?: string; - nodeType: GraphQLObjectType; + nodeType: GraphQLNamedType | GraphQLNonNull; resolveNode?: GraphQLFieldResolver; resolveCursor?: GraphQLFieldResolver; edgeFields?: Thunk>; diff --git a/src/connection/connection.js b/src/connection/connection.js index b988736..13ce9c4 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -1,13 +1,15 @@ import { - GraphQLBoolean, - GraphQLInt, - GraphQLNonNull, GraphQLList, + GraphQLNonNull, GraphQLObjectType, + GraphQLInt, GraphQLString, + GraphQLBoolean, + getNamedType, } from 'graphql'; import type { + GraphQLNamedType, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFieldResolver, @@ -73,7 +75,7 @@ export type ConnectionArguments = { type ConnectionConfig = {| name?: string, - nodeType: GraphQLObjectType, + nodeType: GraphQLNamedType | GraphQLNonNull, resolveNode?: GraphQLFieldResolver, resolveCursor?: GraphQLFieldResolver, edgeFields?: Thunk>, @@ -100,7 +102,7 @@ export function connectionDefinitions( config: ConnectionConfig, ): GraphQLConnectionDefinitions { const { nodeType } = config; - const name = config.name ?? nodeType.name; + const name = config.name ?? getNamedType(nodeType).name; const edgeFields = config.edgeFields ?? {}; const connectionFields = config.connectionFields ?? {}; const resolveNode = config.resolveNode;