@@ -21,6 +21,7 @@ import { FileUpload } from 'graphql-upload'
2121import stream from 'stream'
2222import * as Oas3Tools from './oas_3_tools'
2323import { JSONPath } from 'jsonpath-plus'
24+ import * as JSONPointer from 'jsonpointer'
2425import { debug } from 'debug'
2526import { GraphQLError , GraphQLFieldResolver } from 'graphql'
2627import formurlencoded from 'form-urlencoded'
@@ -1157,26 +1158,26 @@ function getAuthReqAndProtcolName<TSource, TContext, TArgs>(
11571158 */
11581159function resolveRuntimeExpression (
11591160 paramName : string ,
1160- value : string ,
1161+ runtimeExpression : string ,
11611162 resolveData : any ,
11621163 root : any ,
11631164 args : any
11641165) : any {
1165- if ( value === '$url' ) {
1166+ if ( runtimeExpression === '$url' ) {
11661167 return resolveData . url
1167- } else if ( value === '$method' ) {
1168+ } else if ( runtimeExpression === '$method' ) {
11681169 return resolveData . usedRequestOptions . method
1169- } else if ( value === '$statusCode' ) {
1170+ } else if ( runtimeExpression === '$statusCode' ) {
11701171 return resolveData . usedStatusCode
1171- } else if ( value . startsWith ( '$request.' ) ) {
1172+ } else if ( runtimeExpression . startsWith ( '$request.' ) ) {
11721173 // CASE: parameter is previous body
1173- if ( value === '$request.body' ) {
1174+ if ( runtimeExpression === '$request.body' ) {
11741175 return resolveData . usedPayload
11751176
11761177 // CASE: parameter in previous body
1177- } else if ( value . startsWith ( '$request.body#' ) ) {
1178+ } else if ( runtimeExpression . startsWith ( '$request.body#' ) ) {
11781179 const tokens = JSONPath ( {
1179- path : value . split ( 'body#/' ) [ 1 ] ,
1180+ path : runtimeExpression . split ( 'body#/' ) [ 1 ] ,
11801181 json : resolveData . usedPayload
11811182 } )
11821183 if ( Array . isArray ( tokens ) && tokens . length > 0 ) {
@@ -1186,36 +1187,36 @@ function resolveRuntimeExpression(
11861187 }
11871188
11881189 // CASE: parameter in previous query parameter
1189- } else if ( value . startsWith ( '$request.query' ) ) {
1190+ } else if ( runtimeExpression . startsWith ( '$request.query' ) ) {
11901191 return resolveData . usedParams [
11911192 Oas3Tools . sanitize (
1192- value . split ( 'query.' ) [ 1 ] ,
1193+ runtimeExpression . split ( 'query.' ) [ 1 ] ,
11931194 Oas3Tools . CaseStyle . camelCase
11941195 )
11951196 ]
11961197
11971198 // CASE: parameter in previous path parameter
1198- } else if ( value . startsWith ( '$request.path' ) ) {
1199+ } else if ( runtimeExpression . startsWith ( '$request.path' ) ) {
11991200 return resolveData . usedParams [
12001201 Oas3Tools . sanitize (
1201- value . split ( 'path.' ) [ 1 ] ,
1202+ runtimeExpression . split ( 'path.' ) [ 1 ] ,
12021203 Oas3Tools . CaseStyle . camelCase
12031204 )
12041205 ]
12051206
12061207 // 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 ] ]
12091210 }
1210- } else if ( value . startsWith ( '$response.' ) ) {
1211+ } else if ( runtimeExpression . startsWith ( '$response.' ) ) {
12111212 /**
12121213 * CASE: parameter is body
12131214 *
12141215 * NOTE: may not be used because it implies that the operation does not
12151216 * return a JSON object and OpenAPI-to-GraphQL does not create GraphQL
12161217 * objects for non-JSON data and links can only exists between objects.
12171218 */
1218- if ( value === '$response.body' ) {
1219+ if ( runtimeExpression === '$response.body' ) {
12191220 const result = JSON . parse ( JSON . stringify ( root ) )
12201221 /**
12211222 * _openAPIToGraphQL contains data used by OpenAPI-to-GraphQL to create the GraphQL interface
@@ -1225,45 +1226,37 @@ function resolveRuntimeExpression(
12251226 return result
12261227
12271228 // 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 ] )
12381231
12391232 // CASE: parameter in query parameter
1240- } else if ( value . startsWith ( '$response.query' ) ) {
1233+ } else if ( runtimeExpression . startsWith ( '$response.query' ) ) {
12411234 // NOTE: handled the same way $request.query is handled
12421235 return resolveData . usedParams [
12431236 Oas3Tools . sanitize (
1244- value . split ( 'query.' ) [ 1 ] ,
1237+ runtimeExpression . split ( 'query.' ) [ 1 ] ,
12451238 Oas3Tools . CaseStyle . camelCase
12461239 )
12471240 ]
12481241
12491242 // CASE: parameter in path parameter
1250- } else if ( value . startsWith ( '$response.path' ) ) {
1243+ } else if ( runtimeExpression . startsWith ( '$response.path' ) ) {
12511244 // NOTE: handled the same way $request.path is handled
12521245 return resolveData . usedParams [
12531246 Oas3Tools . sanitize (
1254- value . split ( 'path.' ) [ 1 ] ,
1247+ runtimeExpression . split ( 'path.' ) [ 1 ] ,
12551248 Oas3Tools . CaseStyle . camelCase
12561249 )
12571250 ]
12581251
12591252 // 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 ] ]
12621255 }
12631256 }
12641257
12651258 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.`
12671260 )
12681261}
12691262
0 commit comments