Skip to content

Commit 2166d16

Browse files
committed
fix: fix @defaultValue on Int and Float fields storing strings instead of numbers
If you used `@defaultValue` with an `Int` or `Float` value, the value was stored as a serialized string instead of the numeric value. This made no difference for retrieving the value because strings are implicitly converted to strings there, but it had an effect on sorting and filtering.
1 parent 083b50f commit 2166d16

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

spec/regression/logistics/tests/default-value.graphql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,45 @@ mutation createNewWithSomeOtherValuesOverwritten {
4141
defaultValueEnum
4242
}
4343
}
44+
45+
# just querying (like checkCreatedByInit) does would automatically type-convert strings to numbers
46+
# when required. We had a bug where @defaultValue on Int and Float stored Strings. Filtering does
47+
# not automatically type-convert, so we can use this to verify that the types are correct
48+
# doing this after createNewWithSomeOtherValuesOverwritten to test that that one won't be returned
49+
query checkFiltersByInit {
50+
allDeliveries(
51+
filter: {
52+
destination: {
53+
street: "Hauptstraße"
54+
city: "Stuttgart"
55+
zipCode: "70xxx"
56+
country: { isoCode: "DE" }
57+
}
58+
defaultValueString: "foo"
59+
defaultValueString2: "bar"
60+
defaultValueInt: 42
61+
defaultValueTrue: true
62+
defaultValueFalse: false
63+
defaultValueFloat: 3.14
64+
defaultValueEnum: Bar
65+
}
66+
orderBy: deliveryNumber_ASC
67+
) {
68+
deliveryNumber
69+
destination {
70+
street
71+
city
72+
zipCode
73+
country {
74+
isoCode
75+
}
76+
}
77+
defaultValueString
78+
defaultValueString2
79+
defaultValueInt
80+
defaultValueTrue
81+
defaultValueFalse
82+
defaultValueFloat
83+
defaultValueEnum
84+
}
85+
}

spec/regression/logistics/tests/default-value.result.json

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,65 @@
8080
"defaultValueEnum": "Bar"
8181
}
8282
}
83+
},
84+
"checkFiltersByInit": {
85+
"data": {
86+
"allDeliveries": [
87+
{
88+
"deliveryNumber": "1000173",
89+
"destination": {
90+
"street": "Hauptstraße",
91+
"city": "Stuttgart",
92+
"zipCode": "70xxx",
93+
"country": {
94+
"isoCode": "DE"
95+
}
96+
},
97+
"defaultValueString": "foo",
98+
"defaultValueString2": "bar",
99+
"defaultValueInt": 42,
100+
"defaultValueTrue": true,
101+
"defaultValueFalse": false,
102+
"defaultValueFloat": 3.14,
103+
"defaultValueEnum": "Bar"
104+
},
105+
{
106+
"deliveryNumber": "1000521",
107+
"destination": {
108+
"street": "Hauptstraße",
109+
"city": "Stuttgart",
110+
"zipCode": "70xxx",
111+
"country": {
112+
"isoCode": "DE"
113+
}
114+
},
115+
"defaultValueString": "foo",
116+
"defaultValueString2": "bar",
117+
"defaultValueInt": 42,
118+
"defaultValueTrue": true,
119+
"defaultValueFalse": false,
120+
"defaultValueFloat": 3.14,
121+
"defaultValueEnum": "Bar"
122+
},
123+
{
124+
"deliveryNumber": "1000522",
125+
"destination": {
126+
"street": "Hauptstraße",
127+
"city": "Stuttgart",
128+
"zipCode": "70xxx",
129+
"country": {
130+
"isoCode": "DE"
131+
}
132+
},
133+
"defaultValueString": "foo",
134+
"defaultValueString2": "bar",
135+
"defaultValueInt": 42,
136+
"defaultValueTrue": true,
137+
"defaultValueFalse": false,
138+
"defaultValueFloat": 3.14,
139+
"defaultValueEnum": "Bar"
140+
}
141+
]
142+
}
83143
}
84-
}
144+
}

src/graphql/value-from-ast.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import { PlainObject } from '../utils/utils';
99
export function getValueFromAST(valueNode: ValueNode): any {
1010
switch (valueNode.kind) {
1111
case Kind.STRING:
12-
case Kind.INT:
13-
case Kind.FLOAT:
1412
case Kind.BOOLEAN:
1513
case Kind.ENUM:
1614
return valueNode.value;
15+
case Kind.INT:
16+
// this is the logic in GraphQLInt.prototype.parseInt
17+
return parseInt(valueNode.value, 10);
18+
case Kind.FLOAT:
19+
// this is the logic in GraphQLFloat.prototype.parseFloat
20+
return parseFloat(valueNode.value);
1721
case Kind.LIST:
1822
return [...valueNode.values.map((value) => getValueFromAST(value))];
1923
case Kind.OBJECT:

0 commit comments

Comments
 (0)