Skip to content

Commit d72b351

Browse files
committed
add nodes field
1 parent 2a01b29 commit d72b351

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

src/node/__tests__/node.js

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const photoData = {
4747
},
4848
};
4949

50-
const { nodeField, nodeInterface } = nodeDefinitions(
50+
const { nodeField, nodesField, nodeInterface } = nodeDefinitions(
5151
(id, context, info) => {
5252
expect(info.schema).to.equal(schema);
5353
if (userData[id]) {
@@ -96,7 +96,8 @@ const photoType = new GraphQLObjectType({
9696
const queryType = new GraphQLObjectType({
9797
name: 'Query',
9898
fields: () => ({
99-
node: nodeField
99+
node: nodeField,
100+
nodes: nodesField
100101
})
101102
});
102103

@@ -123,6 +124,27 @@ describe('Node interface and fields', () => {
123124
});
124125
});
125126

127+
it('gets the correct IDs for users', async () => {
128+
const query = `{
129+
nodes(ids: ["1", "2"]) {
130+
id
131+
}
132+
}`;
133+
134+
expect(await graphql(schema, query)).to.deep.equal({
135+
data: {
136+
nodes: [
137+
{
138+
id: '1'
139+
},
140+
{
141+
id: '2'
142+
}
143+
]
144+
}
145+
});
146+
});
147+
126148
it('gets the correct ID for photos', async () => {
127149
const query = `{
128150
node(id: "4") {
@@ -139,6 +161,48 @@ describe('Node interface and fields', () => {
139161
});
140162
});
141163

164+
it('gets the correct IDs for photos', async () => {
165+
const query = `{
166+
nodes(ids: ["3", "4"]) {
167+
id
168+
}
169+
}`;
170+
171+
expect(await graphql(schema, query)).to.deep.equal({
172+
data: {
173+
nodes: [
174+
{
175+
id: '3'
176+
},
177+
{
178+
id: '4'
179+
}
180+
]
181+
}
182+
});
183+
});
184+
185+
it('gets the correct IDs for multiple types', async () => {
186+
const query = `{
187+
nodes(ids: ["1", "3"]) {
188+
id
189+
}
190+
}`;
191+
192+
expect(await graphql(schema, query)).to.deep.equal({
193+
data: {
194+
nodes: [
195+
{
196+
id: '1'
197+
},
198+
{
199+
id: '3'
200+
}
201+
]
202+
}
203+
});
204+
});
205+
142206
it('gets the correct name for users', async () => {
143207
const query = `{
144208
node(id: "1") {
@@ -247,6 +311,25 @@ describe('Node interface and fields', () => {
247311
}
248312
});
249313
});
314+
315+
it('returns nulls for bad IDs', async () => {
316+
const query = `{
317+
nodes(ids: ["3", "5"]) {
318+
id
319+
}
320+
}`;
321+
322+
return expect(await graphql(schema, query)).to.deep.equal({
323+
data: {
324+
nodes: [
325+
{
326+
id: '3'
327+
},
328+
null
329+
]
330+
}
331+
});
332+
});
250333
});
251334

252335
describe('introspection', () => {
@@ -290,7 +373,7 @@ describe('Node interface and fields', () => {
290373
});
291374
});
292375

293-
it('has correct node root field', async () => {
376+
it('has correct node and nodes root fields', async () => {
294377
const query = `{
295378
__schema {
296379
queryType {
@@ -338,6 +421,25 @@ describe('Node interface and fields', () => {
338421
}
339422
}
340423
]
424+
},
425+
{
426+
name: 'nodes',
427+
type: {
428+
name: null,
429+
kind: 'NON_NULL'
430+
},
431+
args: [
432+
{
433+
name: 'ids',
434+
type: {
435+
kind: 'NON_NULL',
436+
ofType: {
437+
name: null,
438+
kind: 'LIST'
439+
}
440+
}
441+
}
442+
]
341443
}
342444
]
343445
}

src/node/node.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import {
1212
GraphQLInterfaceType,
13+
GraphQLList,
1314
GraphQLNonNull,
1415
GraphQLID
1516
} from 'graphql';
@@ -27,7 +28,8 @@ import {
2728

2829
type GraphQLNodeDefinitions = {
2930
nodeInterface: GraphQLInterfaceType,
30-
nodeField: GraphQLFieldConfig<*, *>
31+
nodeField: GraphQLFieldConfig<*, *>,
32+
nodesField: GraphQLFieldConfig<*, *>
3133
};
3234

3335
/**
@@ -69,7 +71,37 @@ export function nodeDefinitions<TContext>(
6971
resolve: (obj, { id }, context, info) => idFetcher(id, context, info),
7072
};
7173

72-
return { nodeInterface, nodeField };
74+
const nodesField = {
75+
name: 'nodes',
76+
description: 'Fetches objects given their IDs',
77+
type: new GraphQLNonNull(
78+
new GraphQLList(
79+
nodeInterface
80+
)
81+
),
82+
args: {
83+
ids: {
84+
type: new GraphQLNonNull(
85+
new GraphQLList(
86+
new GraphQLNonNull(
87+
GraphQLID
88+
)
89+
)
90+
),
91+
description: 'The IDs of objects'
92+
}
93+
},
94+
resolve: (obj, { ids }, context, info) =>
95+
Promise.all(
96+
ids.map(id =>
97+
Promise.resolve(
98+
idFetcher(id, context, info)
99+
)
100+
)
101+
)
102+
};
103+
104+
return { nodeInterface, nodeField, nodesField };
73105
}
74106

75107
type ResolvedGlobalId = {

0 commit comments

Comments
 (0)