Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The use of the refetch with GROQ param refs since GROQ does not support conditional params #580

Open
toddpadwick opened this issue Dec 1, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@toddpadwick
Copy link

I have a blog listing page which has the following params:

  • $industrySlug which is used as an optional filter, whereby if applied it filters blog articles by a reference field. so && industry->slug.current == $industry
  • $offset - which is used to calculate pagination

I might be missing something, but currently, according to this line of the docs, the sanity fetch module listens to changes to the computed properties past into the params argument to trigger a refetch. However, after speaking with the Sanity team in Slack to confirm, GROQ does not support conditional params. Meaning if a param is empty, I'll end up with [_type in ["article"] && industry->slug.current == null, which will return no articles instead of all articles. What I of course need is [_type in ["article]] if no industry is passed. As GROQ does not support conditionals params, any conditional logic has to be handled before the GROQ string is concatenated and passed into the query, however, the module won't be listening to those changes.

const blogQuery = `*[_type == "article"${industry ? ' && industry->slug.current == ${industrySlug}' : ''}]`; // contains conditional logic if industry exists

If there was a way to manually refetch, this might suffice, but if there is a way to listen for changes to the query string that would probably be best otherwise we're doubling up on watchers.

@toddpadwick toddpadwick added the bug Something isn't working label Dec 1, 2022
@toddpadwick
Copy link
Author

Updating you on a failed attempted workaround:
tried using the refetch hook, however, it doesn't regenerate the query string, so the issue still remains:

<script setup>
  import querySrc from '~/queries/groq/entries';

  const limit = ref(12);

  const config = useRuntimeConfig();
  const route = useRoute();

  let options = {types:props.types, limit:limit.value}; // setting base options

  const page = route.params?.page ? parseInt(route.params.page) : 1;
  console.log(page);
  if (page && page > 1) {
    options.offset = limit.value * (page - 1);
  }
  if (route.query.industry) { // listening for filters applied to the route query
    options.industry = route.query.industry;
  }

  if (route.query.role) { // listening for filters applied to the route.query
    options.role = route.query.role;
  }

  const entriesQuery = querySrc(options); // setting the options in my GROQ query concatonation.

  const {pending, data:entries, error, refresh} = useSanityQuery(entriesQuery);


  watch(route, () => {

    refresh(); // This refreshes the previous entriesQuery, not the updated one with new params.
    
  });

</script>

@danielroe danielroe removed their assignment Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants