Skip to content

Add an optional 'customHeaders' field to AWSAppSyncClientOptions to easily add headers to requests#469

Open
babouinette wants to merge 2 commits into
awslabs:masterfrom
babouinette:allow-custom-headers
Open

Add an optional 'customHeaders' field to AWSAppSyncClientOptions to easily add headers to requests#469
babouinette wants to merge 2 commits into
awslabs:masterfrom
babouinette:allow-custom-headers

Conversation

@babouinette

@babouinette babouinette commented Oct 8, 2019

Copy link
Copy Markdown

Description of changes:

This change allows passing a customHeaders argument to AWSAppSyncClientOptions to easily add headers to requests, by using the 'headers' field in createHttpLink.

For example, to add an authorization header, instead of doing this :

const AppSyncConfig = {
  url:  "url",
  region: "us-east-1",
  auth: {
    type: "API_KEY",
    apiKey: "key"
  }
};

const client = new AWSAppSyncClient(AppSyncConfig, {
  link: createAppSyncLink({
    ...AppSyncConfig,
    resultsFetcherLink: ApolloLink.from([
      createHttpLink({
        uri: AppSyncConfig.url,
        headers: {"Authorization" : "Bearer 1234"},
      })
    ])
  })
});

We could do this :

const AppSyncConfig = {
  url: "url",
  region: "us-east-1",
  auth: {
    type: "API_KEY",
    apiKey: "key",
  },
  customHeaders: {"Authorization" : "Bearer 1234"},
};

const client = new AWSAppSyncClient(AppSyncConfig);

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pickfire

Copy link
Copy Markdown

I rather not have this, in our case our custom header is not known until called.

@bpcrao

bpcrao commented Sep 11, 2020

Copy link
Copy Markdown

I rather not have this, in our case our custom header is not known until called.

is there a better way to do this? like pass headers(tokens) only during mutate or query?
and I do not want to create client every time for every query too

@pickfire

Copy link
Copy Markdown

is there a better way to do this? like pass headers(tokens) only during mutate or query?
and I do not want to create client every time for every query too

For us, we only know it during the query, every query needs different header, maybe allow the query to be set during runtime?

@bpcrao

bpcrao commented Sep 14, 2020

Copy link
Copy Markdown

client.mutate({ mutation: mutationName, context: {
headers:{ ....headersJSON} // custom headers
} })

why is this not helping you? it worked for me

@pickfire

pickfire commented Sep 14, 2020

Copy link
Copy Markdown

How did you do client.mutate? I don't have that function here. Note that I was using angular.

@bpcrao

bpcrao commented Sep 16, 2020

Copy link
Copy Markdown

@pickfire using AWSAppSyncClient

@pickfire

Copy link
Copy Markdown

I don't use that directly. Also, I left the company working on this already.

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

Successfully merging this pull request may close these issues.

3 participants