Skip to content

Commit

Permalink
perf(graphql): remove fs.existsSync() check from context
Browse files Browse the repository at this point in the history
We used to check *on every request* wether or not that file existed - that is, of course, completely
unneccessary. This check is now only performed once on server startup.
  • Loading branch information
dmbch committed Nov 22, 2017
1 parent 37e0feb commit 1441d20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/graphql/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ var hopsConfig = require('hops-config');
exports.getFragmentsFile = function getFragmentsFile () {
return path.join(hopsConfig.appDir, 'fragmentTypes.json');
};

exports.getIntrospectionResult = function getIntrospectionResult () {
var file = exports.getFragmentsFile();
try {
require.resolve(file);
return require(file);
} catch (_) {}
};
8 changes: 2 additions & 6 deletions packages/graphql/node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

var fs = require('fs');

var ReactApollo = require('react-apollo');

var common = require('./lib/common');
var fragmentsFile = require('./lib/util').getFragmentsFile();
var introspectionResult = require('./lib/util').getIntrospectionResult();

exports.Context = exports.createContext = common.Context.extend({
prepareRender: function (enhancedElement) {
Expand All @@ -16,9 +14,7 @@ exports.Context = exports.createContext = common.Context.extend({
return common.Context.prototype.createClient.call(this, options);
},
getIntrospectionResult: function () {
if (fs.existsSync(fragmentsFile)) {
return require(fragmentsFile);
}
return introspectionResult;
},
getTemplateData: function () {
var templateData = common.Context.prototype.getTemplateData.call(this);
Expand Down

0 comments on commit 1441d20

Please sign in to comment.