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

fix: handle baseUrl suffix for GHES #186

Merged
merged 4 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const NON_VARIABLE_OPTIONS = [
"mediaType",
];

const GHES_V3_SUFFIX_REGEX = /\/api\/v3$/;
gr2m marked this conversation as resolved.
Show resolved Hide resolved

export function graphql<ResponseData = GraphQlQueryResponseData>(
request: typeof Request,
query: string | RequestParameters,
Expand Down Expand Up @@ -45,6 +47,13 @@ export function graphql<ResponseData = GraphQlQueryResponseData>(
{} as GraphQlEndpointOptions
);

// workaround for GitHub Enterprise baseUrl set with /api/v3 suffix
// https://github.com/octokit/auth-app.js/issues/111#issuecomment-657610451
const baseUrl = options.baseUrl || request.endpoint.DEFAULTS.baseUrl;
if (GHES_V3_SUFFIX_REGEX.test(baseUrl)) {
requestOptions.url = baseUrl.replace(GHES_V3_SUFFIX_REGEX, "/api/graphql");
}

return request(requestOptions).then((response) => {
if (response.data.errors) {
const headers: ResponseHeaders = {};
Expand Down
26 changes: 26 additions & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,30 @@ describe("graphql.defaults()", () => {
expect(result).toStrictEqual(mockData);
});
});

it.only("handle baseUrl set with /api/v3 suffix", () => {
gr2m marked this conversation as resolved.
Show resolved Hide resolved
const ghesGraphQl = graphql.defaults({
baseUrl: "https://github.acme-inc.com/api/v3",
headers: {
authorization: `token secret123`,
},
request: {
fetch: fetchMock.sandbox().post(
"https://github.acme-inc.com/api/graphql",
{ data: { ok: true } },
{
headers: {
authorization: "token secret123",
},
}
),
},
});

return ghesGraphQl(`query {
viewer {
login
}
}`);
});
});