CloudKit JS: Query filtered by reference

I'm trying to query some records which should be filtered by a given reference.


When I simply hack the reference as value for the filter, this leads to an error.

var query = { recordType: 'Colors',
  filterBy: [{
  fieldName: 'User',
  comparator: 'EQUALS',
  fieldValue: { value: id }
  }]};


The given error looks fine, saying it's impossible to provide a string when a reference is needed.

{
"uuid":"c6b24310-f3e7-4f57-adc5-2411c3ed5a55",
  "serverErrorCode":"BAD_REQUEST",
  "reason":"BadRequestException: Invalid value, expected type REFERENCE but actual value was object of class java.lang.String"
}


But when I construct a 'reference type' JSON object according to the CloudKit Web Services Reference, it doesn't work either.

var query = { recordType: 'Colors',
  filterBy: [{
  fieldName: 'User',
  comparator: 'EQUALS',
  fieldValue: { value: { recordName:representation.id, action:'NONE', zoneID:'_defaultZone' }, type: 'REFERENCE' }
  }]};


Now the response is just an internal server error without further information:

{
 "uuid":"9b066fe8-5f4d-4856-a8f3-dda652a78399",
 "serverErrorCode":"INTERNAL_ERROR"
}


Building the query with a reference type instead of the value leads to the same result, so my guess is that the filter is in the wrong format.

Unfortunately neither the CloudKit Catalog nor the CloudKit JS documentation show how to build a query filtered by reference.

Since a reference is the recommended way to handle To–Many–Relations I cannot believe this query feature isn't implemented yet.


So how do I build a query in CloudKit JS which filters by a field containing a reference?

I can't log in at the moment but have you tried only passing the id as a string?

You have set a "filter" index on that field haven't you?


I hadn't, took me half a day to work out.....

Nope, I also tried to pass a reference dictionary as value as well.

Only passing the string leads to the mentioned error 'Passing string but expected reference'

I can only select 'query' for the reference type field and this one is activated, yes.

Additionally I don't see a 'filter' index at any field type.

Have you tried removing the action parameter?

Were you ever able to resolve this one? I'm running into the same issue and need to be able to query by reference.

What I found to be working (actually thanks to one of mafe's ideas) is this structure containing both value and recordName in nested objects:


filterBy: [{
    fieldName: 'ParentParty',
    fieldValue: { value: { recordName: parentPartyRecordId } },
    comparator: 'EQUALS'
}],
CloudKit JS: Query filtered by reference
 
 
Q