sanity-io / sanity-algolia

Utilities for indexing Sanity documents in Algolia
MIT License
67 stars 16 forks source link

null value not allowed for objectID #38

Open filipnathanel opened 1 year ago

filipnathanel commented 1 year ago

Hi There, I am setting up a sanity -> algolia sync with the help of your package.

I am experiencing the following problem:

Given the following hook transformation (identical as in the readme)

{
  "transactionId": _rev,
  "projectId": sanity::projectId(),
  "dataset": sanity::dataset(),
  "ids": {
    "created": [
        select(before() == null && after() != null => _id)
    ],
    "deleted": [
      select(before() != null && after() == null => _id)
    ],
    "updated": [
      select(before() != null && after() != null => _id)
    ],
    "all": [
      _id
    ]
  }
}

the webhook sends following data to my endpoint:

{
  transactionId: 'OP7E1yWFIkgUuKpJgXDaeNV',
  projectId: 'k2o72wlw-this-is-random',
  dataset: 'staging',
  ids: {
    created: [ null ],
    deleted: [ null ],
    updated: [ '25fc2cae-d872-11ed-afa1-0242ac120002 ],
    all: [ '25fc2cae-d872-11ed-afa1-0242ac120002' ]
  }
}

Please note the

    created: [ null ],
    deleted: [ null ],

In such case the library will try to create delete entry on Algolia side with the objectID of null

Which in turn will result in ApiError:

null value not allowed for objectID near line:1 column:61

When I am emulating the webhook data without the null entries in the created and deleted everything works just fine.

filipnathanel commented 1 year ago

For anyone hitting against the same problem, changing the projection fixed it for me:

{
  "transactionId": _rev,
  "projectId": sanity::projectId(),
  "dataset": sanity::dataset(),
  "ids": {
    "created":  
      select(
        before() == null && after() != null => [_id],
        before() != null || after() == null => []
      ),

    "deleted": 
      select(
        before() != null && after() == null => [_id],
        before() == null || after() != null => []
      ),

    "updated": 
      select(
        before() != null && after() != null => [_id],
        before() == null || after() == null => []
      ),

    "all": [
      _id
    ]
  }
}
davidy3k commented 1 year ago

For anyone hitting against the same problem, changing the projection fixed it for me:

{
  "transactionId": _rev,
  "projectId": sanity::projectId(),
  "dataset": sanity::dataset(),
  "ids": {
    "created":  
      select(
        before() == null && after() != null => [_id],
        before() != null || after() == null => []
      ),

    "deleted": 
      select(
        before() != null && after() == null => [_id],
        before() == null || after() != null => []
      ),

    "updated": 
      select(
        before() != null && after() != null => [_id],
        before() == null || after() == null => []
      ),

    "all": [
      _id
    ]
  }
}

Had the same problem and this works. Thank you!

ashtonlance commented 1 year ago

For anyone hitting against the same problem, changing the projection fixed it for me:

{
  "transactionId": _rev,
  "projectId": sanity::projectId(),
  "dataset": sanity::dataset(),
  "ids": {
    "created":  
      select(
        before() == null && after() != null => [_id],
        before() != null || after() == null => []
      ),

    "deleted": 
      select(
        before() != null && after() == null => [_id],
        before() == null || after() != null => []
      ),

    "updated": 
      select(
        before() != null && after() != null => [_id],
        before() == null || after() == null => []
      ),

    "all": [
      _id
    ]
  }
}

Had the same problem and this works. Thank you!

I'm running into the same issue and I've updated the Projection settings in the webhook config.

This is the content that my endpoint is receiving, so it seems that it should have an ID to work with?

req.body {
  transactionId: 'TUi1yDXwDXw7DHV1cKifdQ',
  projectId: 'xxxxxx',
  dataset: 'production',
  ids: {
    all: [ '604aa1d1-75a6-43cb-ad9b-c8ab459d4945' ],
    created: [ '604aa1d1-75a6-43cb-ad9b-c8ab459d4945' ],
    deleted: [ null ],
    updated: [ null ]
  }
}
davidy3k commented 1 year ago

I had to delete the old webhook and add a new one with the updated projection, for some reason just updating the webhook didn't work for some reason. Hope this helps.

surjithctly commented 1 year ago

Thank you @filipnathanel for the solution.

Created a new webhook (!!MUST!!) as per @davidy3k (thanks man) and added the query provided by @filipnathanel and it worked for me.

@runeb Time to update the docs / webhook share link?