Skip to content

Commit

Permalink
connection: allow nodeType to any named type with or without non-null
Browse files Browse the repository at this point in the history
Fixes #160 #167
  • Loading branch information
IvanGoncharov committed May 31, 2021
1 parent 02a5a27 commit a4bb75a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/connection/__tests__/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';
import {
GraphQLInt,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
Expand Down Expand Up @@ -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: {
Expand All @@ -71,7 +72,7 @@ const { connectionType: friendConnection } = connectionDefinitions({
});

const { connectionType: userConnection } = connectionDefinitions({
nodeType: userType,
nodeType: new GraphQLNonNull(userType),
resolveNode: (edge) => allUsers[edge.node],
});

Expand Down Expand Up @@ -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!
Expand All @@ -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!
Expand Down
8 changes: 5 additions & 3 deletions src/connection/connection.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {
GraphQLNonNull,
GraphQLNamedType,
GraphQLScalarType,
GraphQLObjectType,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldResolver,
GraphQLObjectType,
GraphQLScalarType,
Thunk,
} from 'graphql';

Expand Down Expand Up @@ -58,7 +60,7 @@ export interface ConnectionArguments {

export interface ConnectionConfig {
name?: string;
nodeType: GraphQLObjectType;
nodeType: GraphQLNamedType | GraphQLNonNull<GraphQLNamedType>;
resolveNode?: GraphQLFieldResolver<any, any>;
resolveCursor?: GraphQLFieldResolver<any, any>;
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>>;
Expand Down
12 changes: 7 additions & 5 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
GraphQLBoolean,
GraphQLInt,
GraphQLNonNull,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLBoolean,
getNamedType,
} from 'graphql';

import type {
GraphQLNamedType,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldResolver,
Expand Down Expand Up @@ -73,7 +75,7 @@ export type ConnectionArguments = {

type ConnectionConfig = {|
name?: string,
nodeType: GraphQLObjectType,
nodeType: GraphQLNamedType | GraphQLNonNull<GraphQLNamedType>,
resolveNode?: GraphQLFieldResolver<any, any>,
resolveCursor?: GraphQLFieldResolver<any, any>,
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>>,
Expand All @@ -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;
Expand Down

0 comments on commit a4bb75a

Please sign in to comment.