@@ -28,29 +28,29 @@ export const decodeResultData = ({ request, data, sddm, scalars }: {
28
28
if ( ! sddmOutputObject ) return
29
29
if ( ! data ) return
30
30
31
- for ( const [ key , value ] of Object . entries ( data ) ) {
32
- const documentField = findDocumentField ( request . operation . selectionSet , key )
33
- const kSchema = documentField ?. name . value ?? key
34
- const sddmOutputField = sddmOutputObject . f [ kSchema ]
35
- if ( ! sddmOutputField ?. nt ) continue
36
-
37
- decodeResultValue ( {
38
- parentContext : { type : `object` , object : data , key } ,
39
- value,
40
- sddmNode : sddmOutputField . nt ,
41
- documentPart : documentField ?. selectionSet ?? null ,
42
- scalars,
43
- } )
44
- }
31
+ decodeResultValue ( {
32
+ parentContext : null ,
33
+ value : data ,
34
+ sddmNode : sddmOutputObject ,
35
+ documentPart : request . operation . selectionSet ,
36
+ scalars,
37
+ } )
45
38
}
46
39
47
40
const decodeResultValue = ( input : {
48
- parentContext : { type : `object`; object : Record < string , any > ; key : string } | {
49
- type : `list`
50
- object : any [ ]
51
- key : number
52
- }
53
- value : Value
41
+ parentContext :
42
+ | null
43
+ | {
44
+ type : `object`
45
+ object : Record < string , any >
46
+ fieldName : string
47
+ }
48
+ | {
49
+ type : `list`
50
+ object : any [ ]
51
+ index : number
52
+ }
53
+ value : Grafaid . SomeFieldData
54
54
sddmNode : SchemaDrivenDataMap . OutputNodes
55
55
documentPart : null | Grafaid . Document . SelectionSetNode
56
56
scalars : Schema . Scalar . ScalarMap
@@ -65,7 +65,7 @@ const decodeResultValue = (input: {
65
65
// todo test case of array data of scalars
66
66
value . forEach ( ( item , index ) => {
67
67
decodeResultValue ( {
68
- parentContext : { type : `list` , object : value , key : index } ,
68
+ parentContext : { type : `list` , object : value , index } ,
69
69
value : item ,
70
70
sddmNode,
71
71
documentPart,
@@ -79,34 +79,41 @@ const decodeResultValue = (input: {
79
79
// todo in strict mode throw error that sddmNode is inconsistent with data shape.
80
80
}
81
81
const object = value
82
- for ( const [ key , value ] of Object . entries ( object ) ) {
83
- const documentField = findDocumentField ( documentPart , key )
84
- const kSchema = documentField ?. name . value ?? key
82
+ for ( const [ fieldName , value ] of Object . entries ( object ) ) {
83
+ // TODO optimize field lookup
84
+ // We need a smart algorithm that considers:
85
+ // key space of schema vs key space of data
86
+ const documentField = findDocumentField ( documentPart , fieldName )
87
+ const kSchema = documentField ?. name . value ?? fieldName
85
88
const sddmOutputField = sddmNode . f [ kSchema ]
86
89
if ( ! sddmOutputField ?. nt ) continue
87
90
decodeResultValue ( {
88
- parentContext : { type : `object` , object, key } ,
91
+ parentContext : { type : `object` , object, fieldName } ,
89
92
value,
90
93
sddmNode : sddmOutputField . nt ,
91
94
documentPart : documentField ?. selectionSet ?? null ,
92
95
scalars,
93
96
} )
94
97
}
95
98
} else {
99
+ if ( ! parentContext ) {
100
+ // Should be impossible. Strict mode could error here.
101
+ return
102
+ }
96
103
if ( SchemaDrivenDataMap . isScalar ( sddmNode ) ) {
97
104
const decodedValue = Schema . Scalar . applyCodec ( sddmNode . codec . decode , value )
98
105
if ( parentContext . type === `object` ) {
99
- parentContext . object [ parentContext . key ] = decodedValue
106
+ parentContext . object [ parentContext . fieldName ] = decodedValue
100
107
} else {
101
- parentContext . object [ parentContext . key ] = decodedValue
108
+ parentContext . object [ parentContext . index ] = decodedValue
102
109
}
103
110
} else if ( SchemaDrivenDataMap . isCustomScalarName ( sddmNode ) ) {
104
111
const scalar = Schema . Scalar . lookupCustomScalarOrFallbackToString ( scalars , sddmNode )
105
112
const decodedValue = Schema . Scalar . applyCodec ( scalar . codec . decode , value )
106
113
if ( parentContext . type === `object` ) {
107
- parentContext . object [ parentContext . key ] = decodedValue
114
+ parentContext . object [ parentContext . fieldName ] = decodedValue
108
115
} else {
109
- parentContext . object [ parentContext . key ] = decodedValue
116
+ parentContext . object [ parentContext . index ] = decodedValue
110
117
}
111
118
} else {
112
119
// enums not decoded.
@@ -116,23 +123,19 @@ const decodeResultValue = (input: {
116
123
117
124
const findDocumentField = (
118
125
selectionSet : null | Grafaid . Document . SelectionSetNode ,
119
- k : string ,
126
+ fieldName : string ,
120
127
) : Grafaid . Document . FieldNode | null => {
121
128
if ( ! selectionSet ) return null
122
129
123
130
for ( const selection of selectionSet . selections ) {
124
- if ( selection . kind === Kind . FIELD && ( selection . alias ?. value ?? selection . name . value ) === k ) {
131
+ if ( selection . kind === Kind . FIELD && ( selection . alias ?. value ?? selection . name . value ) === fieldName ) {
125
132
return selection
126
133
}
127
134
if ( selection . kind === Kind . INLINE_FRAGMENT ) {
128
- const result = findDocumentField ( selection . selectionSet , k )
135
+ const result = findDocumentField ( selection . selectionSet , fieldName )
129
136
if ( result !== null ) return result
130
137
}
131
138
}
132
139
133
140
return null
134
141
}
135
-
136
- type Value = Grafaid . Schema . StandardScalarRuntimeTypes [ ] | Grafaid . Schema . StandardScalarRuntimeTypes | null | {
137
- [ k : string ] : Value
138
- }
0 commit comments