IBM / openapi-to-graphql

Translate APIs described by OpenAPI Specifications (OAS) into GraphQL
https://developer.ibm.com/open/projects/openapi-to-graphql/
MIT License
1.61k stars 211 forks source link

Preserve headers #446

Open sudall opened 2 years ago

sudall commented 2 years ago

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.

Alan-Cha commented 2 years ago

@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.

cancan101 commented 2 years ago

@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)
       }
     }