Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI forward tenant header to API #229

Closed
gauthierelcapitan opened this issue Apr 11, 2020 · 2 comments
Closed

OpenAPI forward tenant header to API #229

gauthierelcapitan opened this issue Apr 11, 2020 · 2 comments

Comments

@gauthierelcapitan
Copy link

gauthierelcapitan commented Apr 11, 2020

Hi guys,

I'm trying to use GraphQL Mesh with a multi-tenant OpenApi 3.0. The OpenApi has a apiKey security scheme described as :

components:
  securitySchemes:
    api_key:
      type: apiKey
      in: header
      name: X-Tenant-ID
      description: Tenant ID

In the GraphQL query I added headers (with Apollo Playground) :

{
  "X-Tenant-ID": "my-tenant-id"
}

But I can't figure out how to forward this header to the REAST Api. Is there any way yet ?

I noticed that the release 0.0.20 allows to add operationHeaders, so for the moment I can hardcode my tenant header.

sources:
  - name: My API
    handler:
      openapi:
        source: http://localhost:8080/api-docs/json
        operationHeaders:
          X-Tenant-ID: my-tenant-id
        baseUrl: http://localhost:8080     

Thanks for the amazing job guys !

@ardatan
Copy link
Owner

ardatan commented Apr 11, 2020

GraphQL Mesh is not a server that forwards your requests. It only generates an executable schema that you can even execute using pure graphql-js. So I wouldn't say forwarding headers is a good idea.

We are currently working on a PR that implements a feature to pass custom headers using root, args, context and info. So with this canary version you can try the following methods.

0.0.21-alpha-d20fcf0.20+d20fcf0

You can only use root, args, context or info which is three parameters of resolver function.

sources:
  - name: My API
    handler:
      openapi:
        source: http://localhost:8080/api-docs/json
        operationHeaders:
          X-Tenant-ID: {args.tenantId}
        baseUrl: http://localhost:8080   

If you do that, GraphQL Mesh detects there must be a shared argument in your root operations, so it will add tenantId argument to all of your root operation in your schema.

Or if you pass context without using mesh serve, you can use context and root object like below;

sources:
  - name: My API
    handler:
      openapi:
        source: http://localhost:8080/api-docs/json
        operationHeaders:
          X-Tenant-ID: {context.tenantId}
        baseUrl: http://localhost:8080   

or you can use environmental variables;

sources:
  - name: My API
    handler:
      openapi:
        source: http://localhost:8080/api-docs/json
        operationHeaders:
          X-Tenant-ID: ${TENANT_ID}
        baseUrl: http://localhost:8080   

@ardatan
Copy link
Owner

ardatan commented Apr 16, 2020

Available in 0.1.0

@ardatan ardatan closed this as completed Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants