Skip to content

Instantly share code, notes, and snippets.

@Bram-Zijp
Created June 2, 2020 21:42
Show Gist options
  • Save Bram-Zijp/eb854b5a92f8231376130da9504dadee to your computer and use it in GitHub Desktop.
Save Bram-Zijp/eb854b5a92f8231376130da9504dadee to your computer and use it in GitHub Desktop.
temp workaround Cannot find module './third-party/gatsby-node'
{
...
"scripts": {
...
"postinstall": "node scripts/injectPrismic.js"
...
},
"dependencies": {
...
"fs-extra": "^9.0.0"
...
}
...
}
// scripts/gatsby-node.js
/* eslint-disable */
'use strict';
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _asyncToGenerator2 = _interopRequireDefault(require('@babel/runtime/helpers/asyncToGenerator'));
// mirror off gatsby-source-graphql
// sigh...
const uuidv4 = require(`uuid/v4`);
const _require = require(`gatsby/graphql`),
buildSchema = _require.buildSchema,
printSchema = _require.printSchema;
const _require2 = require(`graphql-tools`),
transformSchema = _require2.transformSchema,
introspectSchema = _require2.introspectSchema,
RenameTypes = _require2.RenameTypes;
const _require3 = require(`apollo-link-http`),
createHttpLink = _require3.createHttpLink;
const nodeFetch = require(`node-fetch`);
const invariant = require(`invariant`);
const _require4 = require(`gatsby-source-graphql/batching/dataloader-link`),
createDataloaderLink = _require4.createDataloaderLink;
const _require5 = require(`gatsby-source-graphql/transforms`),
NamespaceUnderFieldTransform = _require5.NamespaceUnderFieldTransform,
StripNonQueryTransform = _require5.StripNonQueryTransform;
exports.sourceNodes =
/*#__PURE__*/
(function () {
var _ref = (0, _asyncToGenerator2.default)(function* (
{ actions, createNodeId, cache, createContentDigest },
options,
) {
const addThirdPartySchema = actions.addThirdPartySchema,
createNode = actions.createNode;
const url = options.url,
typeName = options.typeName,
fieldName = options.fieldName,
_options$headers = options.headers,
headers = _options$headers === void 0 ? {} : _options$headers,
_options$fetch = options.fetch,
fetch = _options$fetch === void 0 ? nodeFetch : _options$fetch,
_options$fetchOptions = options.fetchOptions,
fetchOptions = _options$fetchOptions === void 0 ? {} : _options$fetchOptions,
createLink = options.createLink,
createSchema = options.createSchema,
refetchInterval = options.refetchInterval,
_options$batch = options.batch,
batch = _options$batch === void 0 ? false : _options$batch;
invariant(
typeName && typeName.length > 0,
`gatsby-source-graphql requires option \`typeName\` to be specified`,
);
invariant(
fieldName && fieldName.length > 0,
`gatsby-source-graphql requires option \`fieldName\` to be specified`,
);
invariant(
(url && url.length > 0) || createLink,
`gatsby-source-graphql requires either option \`url\` or \`createLink\` callback`,
);
let link;
if (createLink) {
link = yield createLink(options);
} else {
const options = {
uri: url,
fetch,
fetchOptions,
headers: typeof headers === `function` ? yield headers() : headers,
};
link = batch ? createDataloaderLink(options) : createHttpLink(options);
}
let introspectionSchema;
if (createSchema) {
introspectionSchema = yield createSchema(options);
} else {
const cacheKey = `gatsby-source-graphql-schema-${typeName}-${fieldName}`;
let sdl = yield cache.get(cacheKey);
if (!sdl) {
introspectionSchema = yield introspectSchema(link);
sdl = printSchema(introspectionSchema);
} else {
introspectionSchema = buildSchema(sdl);
}
yield cache.set(cacheKey, sdl);
}
const nodeId = createNodeId(`gatsby-source-graphql-${typeName}`);
const node = createSchemaNode({
id: nodeId,
typeName,
fieldName,
createContentDigest,
});
createNode(node);
const resolver = (parent, args, context) => {
context.nodeModel.createPageDependency({
path: context.path,
nodeId: nodeId,
});
return {};
};
const schema = transformSchema(
{
schema: introspectionSchema,
link,
},
[
new StripNonQueryTransform(),
new RenameTypes((name) => `${typeName}_${name}`),
new NamespaceUnderFieldTransform({
typeName,
fieldName,
resolver,
}),
],
);
addThirdPartySchema({
schema,
});
if (process.env.NODE_ENV !== `production`) {
if (refetchInterval) {
const msRefetchInterval = refetchInterval * 1000;
const refetcher = () => {
createNode(
createSchemaNode({
id: nodeId,
typeName,
fieldName,
createContentDigest,
}),
);
setTimeout(refetcher, msRefetchInterval);
};
setTimeout(refetcher, msRefetchInterval);
}
}
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})();
function createSchemaNode({ id, typeName, fieldName, createContentDigest }) {
const nodeContent = uuidv4();
const nodeContentDigest = createContentDigest(nodeContent);
return {
id,
typeName: typeName,
fieldName: fieldName,
parent: null,
children: [],
internal: {
type: `GraphQLSource_${typeName}`,
contentDigest: nodeContentDigest,
ignoreType: true,
},
};
}
// scripts/injectPrismic.js
// eslint-disable-next-line import/no-extraneous-dependencies
const fs = require('fs-extra');
const path = require('path');
const sourceFile = path.join(__dirname, './gatsby-node.js');
fs.copySync(
sourceFile,
path.join(__dirname, '../node_modules/gatsby-source-prismic-graphql/third-party/gatsby-node.js'),
);
fs.copySync(
sourceFile,
path.join(
__dirname,
'../node_modules/gatsby-source-graphql-universal/third-party/gatsby-node.js',
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment