Skip to content

Commit 2c84ee1

Browse files
committed
fix: make Node interface global
1 parent 7772f7d commit 2c84ee1

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/composeWithRelay.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import { getNodeFieldConfig } from './nodeFieldConfig';
1111
// for futher type resolving via NodeInterface.resolveType method
1212
export const TypeMapForRelayNode = {};
1313

14-
export function composeWithRelay<TSource, TContext>(
15-
tc: ObjectTypeComposer<TSource, TContext>
16-
): ObjectTypeComposer<TSource, TContext> {
17-
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
14+
export function composeWithRelay<TContext>(
15+
tc: ObjectTypeComposer<any, TContext>
16+
): ObjectTypeComposer<any, TContext> {
17+
if (!(tc instanceof ObjectTypeComposer)) {
1818
throw new Error('You should provide ObjectTypeComposer instance to composeWithRelay method');
1919
}
2020

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
import { composeWithRelay, TypeMapForRelayNode } from './composeWithRelay';
44
import { fromGlobalId, toGlobalId } from './globalId';
5-
import { getNodeInterface } from './nodeInterface';
5+
import { getNodeInterface, NodeInterface } from './nodeInterface';
66

77
export default composeWithRelay;
8-
export { composeWithRelay, getNodeInterface, TypeMapForRelayNode, fromGlobalId, toGlobalId };
8+
export {
9+
composeWithRelay,
10+
getNodeInterface,
11+
TypeMapForRelayNode,
12+
NodeInterface,
13+
fromGlobalId,
14+
toGlobalId,
15+
};

src/nodeInterface.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
import { InterfaceTypeComposer, type SchemaComposer } from 'graphql-compose';
44

5+
const NodeTC = InterfaceTypeComposer.createTemp({
6+
name: 'Node',
7+
description: 'An object, that can be fetched by the globally unique ID among all types.',
8+
fields: {
9+
id: {
10+
type: 'ID!',
11+
description: 'The globally unique ID among all types.',
12+
},
13+
},
14+
resolveType: payload => {
15+
// `payload.__nodeType` was added to payload via nodeFieldConfig.resolve
16+
return payload.__nodeType ? payload.__nodeType : null;
17+
},
18+
});
19+
20+
export const NodeInterface = NodeTC.getType();
21+
522
export function getNodeInterface<TContex>(
623
sc: SchemaComposer<TContex>
724
): InterfaceTypeComposer<any, TContex> {
825
if (sc.hasInstance('Node', InterfaceTypeComposer)) {
926
return (sc.get('Node'): any);
1027
}
11-
12-
const NodeInterface = InterfaceTypeComposer.create(
13-
{
14-
name: 'Node',
15-
description: 'An object, that can be fetched by the globally unique ID among all types.',
16-
fields: {
17-
id: {
18-
type: 'ID!',
19-
description: 'The globally unique ID among all types.',
20-
},
21-
},
22-
resolveType: payload => {
23-
// `payload.__nodeType` was added to payload via nodeFieldConfig.resolve
24-
return payload.__nodeType ? payload.__nodeType : null;
25-
},
26-
},
27-
sc
28-
);
29-
30-
return NodeInterface;
28+
sc.set('Node', (NodeTC: any));
29+
return (NodeInterface: any);
3130
}

0 commit comments

Comments
 (0)