Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return next => async (r: any) => {
const req: RelayRequest = r;
const cacheKey = getCacheKey(req.operation.name, req.variables);
const cachedResponse = this.cache.get(cacheKey);
if (cachedResponse) {
this.log('Get graphql query from cache', cacheKey);
return RelayResponse.createFromGraphQL(await cachedResponse);
}
this.log('Run graphql query', cacheKey);
const graphqlArgs: SSRGraphQLArgs = isFunction(args) ? await args() : (args: any);
const hasSchema = graphqlArgs && graphqlArgs.schema;
const gqlResponse = new Promise(async (resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('RelayRequest timeout'));
}, 30000);
let payload = null;
try {
if (hasSchema) {
payload = await graphql({
...graphqlArgs,
} else {
payload = await next(r);
}
clearTimeout(timeout);
resolve(payload);
} catch (e) {
clearTimeout(timeout);
reject(e);
}
});
this.cache.set(cacheKey, gqlResponse);
const res = await gqlResponse;
this.log('Recieved response for', cacheKey, inspect(res, { colors: true, depth: 4 }));
return RelayResponse.createFromGraphQL(res);
};
}