@@ -21,6 +21,7 @@ import { FileUpload } from 'graphql-upload'
21
21
import stream from 'stream'
22
22
import * as Oas3Tools from './oas_3_tools'
23
23
import { JSONPath } from 'jsonpath-plus'
24
+ import * as JSONPointer from 'jsonpointer'
24
25
import { debug } from 'debug'
25
26
import { GraphQLError , GraphQLFieldResolver } from 'graphql'
26
27
import formurlencoded from 'form-urlencoded'
@@ -1157,26 +1158,26 @@ function getAuthReqAndProtcolName<TSource, TContext, TArgs>(
1157
1158
*/
1158
1159
function resolveRuntimeExpression (
1159
1160
paramName : string ,
1160
- value : string ,
1161
+ runtimeExpression : string ,
1161
1162
resolveData : any ,
1162
1163
root : any ,
1163
1164
args : any
1164
1165
) : any {
1165
- if ( value === '$url' ) {
1166
+ if ( runtimeExpression === '$url' ) {
1166
1167
return resolveData . url
1167
- } else if ( value === '$method' ) {
1168
+ } else if ( runtimeExpression === '$method' ) {
1168
1169
return resolveData . usedRequestOptions . method
1169
- } else if ( value === '$statusCode' ) {
1170
+ } else if ( runtimeExpression === '$statusCode' ) {
1170
1171
return resolveData . usedStatusCode
1171
- } else if ( value . startsWith ( '$request.' ) ) {
1172
+ } else if ( runtimeExpression . startsWith ( '$request.' ) ) {
1172
1173
// CASE: parameter is previous body
1173
- if ( value === '$request.body' ) {
1174
+ if ( runtimeExpression === '$request.body' ) {
1174
1175
return resolveData . usedPayload
1175
1176
1176
1177
// CASE: parameter in previous body
1177
- } else if ( value . startsWith ( '$request.body#' ) ) {
1178
+ } else if ( runtimeExpression . startsWith ( '$request.body#' ) ) {
1178
1179
const tokens = JSONPath ( {
1179
- path : value . split ( 'body#/' ) [ 1 ] ,
1180
+ path : runtimeExpression . split ( 'body#/' ) [ 1 ] ,
1180
1181
json : resolveData . usedPayload
1181
1182
} )
1182
1183
if ( Array . isArray ( tokens ) && tokens . length > 0 ) {
@@ -1186,36 +1187,36 @@ function resolveRuntimeExpression(
1186
1187
}
1187
1188
1188
1189
// CASE: parameter in previous query parameter
1189
- } else if ( value . startsWith ( '$request.query' ) ) {
1190
+ } else if ( runtimeExpression . startsWith ( '$request.query' ) ) {
1190
1191
return resolveData . usedParams [
1191
1192
Oas3Tools . sanitize (
1192
- value . split ( 'query.' ) [ 1 ] ,
1193
+ runtimeExpression . split ( 'query.' ) [ 1 ] ,
1193
1194
Oas3Tools . CaseStyle . camelCase
1194
1195
)
1195
1196
]
1196
1197
1197
1198
// CASE: parameter in previous path parameter
1198
- } else if ( value . startsWith ( '$request.path' ) ) {
1199
+ } else if ( runtimeExpression . startsWith ( '$request.path' ) ) {
1199
1200
return resolveData . usedParams [
1200
1201
Oas3Tools . sanitize (
1201
- value . split ( 'path.' ) [ 1 ] ,
1202
+ runtimeExpression . split ( 'path.' ) [ 1 ] ,
1202
1203
Oas3Tools . CaseStyle . camelCase
1203
1204
)
1204
1205
]
1205
1206
1206
1207
// CASE: parameter in previous header parameter
1207
- } else if ( value . startsWith ( '$request.header' ) ) {
1208
- return resolveData . usedRequestOptions . headers [ value . split ( 'header.' ) [ 1 ] ]
1208
+ } else if ( runtimeExpression . startsWith ( '$request.header' ) ) {
1209
+ return resolveData . usedRequestOptions . headers [ runtimeExpression . split ( 'header.' ) [ 1 ] ]
1209
1210
}
1210
- } else if ( value . startsWith ( '$response.' ) ) {
1211
+ } else if ( runtimeExpression . startsWith ( '$response.' ) ) {
1211
1212
/**
1212
1213
* CASE: parameter is body
1213
1214
*
1214
1215
* NOTE: may not be used because it implies that the operation does not
1215
1216
* return a JSON object and OpenAPI-to-GraphQL does not create GraphQL
1216
1217
* objects for non-JSON data and links can only exists between objects.
1217
1218
*/
1218
- if ( value === '$response.body' ) {
1219
+ if ( runtimeExpression === '$response.body' ) {
1219
1220
const result = JSON . parse ( JSON . stringify ( root ) )
1220
1221
/**
1221
1222
* _openAPIToGraphQL contains data used by OpenAPI-to-GraphQL to create the GraphQL interface
@@ -1225,45 +1226,37 @@ function resolveRuntimeExpression(
1225
1226
return result
1226
1227
1227
1228
// CASE: parameter in body
1228
- } else if ( value . startsWith ( '$response.body#' ) ) {
1229
- const tokens = JSONPath ( {
1230
- path : value . split ( 'body#/' ) [ 1 ] ,
1231
- json : root
1232
- } )
1233
- if ( Array . isArray ( tokens ) && tokens . length > 0 ) {
1234
- return tokens [ 0 ]
1235
- } else {
1236
- httpLog ( `Warning: could not extract parameter '${ paramName } ' from link` )
1237
- }
1229
+ } else if ( runtimeExpression . startsWith ( '$response.body#' ) ) {
1230
+ return JSONPointer . get ( root , runtimeExpression . split ( 'body#' ) [ 1 ] )
1238
1231
1239
1232
// CASE: parameter in query parameter
1240
- } else if ( value . startsWith ( '$response.query' ) ) {
1233
+ } else if ( runtimeExpression . startsWith ( '$response.query' ) ) {
1241
1234
// NOTE: handled the same way $request.query is handled
1242
1235
return resolveData . usedParams [
1243
1236
Oas3Tools . sanitize (
1244
- value . split ( 'query.' ) [ 1 ] ,
1237
+ runtimeExpression . split ( 'query.' ) [ 1 ] ,
1245
1238
Oas3Tools . CaseStyle . camelCase
1246
1239
)
1247
1240
]
1248
1241
1249
1242
// CASE: parameter in path parameter
1250
- } else if ( value . startsWith ( '$response.path' ) ) {
1243
+ } else if ( runtimeExpression . startsWith ( '$response.path' ) ) {
1251
1244
// NOTE: handled the same way $request.path is handled
1252
1245
return resolveData . usedParams [
1253
1246
Oas3Tools . sanitize (
1254
- value . split ( 'path.' ) [ 1 ] ,
1247
+ runtimeExpression . split ( 'path.' ) [ 1 ] ,
1255
1248
Oas3Tools . CaseStyle . camelCase
1256
1249
)
1257
1250
]
1258
1251
1259
1252
// CASE: parameter in header parameter
1260
- } else if ( value . startsWith ( '$response.header' ) ) {
1261
- return resolveData . responseHeaders [ value . split ( 'header.' ) [ 1 ] ]
1253
+ } else if ( runtimeExpression . startsWith ( '$response.header' ) ) {
1254
+ return resolveData . responseHeaders [ runtimeExpression . split ( 'header.' ) [ 1 ] ]
1262
1255
}
1263
1256
}
1264
1257
1265
1258
throw new Error (
1266
- `Cannot create link because '${ value } ' is an invalid runtime expression.`
1259
+ `Cannot resolve link because '${ runtimeExpression } ' is an invalid runtime expression.`
1267
1260
)
1268
1261
}
1269
1262
0 commit comments