Skip to content

Commit b152e10

Browse files
committed
fix(doesQueryContain): support recursive types
fix #2
1 parent cfbd6b9 commit b152e10

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

src/doesQueryContain.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export default function doesQueryContain(
3030

3131
function doesNodeContain(node: Node, data: any, type: Type): boolean {
3232
if (type === targetType) {
33-
if (!ids) return true
34-
return data && ids.has(data[idField])
33+
if (!ids || (data && ids.has(data[idField]))) return true
3534
}
3635
if (!type.name) return false
3736
const ancestorEntry = potentialAncestors.get(type)

test/doesQueryContain.js

+150
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,154 @@ describe(`doesQueryContain`, function() {
155155
expect(doesQueryContain(document, types, 'Device', data, new Set([5]))).to
156156
.be.false
157157
})
158+
it(`recursive type test`, function() {
159+
const document = gql`
160+
{
161+
MetadataItem(tag: "foo/bar") {
162+
tag
163+
Parent {
164+
tag
165+
Parent {
166+
tag
167+
}
168+
}
169+
}
170+
}
171+
`
172+
const data = {
173+
MetadataItem: {
174+
tag: 'foo/bar',
175+
__typename: 'MetadataItem',
176+
Parent: {
177+
tag: 'foo',
178+
__typename: 'MetadataItem',
179+
Parent: null,
180+
},
181+
},
182+
}
183+
expect(doesQueryContain(document, types, 'MetadataItem')).to.be.true
184+
expect(
185+
doesQueryContain(
186+
document,
187+
types,
188+
'MetadataItem',
189+
data,
190+
new Set(['foo']),
191+
'tag'
192+
)
193+
).to.be.true
194+
expect(
195+
doesQueryContain(
196+
document,
197+
types,
198+
'MetadataItem',
199+
data,
200+
new Set(['foo', 'foo/bar']),
201+
'tag'
202+
)
203+
).to.be.true
204+
expect(
205+
doesQueryContain(
206+
document,
207+
types,
208+
'MetadataItem',
209+
data,
210+
new Set(['foo/bar']),
211+
'tag'
212+
)
213+
).to.be.true
214+
expect(
215+
doesQueryContain(
216+
document,
217+
types,
218+
'MetadataItem',
219+
data,
220+
new Set(['foo/bar/baz']),
221+
'tag'
222+
)
223+
).to.be.false
224+
})
225+
it(`multi-step recursive type test`, function() {
226+
const document = gql`
227+
{
228+
Organization(id: 2) {
229+
id
230+
Users {
231+
edges {
232+
node {
233+
id
234+
Organizations {
235+
edges {
236+
node {
237+
id
238+
}
239+
}
240+
}
241+
}
242+
}
243+
}
244+
}
245+
}
246+
`
247+
const data = {
248+
Organization: {
249+
id: 2,
250+
Users: {
251+
edges: [
252+
{
253+
node: {
254+
id: 3,
255+
Organizations: {
256+
edges: [
257+
{
258+
node: {
259+
id: 5,
260+
},
261+
},
262+
{
263+
node: {
264+
id: 6,
265+
},
266+
},
267+
],
268+
},
269+
},
270+
},
271+
{
272+
node: {
273+
id: 4,
274+
Organizations: {
275+
edges: [
276+
{
277+
node: {
278+
id: 7,
279+
},
280+
},
281+
{
282+
node: {
283+
id: 8,
284+
},
285+
},
286+
],
287+
},
288+
},
289+
},
290+
],
291+
},
292+
},
293+
}
294+
295+
expect(
296+
doesQueryContain(document, types, 'Organization', data, new Set([7]))
297+
).to.be.true
298+
expect(
299+
doesQueryContain(document, types, 'Organization', data, new Set([5, 8]))
300+
).to.be.true
301+
expect(
302+
doesQueryContain(document, types, 'Organization', data, new Set([2]))
303+
).to.be.true
304+
expect(
305+
doesQueryContain(document, types, 'Organization', data, new Set([3]))
306+
).to.be.false
307+
})
158308
})

test/getPotentialAncestors.js

+6
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ describe(`getPotentialAncestors`, function() {
2727
expect(ancestors.get(types.Organization).fields.has('CustomDashboards')).to
2828
.be.false
2929
expect(ancestors.get(types.CustomDashboards)).not.to.exist
30+
expect(ancestors.get(types.MetadataItem).fields.has('Parent')).to.be.true
31+
32+
expect(getPotentialAncestors(types.MetadataItem).has(types.MetadataItem))
33+
expect(getPotentialAncestors(types.Organization).has(types.User))
34+
expect(getPotentialAncestors(types.User).has(types.Organization))
35+
expect(getPotentialAncestors(types.Organization).has(types.Organization))
3036
})
3137
})

0 commit comments

Comments
 (0)