From c9594f769c7b8930241b8fd78e7f63d70b2160c8 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 20 May 2020 09:51:01 -0400 Subject: [PATCH] Match all joins in query other than Sanity assets --- index.js | 38 ++++++++++++++++++++++++-------------- package.json | 2 +- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 8a16b14..0da886b 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index c484a7e..c70bb61 100644 --- a/package.json +++ b/package.json @@ -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 ", "keywords": [ "gatsby"