Skip to content

Commit c5336b3

Browse files
authored
Merge pull request #41 from anvilco/newhouse/issue-40/enum-default-bug
Enum default value bug
2 parents c22641a + f1cfbcc commit c5336b3

File tree

5 files changed

+1778
-18
lines changed

5 files changed

+1778
-18
lines changed

lib/reducer.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,34 @@ export const introspectionFieldReducerGenerator: (
103103
return introspectionFieldReducer
104104
}
105105

106-
// ENUM type defaults will not JSON.parse correctly, so if it is an ENUM then don't
107-
// try to do that.
106+
// ENUM type defaults will not JSON.parse correctly, so we have to do some work
108107
// TODO: fix typing here
109108
export const resolveDefaultValue = (curr: any) => {
110-
return isIntrospectionEnumType(curr.type)
111-
? curr.defaultValue
112-
: JSON.parse(curr.defaultValue)
109+
let type = curr.type
110+
let isList = false
111+
112+
// Dig out the underlying type in case it's a LIST or a NON_NULL or both
113+
while (true) {
114+
if (isIntrospectionListTypeRef(type)) {
115+
isList = true
116+
type = type.ofType
117+
} else if (isNonNullIntrospectionType(type)) {
118+
type = type.ofType
119+
} else {
120+
break
121+
}
122+
}
123+
// Not an ENUM? No problem...just JSON parse it
124+
if (!isIntrospectionEnumType(type)) {
125+
return JSON.parse(curr.defaultValue)
126+
}
127+
128+
if (!isList || !curr.defaultValue || typeof curr.defaultValue !== 'string') {
129+
return curr.defaultValue
130+
}
131+
132+
// Take a string like "[RED, GREEN]" and convert it to ["RED", "GREEN"]
133+
return curr.defaultValue.substr(1, curr.defaultValue.length - 2).split(',').map((val: string) => val.trim())
113134
}
114135

115136
// Reducer for each type exposed by the GraphQL Schema

nodemon.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"delay": 2000,
3+
"verbose": true,
4+
"ext": "ts,js,json",
5+
"ignore": [
6+
".git",
7+
"dist"
8+
]
9+
}

0 commit comments

Comments
 (0)