Skip to content

Commit

Permalink
feat: configurable graphql request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Leksat committed Oct 21, 2024
1 parent 1836deb commit 1ebcd2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/npm/@amazeelabs/gatsby-source-silverback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ The following configuration options are supported:
GraphQL query. Defaults to 100.
- `type_prefix` **(optional)**: A prefix to be added to all generated GraphQL
types. Defaults to `Drupal`.
- `request_timeout` **(optional)**: The timeout for the GraphQL requests in
milliseconds. Defaults to 60_000.

The optional credential parameters can be used to enable different workflows. On
production, they can be omitted to make sure Drupal handles these requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const pluginOptionsSchema: GatsbyNode['pluginOptionsSchema'] = ({
schema_configuration: Joi.string().optional(),
directives: Joi.object().pattern(Joi.string(), Joi.function()).optional(),
sources: Joi.object().pattern(Joi.string(), Joi.function()).optional(),
request_timeout: Joi.number().optional().min(1),
});

const getForwardedHeaders = (url: URL) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ export const createQueryExecutor = (
throw "No Drupal Url defined. Can't instantiate query executor.";
}
const url = `${new URL(options.drupal_url).origin}${options.graphql_path}`;
const executor = createNetworkQueryExecutor(url, {
headers: {
...options?.headers,
...(options?.auth_user && options?.auth_pass
? {
Authorization: `Basic ${Buffer.from(
`${options.auth_user}:${options.auth_pass}`,
).toString('base64')}`,
}
: {}),
...(options?.auth_key ? { 'api-key': options.auth_key } : {}),
const executor = createNetworkQueryExecutor(
url,
{
headers: {
...options?.headers,
...(options?.auth_user && options?.auth_pass
? {
Authorization: `Basic ${Buffer.from(
`${options.auth_user}:${options.auth_pass}`,
).toString('base64')}`,
}
: {}),
...(options?.auth_key ? { 'api-key': options.auth_key } : {}),
},
},
});
options.request_timeout,
);
return wrapQueryExecutorWithQueue(executor, {
concurrency: options.query_concurrency || 10,
});
Expand All @@ -44,12 +48,12 @@ export const createQueryExecutor = (
export function createNetworkQueryExecutor(
uri: string,
fetchOptions: RequestInit = {},
timeout = 60_000,
): IQueryExecutor {
return async function execute(args) {
const { query, variables, operationName } = args;

let response;
const timeout = 60_000;
const signal = timeoutSignal(timeout);
signal.onabort = () =>
console.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type Options = {
directives?: Record<string, Function>;
// A list of functions that will be available as data source.
sources?: Record<string, Function>;
// Request timeout for GraphQL queries in milliseconds. Defaults to 60_000.
request_timeout?: number;
};

export const validOptions = (options: {
Expand Down

0 comments on commit 1ebcd2f

Please sign in to comment.