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

Add ability to retry Graphql requests #26

Merged
merged 7 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 34 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require('chalk');
const { Directus } = require('@directus/sdk');
const { sourceNodes, createSchemaCustomization } = require('gatsby-source-graphql/gatsby-node');
const { createRemoteFileNode } = require('gatsby-source-filesystem');
const nodeFetch = require('node-fetch').default;

/**
* Validate plugin options
Expand Down Expand Up @@ -32,6 +33,7 @@ exports.pluginOptionsSchema = ({ Joi }) => {
}),
graphql: Joi.object(),
concurrency: Joi.number().default(10),
retries: Joi.number().default(5),
});
};

Expand Down Expand Up @@ -149,10 +151,11 @@ class Plugin {
this.refreshInterval = 0;
this.authPromise = null;
this.concurrency = 10;
this.retries = 5;
}

async setOptions(options) {
const { url, dev, auth, concurrency } = options;
const { url, dev, auth, concurrency, retries } = options;

if (isEmpty(url)) error('"url" must be defined');

Expand Down Expand Up @@ -202,6 +205,7 @@ class Plugin {

this.options = options;
this.concurrency = concurrency;
this.retries = retries;

return this.authPromise;
}
Expand All @@ -219,6 +223,31 @@ class Plugin {
typeName: this.options?.type?.name || 'DirectusData',
fieldName: this.options?.type?.field || 'directus',
headers: this.headers.bind(this),
fetch: async (uri, options) => {
function request() {
return nodeFetch(uri, options);
}

let count = 0;
let response = null;
let error = null;

while (response === null && count++ < this.retries) {
try {
response = await request();
} catch (err) {
error = err;
}

if (count > 0) {
await sleep(Math.pow(2, count) * 1000);
}
}

if (response === null) throw error;

return response;
},
};
}

Expand Down Expand Up @@ -317,4 +346,8 @@ function warning(message) {
Log.warning(message);
}

function sleep(ms) {
return new Promise((res) => setTimeout(res, ms));
}
rijkvanzanten marked this conversation as resolved.
Show resolved Hide resolved

const plugin = new Plugin();
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
],
"dependencies": {
"@directus/sdk": "10.2.0",
"ms": "2.1.3"
"ms": "2.1.3",
"node-fetch": "3.3.1"
},
"peerDependencies": {
"eslint": "7||8",
"gatsby-source-filesystem": "4||5",
"gatsby-source-graphql": "4||5",
"eslint": "7||8"
"gatsby-source-graphql": "4||5"
},
"repository": "directus/gatsby-source-directus",
"bugs": {
Expand Down
54 changes: 53 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.