Skip to content

Commit

Permalink
fix(doesQueryContain): support recursive types
Browse files Browse the repository at this point in the history
fix #2
  • Loading branch information
jedwards1211 committed Dec 20, 2018
1 parent cfbd6b9 commit b152e10
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/doesQueryContain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export default function doesQueryContain(

function doesNodeContain(node: Node, data: any, type: Type): boolean {
if (type === targetType) {
if (!ids) return true
return data && ids.has(data[idField])
if (!ids || (data && ids.has(data[idField]))) return true
}
if (!type.name) return false
const ancestorEntry = potentialAncestors.get(type)
Expand Down
150 changes: 150 additions & 0 deletions test/doesQueryContain.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,154 @@ describe(`doesQueryContain`, function() {
expect(doesQueryContain(document, types, 'Device', data, new Set([5]))).to
.be.false
})
it(`recursive type test`, function() {
const document = gql`
{
MetadataItem(tag: "foo/bar") {
tag
Parent {
tag
Parent {
tag
}
}
}
}
`
const data = {
MetadataItem: {
tag: 'foo/bar',
__typename: 'MetadataItem',
Parent: {
tag: 'foo',
__typename: 'MetadataItem',
Parent: null,
},
},
}
expect(doesQueryContain(document, types, 'MetadataItem')).to.be.true
expect(
doesQueryContain(
document,
types,
'MetadataItem',
data,
new Set(['foo']),
'tag'
)
).to.be.true
expect(
doesQueryContain(
document,
types,
'MetadataItem',
data,
new Set(['foo', 'foo/bar']),
'tag'
)
).to.be.true
expect(
doesQueryContain(
document,
types,
'MetadataItem',
data,
new Set(['foo/bar']),
'tag'
)
).to.be.true
expect(
doesQueryContain(
document,
types,
'MetadataItem',
data,
new Set(['foo/bar/baz']),
'tag'
)
).to.be.false
})
it(`multi-step recursive type test`, function() {
const document = gql`
{
Organization(id: 2) {
id
Users {
edges {
node {
id
Organizations {
edges {
node {
id
}
}
}
}
}
}
}
}
`
const data = {
Organization: {
id: 2,
Users: {
edges: [
{
node: {
id: 3,
Organizations: {
edges: [
{
node: {
id: 5,
},
},
{
node: {
id: 6,
},
},
],
},
},
},
{
node: {
id: 4,
Organizations: {
edges: [
{
node: {
id: 7,
},
},
{
node: {
id: 8,
},
},
],
},
},
},
],
},
},
}

expect(
doesQueryContain(document, types, 'Organization', data, new Set([7]))
).to.be.true
expect(
doesQueryContain(document, types, 'Organization', data, new Set([5, 8]))
).to.be.true
expect(
doesQueryContain(document, types, 'Organization', data, new Set([2]))
).to.be.true
expect(
doesQueryContain(document, types, 'Organization', data, new Set([3]))
).to.be.false
})
})
6 changes: 6 additions & 0 deletions test/getPotentialAncestors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ describe(`getPotentialAncestors`, function() {
expect(ancestors.get(types.Organization).fields.has('CustomDashboards')).to
.be.false
expect(ancestors.get(types.CustomDashboards)).not.to.exist
expect(ancestors.get(types.MetadataItem).fields.has('Parent')).to.be.true

expect(getPotentialAncestors(types.MetadataItem).has(types.MetadataItem))
expect(getPotentialAncestors(types.Organization).has(types.User))
expect(getPotentialAncestors(types.User).has(types.Organization))
expect(getPotentialAncestors(types.Organization).has(types.Organization))
})
})

0 comments on commit b152e10

Please sign in to comment.