10
10
11
11
import invariant from '../jsutils/invariant' ;
12
12
import isNullish from '../jsutils/isNullish' ;
13
- import type { Value } from '../language/ast' ;
13
+ import type {
14
+ Value ,
15
+ IntValue ,
16
+ FloatValue ,
17
+ StringValue ,
18
+ BooleanValue ,
19
+ EnumValue ,
20
+ ListValue ,
21
+ ObjectValue ,
22
+ } from '../language/ast' ;
14
23
import {
15
24
NAME ,
16
25
INT ,
@@ -68,14 +77,14 @@ export function astFromValue(
68
77
// the value is not an array, convert the value using the list's item type.
69
78
if ( Array . isArray ( _value ) ) {
70
79
const itemType = type instanceof GraphQLList ? type . ofType : null ;
71
- return {
80
+ return ( {
72
81
kind : LIST ,
73
82
values : _value . map ( item => {
74
83
const itemValue = astFromValue ( item , itemType ) ;
75
84
invariant ( itemValue , 'Could not create AST item.' ) ;
76
85
return itemValue ;
77
86
} )
78
- } ;
87
+ } : ListValue ) ;
79
88
} else if ( type instanceof GraphQLList ) {
80
89
// Because GraphQL will accept single values as a "list of one" when
81
90
// expecting a list, if there's a non-array value and an expected list type,
@@ -84,7 +93,7 @@ export function astFromValue(
84
93
}
85
94
86
95
if ( typeof _value === 'boolean' ) {
87
- return { kind : BOOLEAN , value : _value } ;
96
+ return ( { kind : BOOLEAN , value : _value } : BooleanValue ) ;
88
97
}
89
98
90
99
// JavaScript numbers can be Float or Int values. Use the GraphQLType to
@@ -95,23 +104,26 @@ export function astFromValue(
95
104
const isIntValue = / ^ [ 0 - 9 ] + $ / . test ( stringNum ) ;
96
105
if ( isIntValue ) {
97
106
if ( type === GraphQLFloat ) {
98
- return { kind : FLOAT , value : stringNum + '.0' } ;
107
+ return ( { kind : FLOAT , value : stringNum + '.0' } : FloatValue ) ;
99
108
}
100
- return { kind : INT , value : stringNum } ;
109
+ return ( { kind : INT , value : stringNum } : IntValue ) ;
101
110
}
102
- return { kind : FLOAT , value : stringNum } ;
111
+ return ( { kind : FLOAT , value : stringNum } : FloatValue ) ;
103
112
}
104
113
105
114
// JavaScript strings can be Enum values or String values. Use the
106
115
// GraphQLType to differentiate if possible.
107
116
if ( typeof _value === 'string ') {
108
117
if ( type instanceof GraphQLEnumType &&
109
118
/ ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 ] * $ / . test ( _value ) ) {
110
- return { kind : ENUM , value : _value } ;
119
+ return ( { kind : ENUM , value : _value } : EnumValue ) ;
111
120
}
112
121
// Use JSON stringify, which uses the same string encoding as GraphQL,
113
122
// then remove the quotes.
114
- return { kind : STRING , value : JSON . stringify ( _value ) . slice ( 1 , - 1 ) } ;
123
+ return ( {
124
+ kind : STRING ,
125
+ value : JSON . stringify ( _value ) . slice ( 1 , - 1 )
126
+ } : StringValue ) ;
115
127
}
116
128
117
129
// last remaining possible typeof
@@ -135,5 +147,5 @@ export function astFromValue(
135
147
} ) ;
136
148
}
137
149
} ) ;
138
- return { kind : OBJECT , fields } ;
150
+ return ( { kind : OBJECT , fields } : ObjectValue ) ;
139
151
}
0 commit comments