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 3 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
39 changes: 38 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) {
err = error;
joselcvarela marked this conversation as resolved.
Show resolved Hide resolved
}

if (count > 0) {
await sleep(randInt(1000, 8000));
joselcvarela marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (response === null) throw error;

return response;
},
};
}

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

function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

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

const plugin = new Plugin();
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
"ms": "2.1.3"
},
"peerDependencies": {
"eslint": "7||8",
"gatsby-source-filesystem": "4||5",
"gatsby-source-graphql": "4||5",
"eslint": "7||8"
"node-fetch": "^3.3.1"
},
"repository": "directus/gatsby-source-directus",
"bugs": {
"url": "https://github.com/directus/directus/issues"
},
"gitHead": "24621f3934dc77eb23441331040ed13c676ceffd",
"devDependencies": {
"eslint-config-prettier": "^8.5.0"
"eslint-config-prettier": "^8.5.0",
"node-fetch": "^3.3.1"
}
}
Loading