Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): output apis.json for current valid API list #16061

Merged
merged 7 commits into from
Jul 25, 2019

Conversation

DSchau
Copy link
Contributor

@DSchau DSchau commented Jul 24, 2019

Description

This PR begins implementing one chunk of the work required to improve the experience outlined in #16055.

Specifically, this PR outputs an apis.json file that we can then distribute with NPM (and query with e.g. unpkg).

This uses a doclet @gatsbyVersion to denote when an API is available. This will be used to deliver richer error messages (or warnings) like so:

The plugin gatsby-source-filesystem is using the Gatsby API: `createSchemaCustomization`.

This API was introduced in gatsby@2.12.0. Upgrade your version of Gatsby (currently 2.1.0) for the best possible experience:

npm install gatsby@^2.12.0

A harder problem to solve is if we tweak an existing API (e.g. to pass a new utility or something) but I'd contend that we can either: 1) not solve that problem, or 2) Solve it later with additional metadata. As an example of this problem, createTypes was a new utility passed to sourceNodes in gatsby@2.2.0.

The idea is we'll progressively improve the error message when/if we add more metadata to apis.json in the future. For context, the below is a sample apis.json that will be produced with this PR.

{
  "ssr": {
    "replaceRenderer": {},
    "onRenderBody": {},
    "onPreRenderHTML": {},
    "wrapPageElement": {},
    "wrapRootElement": {}
  },
  "browser": {
    "onClientEntry": {},
    "onInitialClientRender": {},
    "onPreRouteUpdate": {},
    "onRouteUpdateDelayed": {},
    "onRouteUpdate": {},
    "shouldUpdateScroll": {},
    "registerServiceWorker": {},
    "replaceComponentRenderer": {
      "deprecated": true
    },
    "wrapPageElement": {},
    "wrapRootElement": {},
    "onPrefetchPathname": {},
    "onPostPrefetchPathname": {},
    "disableCorePrefetching": {},
    "replaceHydrateFunction": {},
    "onServiceWorkerInstalled": {},
    "onServiceWorkerUpdateFound": {},
    "onServiceWorkerUpdateReady": {},
    "onServiceWorkerActive": {},
    "onServiceWorkerRedundant": {}
  },
  "node": {
    "resolvableExtensions": {},
    "createPages": {},
    "createPagesStatefully": {},
    "sourceNodes": {},
    "onCreateNode": {},
    "onCreatePage": {},
    "setFieldsOnGraphQLNodeType": {},
    "createSchemaCustomization": {
      "version": "2.12.0"
    },
    "createResolvers": {
      "version": "2.2.0"
    },
    "preprocessSource": {},
    "generateSideEffects": {},
    "onCreateBabelConfig": {},
    "onCreateWebpackConfig": {},
    "onPreInit": {},
    "onPreBootstrap": {},
    "onPostBootstrap": {},
    "onPreBuild": {},
    "onPostBuild": {},
    "onPreExtractQueries": {},
    "onCreateDevServer": {}
  }
}

Any thoughts @gatsbyjs/core? Any metadata that is currently missing that we could use to improve this experience?

Related Issues

Begins implementing #16055.

@DSchau DSchau requested a review from a team as a code owner July 24, 2019 17:12
@DSchau DSchau changed the title feat(gatsby): output apis.json for current valid API list chore(gatsby): output apis.json for current valid API list Jul 24, 2019
@DSchau
Copy link
Contributor Author

DSchau commented Jul 24, 2019

I have no idea why unit tests failed -- seems unrelated 😢

@pieh
Copy link
Contributor

pieh commented Jul 25, 2019

Adding documentation to gatsby's devDependencies seems to cause different node_modules structure (causing snapshot to differ).

I think this happens because we do already have documentation package in gatsby-transformer-documentation - but we use documentation@10, while this new package is documentation@12 so both can't be hoisted

---edit
in any case - snapshot testing those exact paths to node_modules seems fragile

@DSchau
Copy link
Contributor Author

DSchau commented Jul 25, 2019

@pieh good catch -- it's fixed now.

Agreed, it's kinda why I've been leaning towards quite a bit less snapshots. They're kind of a fragile "easy" way to test things, when it seems better to perform tests on specific properties of the object's shape.

@DSchau
Copy link
Contributor Author

DSchau commented Jul 25, 2019

Note: after changing documentation to 10.x, apis.json still has the same content (e.g. version for those few APIs, deprecated field for that one API, etc.).


const output = apis.reduce((merged, [output, api]) => {
merged[api] = output.reduce((mergedOutput, doc) => {
if (doc.namespace.startsWith('.')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really figure out what this does - worth adding small note why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah -- this should block, good catch!

It's because of this line in gatsby-ssr.js, it's captured as an item in the output when it's not really an API.

/**
 * Object containing options defined in `gatsby-config.js`
 * @typedef {object} pluginOptions
 */

That creates:

{
  "namespace": "pluginOptions"
}

whereas every other one is an export so namespace is .replaceRenderer for instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I now do remember I similar thing to our docs (just implemented differently):

const funcs = sortBy(
this.props.data.file.childrenDocumentationJs.filter(
doc => doc.kind !== `typedef`
),
func => func.name
)

(and I didn't add note on what it does and why it's there :S)

pieh
pieh previously approved these changes Jul 25, 2019
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to go. Left small question, but don't consider it blocker.

@DSchau DSchau merged commit 510dad6 into gatsbyjs:master Jul 25, 2019
@DSchau DSchau deleted the gatsby/output-json branch July 25, 2019 17:24
@DSchau
Copy link
Contributor Author

DSchau commented Jul 25, 2019

Woo-hoo 🚀

https://unpkg.com/gatsby@2.13.41/apis.json

johno pushed a commit to johno/gatsby that referenced this pull request Jul 29, 2019
…16061)

* feat(gatsby): output apis.json for current valid API list

* chore: do not commit apis.json

* chore: use undefined instead of assign

* chore: iron out tests

* chore: make code self documenting (s/o Pieh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants