Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Fix input types resolver in generators #373

Merged
merged 4 commits into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/graphqlgen/src/generators/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ Array [
"B",
"D",
"C",
"D",
]
`)
})
15 changes: 12 additions & 3 deletions packages/graphqlgen/src/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,18 @@ export function deepResolveInputTypes(
const childTypes = type.fields
.filter(t => t.type.isInput && !seen[t.type.name])
.map(t => t.type.name)
.map(name =>
deepResolveInputTypes(inputTypesMap, name, { ...seen, [name]: true }),
)
.map(name => {
/**
* Mutate so that we track state across tree branches.
*
* Example, only visit "C" once:
* A
* ├── B ── C
* └── D ── C
*/
seen[name] = true
Copy link
Member

@jasonkuhrt jasonkuhrt Dec 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intended effect of your change, given:

import * as _ from 'lodash'

const seen = {}
const name = 'bar'

const seenCopy = { ...seen, [name]: true }
seen[name] = true

assert(_.isEqual(seen, seenCopy))

?

Copy link
Member

@jasonkuhrt jasonkuhrt Dec 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but by using mutation to share state across the recursion's various branches of the tree-of-types, we effectively guarantee that said branches share their seen state... which otherwise is not the case (seen state is effectively forked at every node.

return deepResolveInputTypes(inputTypesMap, name, seen)
})
.reduce(concat, [])
return [typeName, ...childTypes]
} else {
Expand Down