Skip to content

Commit

Permalink
first pass on updating to gatsby 4
Browse files Browse the repository at this point in the history
  • Loading branch information
teleaziz committed Feb 4, 2022
1 parent 77c8d2b commit cd5da83
Show file tree
Hide file tree
Showing 7 changed files with 23,141 additions and 39,168 deletions.
61,991 changes: 23,047 additions & 38,944 deletions packages/gatsby/package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
"url": "https://github.com/BuilderIO/builder.git",
"directory": "packages/gatsby"
},
"version": "2.0.5",
"version": "2.0.6-0",
"keywords": [
"gatsby",
"gatsby-plugin",
"builder.io",
"builder-io"
],
"dependencies": {
"@apollo/client": "^3.3.21",
"@babel/runtime": "^7.7.6",
"apollo-link": "1.2.13",
"apollo-link-http": "^1.5.16",
"apollo-link-http": "^1.5.17",
"apollo-link-retry": "^2.2.16",
"gatsby-source-graphql": "^4.1.0",
"graphql-tools": "^5.0.0",
"invariant": "^2.2.4",
"node-fetch": "^2.6.1",
Expand All @@ -30,12 +33,12 @@
"@builder.io/block-publish": "^1.1.2",
"babel-preset-gatsby-package": "^0.2.16",
"cross-env": "^5.2.1",
"gatsby": "^3.4.0",
"gatsby": "^4.6.0",
"jest": "^25.1.0"
},
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.15 || ^3.0.0"
"gatsby": "^4.6.0"
},
"scripts": {
"release:patch": "npm run build && npm version patch && ALLOW_PUBLISH=true npm publish",
Expand Down
36 changes: 0 additions & 36 deletions packages/gatsby/src/builder-config.js

This file was deleted.

13 changes: 13 additions & 0 deletions packages/gatsby/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const defaultOptions = {
// Arbitrary name for the remote schema Query type
fieldName: `allBuilderModels`,
typeName: `builder`,
limit: 30,
baseURL: `https://cdn.builder.io/api/v1/graphql`,
overrideDev404: true,
useCache: false,
batch: true,
// custom404Dev: 'path to custom 404'
// globalContext: { store: process.env.STORE_TOKEN } // helpful in multi stores repo
// filter: (entry) => entry.content.data.store === process.env.STORE_TOKEN // helpful in multi-store builder organization
};
57 changes: 57 additions & 0 deletions packages/gatsby/src/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { createHttpLink } = require(`apollo-link-http`)
const { RetryLink } = require(`apollo-link-retry`)
const { defaultOptions } = require('./constants');
const { ApolloLink } = require('apollo-link');
const invariant = require(`invariant`);

const retryLink = new RetryLink({
delay: {
initial: 100,
max: 2000,
jitter: true,
},
attempts: {
max: 5,
retryIf: (error, operation) =>
Boolean(error) && ![400, 401, 403, 404].includes(error.statusCode),
},
})

module.exports = (options) => {
const config = {
...defaultOptions,
...options,
}

invariant(
config.publicAPIKey && config.publicAPIKey.length > 0,
`@builder.io/gatsby requires a public API Key`
);

invariant(
config.limit < 101,
'@builder.io/gatsby maximum pagination limit is 100'
);

const graphqlOptions = {
typeName: config.typeName,
// Field under which the remote schema will be accessible. You'll use this in your Gatsby query
fieldName: config.fieldName,
// Url to query from 30
url: `${config.baseURL}/${config.publicAPIKey}?${!config.useCache ? "cachebust=true" : ''}`,
batch: config.batch,
createLink: pluginOptions => ApolloLink.from([
retryLink,
createHttpLink({ uri: pluginOptions.url }),
]),
};

return {
plugins: [
{
resolve: 'gatsby-source-graphql',
options: graphqlOptions,
}
]
}
};
144 changes: 17 additions & 127 deletions packages/gatsby/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,6 @@
const uuidv4 = require(`uuid/v4`);
const fs = require(`fs`);
const { buildSchema, printSchema } = require(`gatsby/graphql`);
const { transformSchema, introspectSchema, RenameTypes } = require(`graphql-tools`);
const { createHttpLink } = require(`apollo-link-http`);
const fetch = require(`node-fetch`);
const { defaultOptions } = require('./constants');
const invariant = require(`invariant`);
const promiseRetry = require('promise-retry');

const { NamespaceUnderFieldTransform, StripNonQueryTransform } = require(`./transforms`);
const { getGQLOptions, defaultOptions } = require(`./builder-config`);

/**
* @type { import('gatsby').GatsbyNode['sourceNodes'] }
*/
exports.sourceNodes = async ({ actions, createNodeId, cache, createContentDigest }, options) => {
const { addThirdPartySchema, createNode } = actions;
const config = getGQLOptions(options);
const { url, typeName, fieldName } = config;

const link = createHttpLink({
uri: url,
fetch,
useGETForQueries: true,
});

const cacheKey = `@builder.io/gatsby-schema-${typeName}-${fieldName}`;
let sdl = await cache.get(cacheKey);
let introspectionSchema;

if (!sdl) {
introspectionSchema = await introspectSchema(link);
sdl = printSchema(introspectionSchema);
} else {
introspectionSchema = buildSchema(sdl);
}

await cache.set(cacheKey, sdl);

const nodeId = createNodeId(`@builder.io/gatsby-${typeName}`);
const node = createSchemaNode({
id: nodeId,
typeName,
fieldName,
createContentDigest,
});
createNode(node);

const resolver = (_, __, context) => {
const { path, nodeModel } = context;
nodeModel.createPageDependency({
path,
nodeId,
});
return {};
};

const schema = transformSchema(
{
schema: introspectionSchema,
link,
},
[
new StripNonQueryTransform(),
new RenameTypes(name => `${typeName}_${name}`),
new NamespaceUnderFieldTransform({
typeName,
fieldName,
resolver,
}),
]
);

addThirdPartySchema({ schema });
};

function createSchemaNode({ id, typeName, fieldName, createContentDigest }) {
const contentDigest = createContentDigest(uuidv4());
return {
id,
typeName,
fieldName,
parent: null,
children: [],
internal: {
type: `BuilderPlugin`,
contentDigest,
ignoreType: true,
},
};
}

/**
* @type { import('gatsby').GatsbyNode['createPages'] }
*/
exports.createPages = async ({ graphql, actions }, options) => {
const config = {
...defaultOptions,
...options,
};
const { createPage } = actions;
if (typeof config.templates === 'object') {
const models = Object.keys(config.templates);
const offsets = models.map(() => 0);
await createPagesAsync(config, createPage, graphql, models, offsets);
}
};

const fs = require('fs');
/**
*
* @typedef {{
Expand Down Expand Up @@ -137,25 +32,6 @@ const fetchPages = ({ fieldName, models, offsets, graphql, limit }) =>
}
`);

const MAX_TRIES = 3;
/**
*
* @param {FetchPagesArgs} args
*/
const wrappedFetchPages = args =>
promiseRetry(
(retry, number) => {
if (number > 1) {
console.log(
`[Builder.io] data-fetching for ${args.fieldName} failed. Retrying: ${number}/${MAX_TRIES}`
);
}

return fetchPages(args).catch(retry);
},
{ retries: MAX_TRIES }
);

/**
*
* @param {*} config
Expand All @@ -165,7 +41,7 @@ const wrappedFetchPages = args =>
* @param {*} offsets
*/
const createPagesAsync = async (config, createPage, graphql, models, offsets) => {
const result = await wrappedFetchPages({
const result = await fetchPages({
fieldName: config.fieldName,
models,
offsets,
Expand Down Expand Up @@ -257,3 +133,17 @@ exports.onCreatePage = ({ page, actions }, options) => {
}
}
};


exports.createPages = async ({ graphql, actions }, options) => {
const config = {
...defaultOptions,
...options,
};
const { createPage } = actions;
if (typeof config.templates === 'object') {
const models = Object.keys(config.templates);
const offsets = models.map(() => 0);
await createPagesAsync(config, createPage, graphql, models, offsets);
}
};
57 changes: 0 additions & 57 deletions packages/gatsby/src/transforms.js

This file was deleted.

0 comments on commit cd5da83

Please sign in to comment.