Stitch, GraphQL, and Atlas Search

I am looking to mongodb stitch to expose my data through graphql. I went through the initial getting started and see. the 2 queries. I want to use Altas search which requires aggregation. How do I do this from my graphql endpoint?

Hi Fred – In order to do this, you’ll want to create a custom resolver and corresponding function which will use the $search aggregation stage. One caveat for this – We currently only allow $search in functions set to run as System so you’ll want to do any permission checking in the function logic. That being said, we plan on supporting $search alongside our Rules soon.

Thanks for the response, Drew.

That is what I suspected. I am following this example create a custom resolver. I get an error on saving as shown:

Hi Fred – There is currently a bug in that form (fix pending release) causing bsonType to fail validation, you should be able to use ‘type’ instead of ‘bsonType’.

Thanks Drew. Changing to “type” worked. I have a basic custom resolver talking to function that I can hit through graphql now. Next let me see if I can get aggregate working. (I am using sample data sample_mflix)

Watched your session on MongoDB live. Nice job.

I was having trouble returning a collection (in response to an aggregation with $search and autocomplete) from my function that was called by custom resolver. I got error in graphiql.

I was missing cursor.toArray(). All good now. I have been using the sample_mflix database and movies collection. I will now implement in my own data.

Got it working.

  1. created realm app
  2. connect to my db/collection
  3. added rules, user (email/pw provider)
  4. created custom resolver, linked to my function (doing aggregate $search w/autocomplete)
  5. perform query in grapiql. nice! search over 550,000 docs in <1 sec. Shell says 4ms.
  6. authenticate with REST API email/pw and get access_token
  7. use external graphql tool (altair), set authorization header to Bearer <access_token>
  8. bob’s your uncle. graphql externally works too!

Edit: I did manage to get this working today. It was a classic PEBKAC. That said, the question about the System context vs User context is still one that I would love an update on.

Unfortunately, unlike Fred, I have not had as much luck getting this to work. I’ve got a custom resolver defined and pointing to my function. I believe that the function is not being called as I’m not seeing any log statement for the function, though that’s just a guess.

The function itself works as expected when testing it in the function editor. I’m able to pass in a string and it returns me the autocomplete results. These results should match the custom payload type that I defined. (Not sure if Realm would throw an exception if they didn’t) I’ve tried all of the permutations that I could imagine the custom resolver wanting. Nothing seems to come back out to the resolver. Is there any reason an async function would not be able to be used with a custom resolver?

Additionally, I’m having to run this function as System rather than my authenticated user to not have the aggregate step throw an exception (there’s no message, either, just a blank exception). I’m aware that back in June, System was the only way to call $search, but from the docs, it says that it works in the User context but will run as a System user. So I don’t really know what is happening here.

Any and all advice would be gratefully appreciated.