Skip to content

Commit

Permalink
Merge branch 'feature-groq-explorer'
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcaloon committed May 9, 2020
2 parents e0fd222 + 8c23a25 commit 79d90ce
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions plugins/gatsby-plugin-groq/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 );
// }
// } );

}

}
}
Expand Down
18 changes: 17 additions & 1 deletion src/fragments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ exports.demoString = `
_id,
title,
content
`;
`;

exports.demoFunction = num => {

if( num === 2 ) {
return(`
_id,
title
`);
}
else {
return(`
_id
`);
}

}
13 changes: 12 additions & 1 deletion src/templates/Page.js
Original file line number Diff line number Diff line change
@@ -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(

<div>
<h1>Try to add a groQuery export to this page!</h1>
<h1>Try to add a groqQuery export to this page!</h1>
</div>
)

Expand Down

0 comments on commit 79d90ce

Please sign in to comment.