Allow using FetchApi in graphiql api setup

Signed-off-by: Gustaf Räntilä <g.rantila@gmail.com>
This commit is contained in:
Gustaf Räntilä
2023-09-13 16:51:08 +02:00
committed by Gustaf Räntilä
parent 5f7ce0d289
commit b2fbeed540
3 changed files with 21 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-graphiql': patch
---
Add support for using the FetchApi
+3
View File
@@ -8,6 +8,7 @@
import { ApiRef } from '@backstage/core-plugin-api';
import { BackstagePlugin } from '@backstage/core-plugin-api';
import { ErrorApi } from '@backstage/core-plugin-api';
import { FetchApi } from '@backstage/core-plugin-api';
import { IconComponent } from '@backstage/core-plugin-api';
import { JSX as JSX_2 } from 'react';
import { OAuthApi } from '@backstage/core-plugin-api';
@@ -23,6 +24,7 @@ export type EndpointConfig = {
headers?: {
[name in string]: string;
};
fetchApi?: FetchApi;
};
// @public (undocumented)
@@ -31,6 +33,7 @@ export type GithubEndpointConfig = {
title: string;
url?: string;
errorApi?: ErrorApi;
fetchApi?: FetchApi;
githubAuthApi: OAuthApi;
};
@@ -15,7 +15,7 @@
*/
import { GraphQLBrowseApi, GraphQLEndpoint } from './types';
import { ErrorApi, OAuthApi } from '@backstage/core-plugin-api';
import { ErrorApi, FetchApi, OAuthApi } from '@backstage/core-plugin-api';
/**
* Helper for generic http endpoints
@@ -31,6 +31,10 @@ export type EndpointConfig = {
method?: 'POST';
// Defaults to setting Content-Type to application/json
headers?: { [name in string]: string };
/**
* Fetch API to use instead of browser fetch()
*/
fetchApi?: FetchApi;
};
/** @public */
@@ -45,6 +49,10 @@ export type GithubEndpointConfig = {
* Errors will be posted to the ErrorApi if it is provided.
*/
errorApi?: ErrorApi;
/**
* Fetch API to use instead of browser fetch()
*/
fetchApi?: FetchApi;
/**
* GitHub Auth API used to authenticate requests.
*/
@@ -55,7 +63,7 @@ export type GithubEndpointConfig = {
export class GraphQLEndpoints implements GraphQLBrowseApi {
// Create a support
static create(config: EndpointConfig): GraphQLEndpoint {
const { id, title, url, method = 'POST' } = config;
const { id, title, url, method = 'POST', fetchApi } = config;
return {
id,
title,
@@ -66,7 +74,7 @@ export class GraphQLEndpoints implements GraphQLBrowseApi {
...config.headers,
...options.headers,
};
const res = await fetch(await url, {
const res = await (fetchApi?.fetch ?? fetch)(await url, {
method,
headers,
body,
@@ -88,6 +96,7 @@ export class GraphQLEndpoints implements GraphQLBrowseApi {
title,
url = 'https://api.github.com/graphql',
errorApi,
fetchApi,
githubAuthApi,
} = config;
type ResponseBody = {
@@ -101,7 +110,7 @@ export class GraphQLEndpoints implements GraphQLBrowseApi {
let retried = false;
const doRequest = async (): Promise<any> => {
const res = await fetch(url, {
const res = await (fetchApi?.fetch ?? fetch)(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',