Skip to content

Commit

Permalink
Match all joins in query other than Sanity assets
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcaloon committed May 20, 2020
1 parent 49826f2 commit c9594f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
38 changes: 24 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,36 @@ function processJoins( query ) {
const refOption = !! autoRefs ? '._ref' : '';

const search = `\\S+->\\w*`;
const regex = /\S+->\w*/gm;
const regex = new RegExp( search, 'g' );
const matches = [ ... processedQuery.matchAll( regex ) ];

for( let match of regex.exec( processedQuery ) ) {
if( !! matches.length ) {
for( let match of matches ) {

let field = match.replace( '->', '' );
let replace = null;
const matchText = match[0];

// Single refs.
if( ! field.includes( '[]' ) ) {
replace = `*[ ${matchField} == ^.${field}${refOption} ][0]`;
}
// Arrays.
else {
replace = `*[ ${matchField} in ^.${field}${refOption} ]`;
}
// For now we're skipping Sanity .assets since they work by default.
if( matchText.includes( '.asset->' ) ) {
continue;
}

processedQuery = processedQuery.replace( match, replace );
const field = matchText.replace( '->', '' );
let replace = null;

}
// Single refs.
if( ! field.includes( '[]' ) ) {
replace = `*[ ${matchField} == ^.${field}${refOption} ][0]`;
}
// Arrays.
else {
replace = `*[ ${matchField} in ^.${field}${refOption} ]`;
}

processedQuery = processedQuery.replace( matchText, replace );

}

}
}

return processedQuery;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-plugin-groq",
"description": "Gatsby plugin for using GROQ in place of GraphQL",
"version": "1.0.0-alpha.5",
"version": "1.0.0-alpha.7",
"author": "Kevin McAloon <[email protected]>",
"keywords": [
"gatsby"
Expand Down

0 comments on commit c9594f7

Please sign in to comment.