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

feat(gatsby): graphql engine #33030

Merged
merged 8 commits into from
Sep 6, 2021
Merged

feat(gatsby): graphql engine #33030

merged 8 commits into from
Sep 6, 2021

Conversation

vladar
Copy link
Contributor

@vladar vladar commented Sep 2, 2021

Description

This feature is disabled by default. Only available when building gatsby itself with COMPILER_OPTIONS="GATSBY_MAJOR=4"

The new experimental feature: standalone graphql engine (can be required in any node script to run GraphQL queries). Bundles all necessary dependencies using webpack and uses underlying lmdb datastore to access data.

Example usage (after initial gatsby build):

const { GraphQLEngine } = require(`./.cache/query-engine`);

const graphqlEngine = new GraphQLEngine({
  dbPath: `${__dirname}/.cache/data/datastore`,
});

async function run() {
  const variables = {}
  const query = `
  {
    site {
      siteMetadata {
        title
      }
    }
  }
  `;
  const result = await graphqlEngine.runQuery(query, variables);
  console.log(result)
}

run().catch(console.error)

[ch37492]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 2, 2021
@vladar vladar removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 2, 2021
@vladar vladar marked this pull request as ready for review September 2, 2021 16:01
@vladar vladar changed the title feat: graphql engine feat(gatsby): graphql engine Sep 2, 2021
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

I added a bunch of comments, mostly for clarification as I'm not familiar with the code of this feature. If you feel that some comments should be handled in a follow-up to get this shipped, I'm 100% ok with that.

packages/gatsby/src/bootstrap/load-plugins/validate.ts Outdated Show resolved Hide resolved
packages/gatsby/src/schema/schema.js Outdated Show resolved Hide resolved

// Emit file that imports required node APIs
const schemaCustomizationPluginsPath =
process.cwd() + `/.cache/query-engine-plugins.js`
Copy link
Contributor

Choose a reason for hiding this comment

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

we usually don't use process.cwd() perhaps we need a global getter for program.directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do it all over the place in the codebase. I think it makes sense to add such utility and refactor in a separate PR.

packages/gatsby/src/schema/graphql-engine/entry.ts Outdated Show resolved Hide resolved
@vladar vladar requested a review from wardpeet September 3, 2021 16:38
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

LGTM!

@pieh pieh merged commit 48cff06 into master Sep 6, 2021
@pieh pieh deleted the graphql-engine branch September 6, 2021 09:48
@pieh pieh mentioned this pull request Sep 6, 2021
@simonjoom
Copy link

This feature seems very useful for Gatsby.

Does somebody can make a useful test-case example for the doc to understand the power of this feature?

I thought to get the generation pages' data on backend-side with graphql-engine from the browser via a simple js fetch api call which will display a list of all the available images generated by the website by example (with maybe on action-click on an image displaying size and image infos)

all that without any graphql code generated and bundled like before.

.. some other thoughts?

@ascorbic
Copy link
Contributor

@simonjoom This is an internal feature designed to be used during SSR and DSG, rather than something designed for use elsewhere. I made a proof of concept, but it's not something for production: https://github.com/netlify/gatsby-4-demo/blob/main/example/functions/graphql/graphql.ts

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.

5 participants