IBM / openapi-to-graphql

Translate APIs described by OpenAPI Specifications (OAS) into GraphQL

Home Page:https://developer.ibm.com/open/projects/openapi-to-graphql/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preserve headers

sudall opened this issue · comments

Hello,

Is there a way to preserve headers that are passed to the graphql endpoint? ie. forward headers on the REST API endpoint. I tried passing along an Authorization header, but it seems to get lost at some point and the REST API returns with a 401. I've looked through the docs, but I didn't see anything that would do this.

@sudall Sorry for the late reply. Have you tried using the headers or requestOptions options? These options should allow you to pass your custom headers and other request options to the endpoint.

If you've tried this and it did not work, please let me know more about your use case.

@sudall you would want to pass this in correctly, but this is how I hacked the CLI to proxy the Authorization header:

diff --git a/packages/openapi-to-graphql-cli/src/index.ts b/packages/openapi-to-graphql-cli/src/index.ts
index 9393c34..0a21d43 100644
--- a/packages/openapi-to-graphql-cli/src/index.ts
+++ b/packages/openapi-to-graphql-cli/src/index.ts
@@ -144,7 +144,12 @@ Promise.all(
        * for the OpenAPI-to-GraphQL library
        */
       ...{
-        headers: parseKeyValuePairs(program.header),
+        // headers: parseKeyValuePairs(program.header),
+        headers(method, path, title, { source, args, context, info }) {
+          return context.headers.authorization
+            ? { Authorization: context.headers.authorization }
+            : {}
+        },
         qs: parseKeyValuePairs(program.queryString)
       }
     }