@prg/gatsby-source-something-whatever
Version:
something something whatever who gives a crap.
51 lines (37 loc) • 1.85 kB
JavaScript
// TODO figure out how to import graphql file (.gql) from plugin root
import { typeDefs } from './types';
import { createClient } from './client';
import { PLUGIN_NAME } from './constants';
import { createContentNodes } from './nodes';
import { formatMsg, printGraphQLError } from './util';
import { downloadImagesAndCreateFileNodes } from './download-asset';
exports.createSchemaCustomization = ({ actions: { createTypes }}) => createTypes(typeDefs);
exports.sourceNodes = async (helpers, options) => {
const { accessToken, shopId, apiVersion } = options;
// If the user knows they are offline, serve them cached result
// For prod builds though always fail if we can't get the latest data
// if (shouldUseOfflineCache(getNodes)) return;
try {
const startMessage = formatMsg(`Something something you know whatever 🙄`);
const endMessage = formatMsg(`Yaaasssss! It's finally over, it only took you`);
console.log(startMessage);
console.time(endMessage);
// Throw an error early if the API key is missing
// Gatsby provides an API for that, too: https://www.gatsbyjs.org/docs/node-api-helpers/#reporter
if (!accessToken) {
helpers.reporter.panic(`Please include an access token to ${PLUGIN_NAME}.`)
}
const client = createClient(accessToken, apiVersion);
// query for data from the platform and create nodes
await createContentNodes(client, shopId, helpers);
// create remoteFileNodes from image data nodes
return downloadImagesAndCreateFileNodes(helpers)
.then(() => console.timeEnd(endMessage));
} catch (e) {
helpers.reporter.panicOnBuild(`An error occurred in ${PLUGIN_NAME}.`, e)
// If not a GraphQL request error, let Gatsby print the error.
if (!e.hasOwnProperty(`request`)) throw e;
printGraphQLError(e);
return Promse.resolve();
}
};