8000 Support callback for defaults · Issue #326 · octokit/graphql.js · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Support callback for defaults #326
Open
@astorije

Description

@astorije

Hi there!

All the examples of the .defaults() function that I could find are pretty straightforward, for example:

export const graphql = baseGraphql.defaults({
  headers: { authorization: 'token 1234' },
});

However, there are cases where some defaults are not available during the initial importing of modules, for examples if they are loaded from somewhere (i.e. async, e.g. from disk or another API) or if they require additional checking/validation.

Essentially, it would be great if we could do something like this:

export const graphql = baseGraphql.defaults(async () => {
  const oAuthToken = await getOAuthToken();
  return { headers: { authorization: `token ${oAuthToken}` } };
});

The underlying type of defaults would be:

defaults: (newDefaults: RequestParameters | (() => Promise<RequestParameters>)) => graphql;

In the meantime, we could probably get away with something like the following, but TypeScript types are getting in the way and I don't want to end up with something too hacky just to be able to use .defaults:

// This does not work
export const graphql: typeof baseGraphql = async (...args) => {
  const oAuthToken = await getOAuthToken();

  const graphqlWithAuth = baseGraphql.defaults({
    headers: { authorization: `token ${oAuthToken}` },
  });

  return graphqlWithAuth(...args);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: SupportAny questions, information, or general needs around the SDK or GitHub APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0