From 8c23a251a26a483f376ae45ceeab49937f438478 Mon Sep 17 00:00:00 2001 From: Kevin Date: Fri, 8 May 2020 21:22:42 -0400 Subject: [PATCH] WIP --- README.md | 6 +++--- plugins/gatsby-plugin-groq/index.js | 30 ++++++++++++++++++++++++----- src/fragments/index.js | 18 ++++++++++++++++- src/templates/Page.js | 13 ++++++++++++- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 614e81f..83399ef 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,10 @@ Similar to page queries, all files are watched for changes and whenever there is ## ⌛ TO DO (random order) -- Get rid of relative directories -- Work on issues with joins -- Need to execute code before traversing for GROQ fragments / string interpolation +- ~~Get rid of relative directories~~ +- ~~Work on issues with joins~~ we might be limited here - GROQ explorer +- Run fragment functions before interpolating into queries - Experiment with other data sources (WordPress) - Set up an option to auto-resolve references? - Clean up spotty caching issues after running development diff --git a/plugins/gatsby-plugin-groq/index.js b/plugins/gatsby-plugin-groq/index.js index fbb7d44..64c7d94 100644 --- a/plugins/gatsby-plugin-groq/index.js +++ b/plugins/gatsby-plugin-groq/index.js @@ -1,5 +1,9 @@ const groq = require( 'groq-js' ); const murmurhash = require( './murmur' ); +const parser = require( '@babel/parser' ); +const traverse = require( '@babel/traverse' ).default; + +const ROOT = process.env.INIT_CWD; /** * Hook to mimic Gatsby's static query. @@ -17,7 +21,7 @@ exports.useGroqQuery = query => { if( process.env.NODE_ENV === 'development' ) { try { - const result = require( `../../.cache/groq/${hash}.json` ); + const result = require( `${ROOT}/.cache/groq/${hash}.json` ); return result; } catch( err ) { @@ -28,7 +32,7 @@ exports.useGroqQuery = query => { else { try { - const result = require( `../../public/static/groq/${hash}.json` ); + const result = require( `${ROOT}/public/static/groq/${hash}.json` ); return result; } catch( err ) { @@ -80,9 +84,25 @@ exports.runQuery = async ( rawQuery, dataset, options = {} ) => { query = query.replace( pattern, value ); } // Process function. - // else if( typeof value === 'function' ) { - // - // } + else if( typeof value === 'function' ) { + + // const ast = parser.parse( query, { + // errorRecovery: true, + // plugins: [ 'jsx' ], + // sourceType: 'module', + // } ); + // + // traverse( ast, { + // Identifier: function( path ) { + // + // if( path.node.name === name ) { + // + // } + // console.log( '=======', path.node.name ); + // } + // } ); + + } } } diff --git a/src/fragments/index.js b/src/fragments/index.js index 6bdfe50..d12d3a7 100644 --- a/src/fragments/index.js +++ b/src/fragments/index.js @@ -6,4 +6,20 @@ exports.demoString = ` _id, title, content -`; \ No newline at end of file +`; + +exports.demoFunction = num => { + + if( num === 2 ) { + return(` + _id, + title + `); + } + else { + return(` + _id + `); + } + +} \ No newline at end of file diff --git a/src/templates/Page.js b/src/templates/Page.js index dc211cf..c8519a5 100644 --- a/src/templates/Page.js +++ b/src/templates/Page.js @@ -1,12 +1,23 @@ 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 groQuery export to this page!

+

Try to add a groqQuery export to this page!

)