Skip to content

Commit 86ddc88

Browse files
committed
fix(ConnectionType): For same GraphQLObject prepareConnectionType returns the same ConnectionType
1 parent f93c72b commit 86ddc88

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/__tests__/composeWithConnection-test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect } from 'chai';
44
import {
55
graphql,
66
GraphQLSchema,
7+
GraphQLList,
78
} from 'graphql';
89
import { TypeComposer } from 'graphql-compose';
910
import { composeWithConnection } from '../composeWithConnection';
@@ -51,9 +52,19 @@ describe('composeWithRelay', () => {
5152
});
5253
});
5354

54-
describe('TypeComposer props', () => {
55-
it('should has `connection` resolver', () => {
56-
expect(userComposer.getResolver('connection')).to.be.ok;
55+
describe('check `connection` resolver props', () => {
56+
const rsv = userComposer.getResolver('connection');
57+
const type = rsv.getOutputType();
58+
const tc = new TypeComposer(type);
59+
60+
it('should exists', () => {
61+
expect(rsv).to.be.ok;
62+
});
63+
64+
it('should has ConnectionType as outputType', () => {
65+
expect(type).to.be.ok;
66+
expect(tc.getFieldNames()).to.have.members(['count', 'pageInfo', 'edges']);
67+
expect(tc.getFieldType('edges')).instanceof(GraphQLList);
5768
});
5869
});
5970

src/types/__tests__/connectionType-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ describe('types/connectionType.js', () => {
4242
const edgeType = prepareEdgeType(userTypeComposer);
4343
expect(edgeType).property('ofType').equals(userTypeComposer.getType());
4444
});
45+
46+
it('should return same type for same Type in TypeComposer', () => {
47+
const t1 = prepareEdgeType(userTypeComposer);
48+
const t2 = prepareEdgeType(userTypeComposer);
49+
expect(t1).equals(t2);
50+
});
4551
});
4652

4753
describe('prepareConnectionType()', () => {
@@ -82,5 +88,11 @@ describe('types/connectionType.js', () => {
8288
const connectionType = prepareConnectionType(userTypeComposer);
8389
expect(connectionType).property('ofType').equals(userTypeComposer.getType());
8490
});
91+
92+
it('should return same type for same Type in TypeComposer', () => {
93+
const t1 = prepareConnectionType(userTypeComposer);
94+
const t2 = prepareConnectionType(userTypeComposer);
95+
expect(t1).equals(t2);
96+
});
8597
});
8698
});

src/types/connectionType.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ import GraphQLConnectionCursor from './cursorType';
1414

1515
import PageInfoType from './pageInfoType';
1616

17+
const cachedConnectionTypes = new WeakMap;
18+
const cachedEdgeTypes = new WeakMap;
1719

1820
export function prepareEdgeType(typeComposer: TypeComposer): GraphQLObjectType {
1921
const name = `${typeComposer.getTypeName()}Edge`;
22+
const type = typeComposer.getType();
23+
24+
if (cachedEdgeTypes.has(type)) {
25+
return cachedEdgeTypes.get(type);
26+
}
2027

2128
const edgeType = new GraphQLObjectType({
2229
name,
@@ -32,13 +39,20 @@ export function prepareEdgeType(typeComposer: TypeComposer): GraphQLObjectType {
3239
},
3340
}),
3441
});
35-
edgeType.ofType = typeComposer.getType();
42+
edgeType.ofType = type;
43+
44+
cachedEdgeTypes.set(type, edgeType);
3645
return edgeType;
3746
}
3847

3948

4049
export function prepareConnectionType(typeComposer: TypeComposer): GraphQLObjectType {
4150
const name = `${typeComposer.getTypeName()}Connection`;
51+
const type = typeComposer.getType();
52+
53+
if (cachedConnectionTypes.has(type)) {
54+
return cachedConnectionTypes.get(type);
55+
}
4256

4357
const connectionType = new GraphQLObjectType({
4458
name,
@@ -58,6 +72,8 @@ export function prepareConnectionType(typeComposer: TypeComposer): GraphQLObject
5872
},
5973
}),
6074
});
61-
connectionType.ofType = typeComposer.getType();
75+
connectionType.ofType = type;
76+
77+
cachedConnectionTypes.set(type, connectionType);
6278
return connectionType;
6379
}

0 commit comments

Comments
 (0)