",
+ "dependencies": {
+ "@babel/parser": "^7.9.6",
+ "@babel/traverse": "^7.9.6",
+ "axios": "^0.19.2",
+ "chokidar": "^3.4.0",
+ "fs": "^0.0.1-security",
+ "gatsby": "^2.21.21",
+ "gatsby-source-filesystem": "^2.3.1",
+ "gatsby-transformer-json": "^2.4.1",
+ "glob": "^7.1.6",
+ "groq-js": "^0.1.5",
+ "normalize-path": "^3.0.0",
+ "prop-types": "^15.7.2",
+ "react": "^16.12.0",
+ "react-dom": "^16.13.1"
+ },
+ "devDependencies": {
+ "prettier": "2.0.5"
+ },
+ "keywords": [
+ "gatsby"
+ ],
+ "license": "MIT",
+ "scripts": {
+ "build": "gatsby build",
+ "develop": "gatsby develop",
+ "format": "prettier --write \"**/*.{js,jsx,json,md}\"",
+ "start": "gatsby develop",
+ "serve": "gatsby serve",
+ "clean": "gatsby clean",
+ "test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
+ }
+}
diff --git a/packages/gatsby-groq-demo/src/data/characters/inara.json b/packages/gatsby-groq-demo/src/data/characters/inara.json
new file mode 100644
index 0000000..7c8b9a0
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/inara.json
@@ -0,0 +1,9 @@
+{
+ "id": "inaraId",
+ "firstname": "Inara",
+ "lastname": "Serra",
+ "position": "Companion",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/jayne.json b/packages/gatsby-groq-demo/src/data/characters/jayne.json
new file mode 100644
index 0000000..efc1407
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/jayne.json
@@ -0,0 +1,9 @@
+{
+ "id": "jayneId",
+ "firstname": "Jayne",
+ "lastname": "Cobb",
+ "position": "Hired Gun",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/kaylee.json b/packages/gatsby-groq-demo/src/data/characters/kaylee.json
new file mode 100644
index 0000000..c7b7aa7
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/kaylee.json
@@ -0,0 +1,9 @@
+{
+ "id": "kayleeId",
+ "firstname": "Kaylee",
+ "lastname": "Frye",
+ "position": "Mechanic",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/malcom.json b/packages/gatsby-groq-demo/src/data/characters/malcom.json
new file mode 100644
index 0000000..f504d02
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/malcom.json
@@ -0,0 +1,9 @@
+{
+ "id": "malcomId",
+ "firstname": "Malcom",
+ "lastname": "Reynolds",
+ "position": "Captain",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/nandi.json b/packages/gatsby-groq-demo/src/data/characters/nandi.json
new file mode 100644
index 0000000..422a84f
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/nandi.json
@@ -0,0 +1,6 @@
+{
+ "id": "nandiId",
+ "firstname": "Nandi",
+ "lastname": "",
+ "position": "Former Companion"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/niska.json b/packages/gatsby-groq-demo/src/data/characters/niska.json
new file mode 100644
index 0000000..e593a06
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/niska.json
@@ -0,0 +1,9 @@
+{
+ "id": "niskaId",
+ "firstname": "Adelai",
+ "lastname": "Niska",
+ "position": "Mob Boss",
+ "ship": {
+ "_ref": "skyplexId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/river.json b/packages/gatsby-groq-demo/src/data/characters/river.json
new file mode 100644
index 0000000..f291b01
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/river.json
@@ -0,0 +1,9 @@
+{
+ "id": "riverId",
+ "firstname": "River",
+ "lastname": "Tam",
+ "position": "Assassin",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/saffron.json b/packages/gatsby-groq-demo/src/data/characters/saffron.json
new file mode 100644
index 0000000..bc560ac
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/saffron.json
@@ -0,0 +1,6 @@
+{
+ "id": "saffronId",
+ "firstname": "Saffron",
+ "lastname": "Unknown",
+ "position": "Sneeky Crook"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/shepherd.json b/packages/gatsby-groq-demo/src/data/characters/shepherd.json
new file mode 100644
index 0000000..33d93e6
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/shepherd.json
@@ -0,0 +1,9 @@
+{
+ "id": "shepherdId",
+ "firstname": "Shepherd",
+ "lastname": "Book",
+ "position": "Shepherd",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/simon.json b/packages/gatsby-groq-demo/src/data/characters/simon.json
new file mode 100644
index 0000000..829028e
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/simon.json
@@ -0,0 +1,9 @@
+{
+ "id": "simonId",
+ "firstname": "Simon",
+ "lastname": "Tam",
+ "position": "Doctor",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/triumphElder.json b/packages/gatsby-groq-demo/src/data/characters/triumphElder.json
new file mode 100644
index 0000000..da46788
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/triumphElder.json
@@ -0,0 +1,4 @@
+{
+ "id": "triumphElderId",
+ "position": "Village Elder"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/wash.json b/packages/gatsby-groq-demo/src/data/characters/wash.json
new file mode 100644
index 0000000..93a8d43
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/wash.json
@@ -0,0 +1,9 @@
+{
+ "id": "washId",
+ "firstname": "Wash",
+ "lastname": "Washburne",
+ "position": "Pilot",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/characters/zoe.json b/packages/gatsby-groq-demo/src/data/characters/zoe.json
new file mode 100644
index 0000000..a4329b1
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/characters/zoe.json
@@ -0,0 +1,9 @@
+{
+ "id": "zoeId",
+ "firstname": "Zoe",
+ "lastname": "Washburne",
+ "position": "First Mate",
+ "ship": {
+ "_ref": "serenityId"
+ }
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/cattle.json b/packages/gatsby-groq-demo/src/data/jobs/cattle.json
new file mode 100644
index 0000000..1556efb
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/cattle.json
@@ -0,0 +1,17 @@
+{
+ "id": "cattleId",
+ "worlds": [
+ { "_ref": "santoId" },
+ { "_ref": "jiangyinId" }
+ ],
+ "client": {
+ "_ref": "badgerId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "jayneId" },
+ { "_ref": "kayleeId" },
+ { "_ref": "shepherdId" },
+ { "_ref": "zoeId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/heartOfGold.json b/packages/gatsby-groq-demo/src/data/jobs/heartOfGold.json
new file mode 100644
index 0000000..0df10a5
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/heartOfGold.json
@@ -0,0 +1,15 @@
+{
+ "id": "heartOfGoldId",
+ "client": {
+ "_ref": "nandiId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "zoeId" },
+ { "_ref": "jayneId" },
+ { "_ref": "washId" },
+ { "_ref": "shepherdId" },
+ { "_ref": "simonId" },
+ { "_ref": "inaraId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/hospital.json b/packages/gatsby-groq-demo/src/data/jobs/hospital.json
new file mode 100644
index 0000000..5874f95
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/hospital.json
@@ -0,0 +1,15 @@
+{
+ "id": "hospitalId",
+ "worlds": [
+ { "_ref": "aerialId" }
+ ],
+ "client": {
+ "_ref": "simonId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "zoeId" },
+ { "_ref": "jayneId" },
+ { "_ref": "simonId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/jaynestown.json b/packages/gatsby-groq-demo/src/data/jobs/jaynestown.json
new file mode 100644
index 0000000..810894e
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/jaynestown.json
@@ -0,0 +1,16 @@
+{
+ "id": "jaynestownId",
+ "worlds": [
+ { "_ref": "higginsMoonId" }
+ ],
+ "client": {
+ "_ref": "?"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "jayneId" },
+ { "_ref": "kayleeId" },
+ { "_ref": "washId" },
+ { "_ref": "simonId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/lassiter.json b/packages/gatsby-groq-demo/src/data/jobs/lassiter.json
new file mode 100644
index 0000000..2f09a80
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/lassiter.json
@@ -0,0 +1,18 @@
+{
+ "id": "lassiterId",
+ "worlds": [
+ { "_ref": "bellerophonId" }
+ ],
+ "client": {
+ "_ref": "saffronId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "saffronId" },
+ { "_ref": "kayleeId" },
+ { "_ref": "zoeId" },
+ { "_ref": "jayneId" },
+ { "_ref": "washId" },
+ { "_ref": "inaraId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/spaceSalvage.json b/packages/gatsby-groq-demo/src/data/jobs/spaceSalvage.json
new file mode 100644
index 0000000..38159a5
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/spaceSalvage.json
@@ -0,0 +1,12 @@
+{
+ "id": "spaceSalvageId",
+ "worlds": [],
+ "client": {
+ "_ref": "badgerId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "jayneId" },
+ { "_ref": "washId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/trainHeist.json b/packages/gatsby-groq-demo/src/data/jobs/trainHeist.json
new file mode 100644
index 0000000..9b1e590
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/trainHeist.json
@@ -0,0 +1,14 @@
+{
+ "id": "trainHeistId",
+ "worlds": [
+ { "_ref": "reginaId" }
+ ],
+ "client": {
+ "_ref": "niskaId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "jayneId" },
+ { "_ref": "zoeId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/jobs/triumphBandits.json b/packages/gatsby-groq-demo/src/data/jobs/triumphBandits.json
new file mode 100644
index 0000000..04c1b3a
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/jobs/triumphBandits.json
@@ -0,0 +1,14 @@
+{
+ "id": "triumphBanditsId",
+ "worlds": [
+ { "_ref": "triumphId" }
+ ],
+ "client": {
+ "_ref": "niskaId"
+ },
+ "crew": [
+ { "_ref": "malcomId" },
+ { "_ref": "jayneId" },
+ { "_ref": "zoeId" }
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/ships/serenity.json b/packages/gatsby-groq-demo/src/data/ships/serenity.json
new file mode 100644
index 0000000..ee7219f
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/ships/serenity.json
@@ -0,0 +1,12 @@
+{
+ "id": "serenityId",
+ "name": "Serenity",
+ "type": "Firefly",
+ "strengths": [
+ "Large cargo bay",
+ "Plenty of hiding spots"
+ ],
+ "weaknesses": [
+ "Compression coils"
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/ships/skyplex.json b/packages/gatsby-groq-demo/src/data/ships/skyplex.json
new file mode 100644
index 0000000..962a769
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/ships/skyplex.json
@@ -0,0 +1,11 @@
+{
+ "id": "skyplexId",
+ "name": "Niska's Skyplex",
+ "type": "Space Station",
+ "strengths": [
+ "Heavily guarded"
+ ],
+ "weaknesses": [
+ "Suprisingly easy to takeover"
+ ]
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/ariel.json b/packages/gatsby-groq-demo/src/data/worlds/ariel.json
new file mode 100644
index 0000000..0d7c580
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/ariel.json
@@ -0,0 +1,5 @@
+{
+ "id": "arielId",
+ "name": "Ariel",
+ "location": "Core"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/bellerophon.json b/packages/gatsby-groq-demo/src/data/worlds/bellerophon.json
new file mode 100644
index 0000000..c7f255b
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/bellerophon.json
@@ -0,0 +1,5 @@
+{
+ "id": "bellerpophonId",
+ "name": "Bellerophon",
+ "location": "Core"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/higgins.json b/packages/gatsby-groq-demo/src/data/worlds/higgins.json
new file mode 100644
index 0000000..87d87c7
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/higgins.json
@@ -0,0 +1,5 @@
+{
+ "id": "higginsMoonId",
+ "name": "Higgins",
+ "location": "Border"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/jiangyin.json b/packages/gatsby-groq-demo/src/data/worlds/jiangyin.json
new file mode 100644
index 0000000..48f3178
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/jiangyin.json
@@ -0,0 +1,5 @@
+{
+ "id": "jiangyinId",
+ "name": "Jiangyin",
+ "location": "Border"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/regina.json b/packages/gatsby-groq-demo/src/data/worlds/regina.json
new file mode 100644
index 0000000..edb3da0
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/regina.json
@@ -0,0 +1,5 @@
+{
+ "id": "reginaId",
+ "name": "Regina",
+ "location": "Border"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/santo.json b/packages/gatsby-groq-demo/src/data/worlds/santo.json
new file mode 100644
index 0000000..7922ba7
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/santo.json
@@ -0,0 +1,5 @@
+{
+ "id": "santoId",
+ "name": "Santo",
+ "location": "Core"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/data/worlds/triumph.json b/packages/gatsby-groq-demo/src/data/worlds/triumph.json
new file mode 100644
index 0000000..11b27e9
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/data/worlds/triumph.json
@@ -0,0 +1,5 @@
+{
+ "id": "triumphId",
+ "name": "Triumph",
+ "location": "Border"
+}
\ No newline at end of file
diff --git a/packages/gatsby-groq-demo/src/fragments/index.js b/packages/gatsby-groq-demo/src/fragments/index.js
new file mode 100644
index 0000000..c53d473
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/fragments/index.js
@@ -0,0 +1,11 @@
+/**
+ * Put all of your GROQ "fragments" here!
+ */
+
+exports.getWorldsJobs = `
+ * [ internal.type == "jobs" && ^.id in worlds[]._ref ] {
+ client,
+ crew,
+ worlds,
+ }
+`;
\ No newline at end of file
diff --git a/src/images/gatsby-astronaut.png b/packages/gatsby-groq-demo/src/images/gatsby-astronaut.png
similarity index 100%
rename from src/images/gatsby-astronaut.png
rename to packages/gatsby-groq-demo/src/images/gatsby-astronaut.png
diff --git a/src/images/gatsby-icon.png b/packages/gatsby-groq-demo/src/images/gatsby-icon.png
similarity index 100%
rename from src/images/gatsby-icon.png
rename to packages/gatsby-groq-demo/src/images/gatsby-icon.png
diff --git a/src/pages/404.js b/packages/gatsby-groq-demo/src/pages/404.js
similarity index 100%
rename from src/pages/404.js
rename to packages/gatsby-groq-demo/src/pages/404.js
diff --git a/packages/gatsby-groq-demo/src/pages/index.js b/packages/gatsby-groq-demo/src/pages/index.js
new file mode 100644
index 0000000..f11fefd
--- /dev/null
+++ b/packages/gatsby-groq-demo/src/pages/index.js
@@ -0,0 +1,37 @@
+import React from 'react';
+import { useGroqQuery } from '../../plugins/gatsby-plugin-groq';
+
+import { getWorldsJobs } from '../fragments';
+
+
+
+export const groqQuery = `{
+ "worlds": *[ internal.type == "worlds" ] {
+ _id,
+ name,
+ "jobs": ${getWorldsJobs}
+ }
+}`;
+const IndexPage = ( { pageContext } ) => {
+
+ const { data } = pageContext;
+ const awesomeCharacters = useGroqQuery( `
+ *[ internal.type == "characters" ] {
+ ...
+ }
+ ` );
+
+ console.log( 'Worlds', data.worlds );
+ console.log( 'Awesome Characters', awesomeCharacters );
+
+ return(
+
+
+
Try adding a groqQuery export to this page!
+
+
+ )
+
+}
+
+export default IndexPage
diff --git a/plugins/gatsby-plugin-groq/gatsby-node.js b/plugins/gatsby-plugin-groq/gatsby-node.js
deleted file mode 100644
index 230ca9f..0000000
--- a/plugins/gatsby-plugin-groq/gatsby-node.js
+++ /dev/null
@@ -1,539 +0,0 @@
-const axios = require( 'axios' );
-const fs = require( 'fs' );
-const glob = require( 'glob' );
-const murmurhash = require( './murmur' );
-const normalizePath = require( 'normalize-path' );
-const parser = require( '@babel/parser' );
-const path = require( 'path' );
-const gatsbyReporter = require( 'gatsby-cli/lib/reporter' );
-const traverse = require( '@babel/traverse' ).default;
-const { watch } = require( 'chokidar' );
-const { runQuery } = require( './index' );
-
-
-// Will make all this prettier once built out as plugin.
-// Right now everything depends on specific directory structure.
-const ROOT = process.env.INIT_CWD;
-const GROQ_DIR = process.env.NODE_ENV === 'development' ? `${ROOT}/.cache/groq` : `${ROOT}/public/static/groq`;
-
-
-/**
- * Here's where we extract and run all initial queries.
- * Also sets up a watcher to re-run queries during dev when files change.fcache
- *
- * Runs in right after schema creation and before createPages.
- */
-exports.resolvableExtensions = async ( { graphql, actions, cache, getNodes, traceId, store }, plugin ) => {
-
- const reporter = new Reporter();
-
- // Ugly.
- if( ! fs.existsSync( GROQ_DIR ) ) {
- fs.mkdirSync( GROQ_DIR );
- }
-
- // Cache fragments.
- const fragmentsDir = !! plugin.fragmentsDir ? path.join( ROOT, plugin.fragmentsDir ) : null;
-
- if( !! fragmentsDir ) {
- cacheFragments( fragmentsDir, cache );
- }
-
- // Extract initial queries.
- const intitialNodes = getNodes();
-
- extractQueries( { nodes: intitialNodes, traceId, cache } );
-
-
- // For now watching all files to re-extract queries.
- // Right now there doesn't seem to be a way to watch for build updates using Gatsby's public node apis.
- // Created a ticket in github to explore option here.
- const watcher = watch( `${ROOT}/src/**/*.js` );
-
- watcher.on( 'change', async ( filePath ) => {
-
- // Recache if this was a change within fragments directory.
- if( !! fragmentsDir && filePath.includes( fragmentsDir ) ) {
-
- await cacheFragments( fragmentsDir, cache );
-
- // TODO For now we need to force a refresh
- axios.post( 'http://localhost:8000/__refresh' );
-
- }
-
- // Get info for file that was changed.
- const fileContents = fs.readFileSync( filePath, 'utf8' );
-
- // Check if file has either page or static queries.
- const pageQueryMatch = /export const groqQuery = /.exec( fileContents );
- const staticQueryMatch = /useGroqQuery/.exec( fileContents );
- if( ! pageQueryMatch && ! staticQueryMatch ) {
- return;
- }
-
- reporter.info( 'Re-processing groq queries...' );
-
- // Get updated nodes to query against.
- const nodes = getNodes();
-
- // Run page queries.
- if( pageQueryMatch ) {
-
- const { deletePage, createPage } = actions;
- const { pages } = store.getState();
-
- // First we need to reprocess the page query.
- const processedPageQuery = await processFilePageQuery( filePath, nodes, cache );
-
- if( !! processedPageQuery ) {
-
- const { fileHash: newHash, query: newQuery } = processedPageQuery;
- const queryFile = `${GROQ_DIR}/${newHash}.json`;
-
- await cacheQueryResults( newHash, newQuery );
-
- // Update all paths using this page component.
- // Is this performant or should we try to leverage custom cache?
- for( let [ path, page ] of pages ) {
-
- if( page.component !== filePath ) {
- continue;
- }
-
- reporter.info( `Updating path: ${page.path}` );
-
- // Run query and inject into page context.
- pageQueryToContext( {
- actions,
- cache,
- file: queryFile,
- nodes,
- page
- } );
-
- }
-
- }
-
- }
-
- // Static queries.
- if( ! staticQueryMatch ) {
- return reporter.success( 'Finished re-processing page queries' );
- }
-
- try {
-
- // Run query and save to cache.
- // Files using the static query will be automatically refreshed.
- const { hash, json } = await processFileStaticQuery( filePath, nodes, plugin );
- await cacheQueryResults( hash, json );
-
- return reporter.success( 'Finished re-processing queries' )
-
- }
- catch( err ) {
- console.warn( err );
- }
-
- } );
-
-
-}
-
-/**
- * Inject page query results its page.
- */
-exports.onCreatePage = async ( { actions, cache, getNodes, page, traceId } ) => {
-
- // Check for hashed page queries for this component.
- const componentPath = page.component;
- const hash = murmurhash( componentPath );
- const queryFile = `${GROQ_DIR}/${hash}.json`;
-
- if( ! fs.existsSync( queryFile) ) {
- return;
- }
-
- // Run query and write to page context.
- pageQueryToContext( {
- actions,
- cache,
- file: queryFile,
- getNodes,
- page,
- } );
-
-
-}
-
-/**
- * Extract page and static queries from all files.
- * Process and cache results.
- *
- * @param {Object} $0 Gatsby Node Helpers.
- */
-async function extractQueries( { nodes, traceId, cache } ) {
-
- const reporter = new Reporter();
-
- reporter.info( 'Getting files for groq extraction...' );
-
- // Pattern that will be appended to searched directories.
- // It will match any .js, .jsx, .ts, and .tsx files, that are not
- // inside /node_modules.
- const filesRegex = `*.+(t|j)s?(x)`
- const pathRegex = `/{${filesRegex},!(node_modules)/**/${filesRegex}}`;
-
- let files = [
- path.join( ROOT, 'src' ),
- ].reduce( ( merged, folderPath ) => {
-
- merged.push(
- ...glob.sync( path.join( folderPath, pathRegex ), {
- nodir: true,
- } )
- );
-
- return merged;
-
- }, [] );
-
- files = files.filter( d => ! d.match( /\.d\.ts$/ ) );
- files = files.map( normalizePath );
-
- // Loop through files and look for queries to extract.
- for( let file of files ) {
-
- const pageQuery = await processFilePageQuery( file, nodes, cache );
- const staticQuery = await processFileStaticQuery( file, nodes, cache );
-
- // Cache page query.
- // This will only contain a json file of unprocessed query.
- if( !! pageQuery ) {
- const { fileHash, query } = pageQuery;
- cacheQueryResults( fileHash, query );
- }
-
- // Cache static query.
- // This will contain actual results of the query.
- if( !! staticQuery ) {
- const { hash, json } = staticQuery;
- cacheQueryResults( hash, json, 'static', );
- }
-
- }
-
- reporter.info( 'Finished getting files for query extraction' );
-
-
-}
-
-/**
- * Cache fragments.
- *
- * @param {string} fragmentsDir
- * @param {Object} cache
- * @return {bool} if succesfully cached.
- */
-async function cacheFragments( fragmentsDir, cache ) {
-
- const reporter = new Reporter();
- const index = path.join( fragmentsDir, 'index.js' );
-
- if( !! fs.readFileSync( index ) ) {
-
- delete require.cache[ require.resolve( index ) ];
-
- fragments = require( index );
-
- reporter.info( 'Caching fragments' );
-
- await cache.set( 'groq-fragments', fragments );
-
- return true;
-
- }
-
- return false;
-
-}
-
-/**
- * Cache hash of query with fragments.
- *
- * @param {number} hash
- * @param {Object} cache
- */
-// async function cacheFragmentQueryHash( hash, cache ) {
-//
-// const hashes = await cache.get( 'groq-fragment-queries' ) || [];
-//
-// if( !! hashes[hash] ) {
-// return;
-// }
-//
-// hashes.push( hash );
-//
-// await cache.set( 'groq-fragment-queries', hashes );
-//
-// }
-
-/**
-* Run page query and update the related page via createPage.
-*
-* @param {Object} $0 Gatsby Node Helpers.
-*/
-async function pageQueryToContext( { actions, cache, file, getNodes, nodes, page, } ) {
-
- const { createPage, deletePage, setPageData } = actions;
-
- // Get query content.
- const content = fs.readFileSync( file, 'utf8' );
- let { unprocessed: query } = JSON.parse( content );
-
- // Replace any variables within query with context values.
- if( page.context ){
-
- for( let [ key, value ] of Object.entries( page.context ) ) {
-
- const search = `\\$${key}`;
- const pattern = new RegExp( search, 'g' );
- query = query.replace( pattern, `"${value}"` );
-
- }
- }
-
- // Do the thing.
- const allNodes = nodes || getNodes();
- const fragments = await cache.get( 'groq-fragments' );
- const results = await runQuery( query, allNodes, { fragments } );
-
- page.context.data = results;
-
- deletePage( page );
- createPage( {
- ...page,
- } );
-
-}
-
-/**
- * Extracts page query from file and returns its hash and unprocessed string.
- *
- * @param {string} file
- * @param {map} nodes
- * @param {Object} cache
- * @return {Object} fileHash and query
- */
-async function processFilePageQuery( file, nodes, cache ) {
-
- const contents = fs.readFileSync( file, 'utf8' );
- const match = /export const groqQuery = /.exec( contents );
- if( ! match ) {
- return;
- }
-
- try {
-
- const ast = parser.parse( contents, {
- errorRecovery: true,
- plugins: [ 'jsx' ],
- sourceFilename: file,
- sourceType: 'module',
- } );
- // const fragments = await cache.get( 'groq-fragments' );
- // let fragmentStrings = [];
- // let fragmentFunctions = {};
- let pageQuery = null;
- let queryStart = null;
- let queryEnd = null;
-
- traverse( ast, {
- ExportNamedDeclaration: function( path ) {
-
- const declarator = path.node.declaration.declarations[0];
-
- if( declarator.id.name === 'groqQuery' ) {
-
- queryStart = declarator.init.start;
- queryEnd = declarator.init.end;
- pageQuery = contents.substring( queryStart, queryEnd );
- //pageQuery = declarator.init.quasis[0].value.raw;
-
- // if( declarator.init.expressions.length ) {
- // for( let expression of declarator.init.expressions ) {
-
- // Process string variable
- // if( expression.type === 'Identifier' ) {
- //
- // const variableName = expression.name;
- // if( !! fragments[variableName] ) {
- // fragmentStrings[variableName] = fragments[variableName];
- // }
- //
- // }
-
- // Process function variable.
- // if( expression.type === 'CallExpression' ) {
- //
- // const { callee: { name }, arguments } = expression;
- // let args = []
- //
- // if( !! arguments.length ) {
- // for( let { value } of arguments ) {
- // args.push( value );
- // }
- // }
-
-
- // const functionName = fragments[name];
- //
- // if( !! functionName ) {
- // fragmentFunctions[name] = functionName( ...args );
- // }
- //
- // }
-
- // }
- // }
- }
-
- }
- } );
-
- if( ! pageQuery ) {
- return;
- }
-
- const hash = hashQuery( file );
-
- return {
- fileHash: hash,
- query: JSON.stringify( { unprocessed: pageQuery } ),
- }
- }
- catch( err ) {
- console.warn( err );
- return null;
- }
-
-}
-
-/**
- * Extracts static query from file and returns its hash and result.
- *
- * @param {string} file
- * @param {map} nodes
- * @param {Options} plugin
- * @return {Object} hash and query
- */
-async function processFileStaticQuery( file, nodes, plugin ) {
-
- const contents = fs.readFileSync( file, 'utf8' );
- const match = /useGroqQuery/.exec( contents );
-
- if( ! match ) {
- return;
- }
-
- try {
-
- const ast = parser.parse( contents, {
- errorRecovery: true,
- plugins: [ 'jsx' ],
- sourceFilename: file,
- sourceType: 'module',
- } );
- let staticQuery = null;
-
- traverse( ast, {
- CallExpression: function( path ) {
-
- if( !! path.node.callee && path.node.callee.name === 'useGroqQuery' ) {
-
- staticQuery = path.node.arguments[0].quasis[0].value.raw;
-
- }
- }
- } );
- if( ! staticQuery ) {
- return null;
- }
-
- const hash = hashQuery( staticQuery );
- const result = await runQuery( staticQuery, nodes );
- const json = JSON.stringify( result );
-
- return { hash, json };
-
- }
- catch( err ) {
- console.warn( err );
- return null;
- }
-
-
-
-}
-
-/**
- * Cache result from query extraction.
- * For page queries this caches the query itself.
- * For static queries this caches the results of the query.
- *
- * @param {number} hash Hash to the json file.
- * @param {Object|string} data Data we are caching.
- * @param {string} type Page or static query. Optional. Default 'page'
- */
-async function cacheQueryResults( hash, data, type = 'page' ) {
-
- const reporter = new Reporter();
- reporter.info( `Caching ${type} query: ${hash}` );
-
- const json = typeof data !== 'string' ? JSON.stringify( data ) : data;
-
- if( process.env.NODE_ENV === 'development' ) {
-
- // Probably a more sophisticated Gatsby way of doing this.
- fs.writeFileSync( `${GROQ_DIR}/${hash}.json`, json, err => {
- if( err ) {
- throw new Error( err );
- }
- } );
-
- }
- else {
-
- fs.writeFileSync( `${GROQ_DIR}/${hash}.json`, json, err => {
- if( err ) {
- throw new Error( err );
- }
- } );
-
- }
-
-}
-
-/**
- * Generate a hash based on the query.
- *
- * @param {string} query
- * @return {number}
- */
-function hashQuery( query ) {
- return murmurhash( query );
-}
-
-/**
- * Custom reporter.
- */
-function Reporter() {
-
- this.info = msg => gatsbyReporter.info( `[groq] ${msg}` );
- this.warning = msg => gatsbyReporter.warning( `[groq] ${msg}` );
- this.error = msg => gatsbyReporter.error( `[groq] ${msg}` );
-
-}
-
diff --git a/plugins/gatsby-plugin-groq/murmur.js b/plugins/gatsby-plugin-groq/murmur.js
deleted file mode 100644
index c5225d5..0000000
--- a/plugins/gatsby-plugin-groq/murmur.js
+++ /dev/null
@@ -1,82 +0,0 @@
-// murmurhash2 via https://gist.github.com/raycmorgan/588423
-
-/**
- * Murmurhash.
- *
- * @param {string} str
- * @param {string} seed
- * @return {number}
- */
-module.exports = ( str, seed = 'abc' ) => {
-
- var m = 0x5bd1e995;
- var r = 24;
- var h = seed ^ str.length;
- var length = str.length;
- var currentIndex = 0;
-
- while (length >= 4) {
- var k = UInt32(str, currentIndex);
-
- k = Umul32(k, m);
- k ^= k >>> r;
- k = Umul32(k, m);
-
- h = Umul32(h, m);
- h ^= k;
-
- currentIndex += 4;
- length -= 4;
- }
-
- switch (length) {
- case 3:
- h ^= UInt16(str, currentIndex);
- h ^= str.charCodeAt(currentIndex + 2) << 16;
- h = Umul32(h, m);
- break;
-
- case 2:
- h ^= UInt16(str, currentIndex);
- h = Umul32(h, m);
- break;
-
- case 1:
- h ^= str.charCodeAt(currentIndex);
- h = Umul32(h, m);
- break;
- }
-
- h ^= h >>> 13;
- h = Umul32(h, m);
- h ^= h >>> 15;
-
- return h >>> 0;
- }
-
- function UInt32(str, pos) {
- return (str.charCodeAt(pos++)) +
- (str.charCodeAt(pos++) << 8) +
- (str.charCodeAt(pos++) << 16) +
- (str.charCodeAt(pos) << 24);
- }
-
- function UInt16(str, pos) {
- return (str.charCodeAt(pos++)) +
- (str.charCodeAt(pos++) << 8);
- }
-
- function Umul32(n, m) {
- n = n | 0;
- m = m | 0;
- var nlo = n & 0xffff;
- var nhi = n >>> 16;
- var res = ((nlo * m) + (((nhi * m) & 0xffff) << 16)) | 0;
- return res;
- }
-
- function getBucket(str, buckets) {
- var hash = doHash(str, str.length);
- var bucket = hash % buckets;
- return bucket;
- }
\ No newline at end of file
diff --git a/plugins/gatsby-plugin-groq/package.json b/plugins/gatsby-plugin-groq/package.json
deleted file mode 100644
index ee2784e..0000000
--- a/plugins/gatsby-plugin-groq/package.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "name": "gatsby-plugin-groq",
- "private": true,
- "description": "Hi",
- "version": "0.1.0",
- "author": "Kevin McAloon ",
- "keywords": [
- "gatsby"
- ],
- "license": "MIT",
- "repository": {
- "type": "git",
- "url": ""
- }
-}
diff --git a/src/fragments/index.js b/src/fragments/index.js
deleted file mode 100644
index d12d3a7..0000000
--- a/src/fragments/index.js
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * Put all of your GROQ "fragments" here!
- */
-
-exports.demoString = `
- _id,
- title,
- content
-`;
-
-exports.demoFunction = num => {
-
- if( num === 2 ) {
- return(`
- _id,
- title
- `);
- }
- else {
- return(`
- _id
- `);
- }
-
-}
\ No newline at end of file
diff --git a/src/pages/index.js b/src/pages/index.js
deleted file mode 100644
index d588de5..0000000
--- a/src/pages/index.js
+++ /dev/null
@@ -1,17 +0,0 @@
-import React from 'react';
-
-
-const IndexPage = ( { pageContext } ) => {
-
-
- return(
-
-
-
Go to a page
-
-
- )
-
-}
-
-export default IndexPage
diff --git a/src/templates/Page.js b/src/templates/Page.js
deleted file mode 100644
index c8519a5..0000000
--- a/src/templates/Page.js
+++ /dev/null
@@ -1,25 +0,0 @@
-import React from 'react';
-
-import { demoFunction, demoString } from '../fragments';
-
-
-export const groqQuery = `
- *[ _type == "post" && _id == $_id ] {
- ...
- }[0]
-`;
-export const Page = ( { pageContext } ) => {
-
- const { data } = pageContext;
-
- console.log( data );
-
- return(
-
-
-
Try to add a groqQuery export to this page!
-
- )
-
-}
-export default Page;
\ No newline at end of file