Custom query resolvers supporting the generated query input

I have a simple query resolver that finds all Documents created by a User.
e.g.

query {
  users {
    documents {
      name
    }
  }
}

Under the hood it does smth like this:

const requesterOpportunitiesResolver = (_, source) => context.services.get('mongodb-atlas').db('xxxl').collection('documents').find({ user: source._id }).toArray()

Now the question is how do I support all the filtering options that the generated documents query supports?
Manually map everything from the input to some mongodb queries??

E.g. I want smth like this:

query {
  users {
    documents (query: {tags_in: ["tag1", "tag2"]}) {
      name
    }
  }
}

Basically the way I imagine it could work is if there was a function to generate mongodb query based on input argument.

1 Like

Hi! You should be able to do this by defining a “custom input type” (ex here - https://docs.mongodb.com/realm/graphql/custom-resolvers#define-the-input-type)

You would probably define the input like this:

  "bsonType": "object",
  "title": "requesterOpportunitiesResolver",
  "properties": {
    "tags_in": {
      "bsonType": "array",
       "items": 
          { 
           "bsonType": "string"
          }
      
    }
  }
}

Is this what you’re looking for?

Hi @Sumedha_Mehta1! Thanks for your reply!
I understand that I can implement by hand all possible filter types. However it would be a lot of work to support all possible filter types consistently with how generated field filters work.

How I imagined it could work: you mark an input with some existing type, e.g. "input_type_format": "UserQueryInput", and then in the resolver function you’d have some function exposed that would turn your input into a mongodb query object, e.g. a kind of query builder: context.services.get('mongodb-atlas').db('xxxl').collection('documents').find(QueryHelper.build(input))

1 Like