builder icon indicating copy to clipboard operation
builder copied to clipboard

`$elemMatch` operator does not work in the new SDK

Open kaceycleveland opened this issue 6 months ago • 1 comments

Describe the bug A clear and concise description of what the bug is.

We are in the process of porting over to the new SDK (which I saw was marked as 1.0.0, nice!) Unfortunately, the old functionality of the API could not be replicated with the new API with $elemMatch.

To Reproduce Not a concrete reproduction but my test script is as follows:

// @ts-nocheck ignore all issues as this is a script
import { builder } from '@builder.io/sdk';
import { fetchEntries } from '@builder.io/sdk-react';

builder.init(process.env.NX_BUILDER_IO_PUBLIC_KEY);

const callBuilder = async () => {
  const results = await builder.getAll('blog-article', {
    options: {
      sort: {
        createdDate: -1,
      },
      enrich: true,
    },
    query: {
      'data.categories': {
        $elemMatch: {
          'category.id': {
            $in: JSON.stringify(['d491820ec5da422199336a73f388f354']).trim(),
          },
        },
      },
    },
    limit: 5,
    omit: 'data.blocks',
  });
  const resultsNew = await fetchEntries({
    model: 'blog-article',
    apiKey: process.env.NX_BUILDER_IO_PUBLIC_KEY!,
    options: {
      sort: {
        createdDate: -1,
      },
      enrich: true,
    },
    query: {
      'data.categories': {
        $elemMatch: {
          'category.id': {
            $in: JSON.stringify(['d491820ec5da422199336a73f388f354']).trim(),
          },
        },
      },
    },
    limit: 5,
    omit: 'data.blocks',
  });

  const pages = results.map((res) => res.data.title);
  pages.forEach((article) => console.log(article));

  if (results.length && results[0]) {
    console.log(Object.keys(results[0].data));
    console.log(results[0].data.categories);
  }

  console.log('NEW -------------');

  const pagesNew = resultsNew.map((res) => res.data.title);
  pagesNew.forEach((article) => console.log(article));

  if (resultsNew.length && resultsNew[0]) {
    console.log(Object.keys(resultsNew[0].data));
    console.log(resultsNew[0].data.categories);
  }
};

callBuilder();

Expected behavior A clear and concise description of what you expected to happen. The return in the repo test script should be the same results but the new API returns no results. It would be helpful to have a debug option attached to the builder API client to output some additional information such as the query keys being passed to the request or something.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

kaceycleveland avatar Feb 12 '24 21:02 kaceycleveland

Posting for context, https://forum.builder.io/t/new-sdk-returning-different-then-the-old-sdk/4975/7

There is an example of getting it working with the new SDK via the options param and encoding URLSearchParams but its not great.

I really think this is a large issue with the new SDK as it blocks a large portion of querying.

kaceycleveland avatar Feb 16 '24 16:02 kaceycleveland

Update from the above post, the work around and the example query provided in the above thread do not work.

From my POV, the $elemMatch operator seems to be broken or at least I have yet to be able to find a query that returns the correct results.

Mentioned in the thread, but I really feel like this is an important functionality to cover especially when we were told we should be using the new SDK. I would take a stab at creating a PR to address this in the new SDK but I would first need to find out if its even a problem with the SDK itself or if its the new content API. So far, I have not been able to get it working properly with the API query itself so that would be the first step if someone could provide a working example.

kaceycleveland avatar Feb 20 '24 01:02 kaceycleveland

@kaceycleveland Thanks for reporting this bug with great detail. We will let you know when we have an update on the root cause and/or have a workaround for you.

samijaber avatar Feb 28 '24 18:02 samijaber

We encountered the same issue with the $elemMatch. Looking forward to a resolution.

chub7 avatar Feb 29 '24 14:02 chub7

Hey y'all, any updates on this?

mikehuebner avatar Apr 08 '24 20:04 mikehuebner

A fixed was deployed in 1.0.27 https://github.com/BuilderIO/builder/releases/tag/%40builder.io%2Fsdk-react%401.0.27

samijaber avatar May 03 '24 15:05 samijaber