Loading...
Please wait a moment

From a MongoDB Query to a GraphQL Query and Schema

Moving a MongoDB-backed API to GraphQL? Paste a find() call and you get the GraphQL operation that asks for the same documents, with the filter turned into arguments, plus a schema that validates. Paste documents instead and you get a type worked out from all of them. It is a starting point for your schema and resolvers, not a running server.

How to Convert MongoDB to GraphQL

Step 1

Paste a query or some documents

Paste a find(), findOne() or countDocuments() call the way you would type it in mongosh or Compass, a filter object on its own, or a list of documents (an insertMany() call or a JSON export). Upload and Sample work too.

Example: a find() query

db.orders.find(
  { status: "shipped", total: { $gte: 100 } },
  { status: 1, total: 1 }
).sort({ total: -1 }).limit(20)
Step 2

Read the GraphQL query and schema

A query turns into a GraphQL operation with one argument per filter condition, plus the Query type and document type it needs. Documents turn into a type whose fields come from every document in the list. The output refreshes about half a second after you stop typing.

What the example above turns into

# Query
query FindOrders {
  orders(status: "shipped", total_gte: 100, orderBy: [{total: DESC}], limit: 20) {
    _id
    status
    total
  }
}

# Schema
enum SortDirection {
  ASC
  DESC
}

"Sort order for orders. Pass a list; the first entry sorts first."
input OrderOrderBy {
  total: SortDirection
}

type Query {
  orders(
    status: String
    "MongoDB filter: total $gte"
    total_gte: Int
    orderBy: [OrderOrderBy!]
    limit: Int
  ): [Order!]!
}

type Order {
  _id: ID!
  status: String
  total: Int
}

The type only has _id and the projected fields; add the rest of your document's fields, or paste a few documents to get them all.

Step 3

Check the notes at the top

Anything that needed a decision is listed in a comment block: filter parts with no GraphQL argument form (like $or), fields whose type the query didn't show, custom scalars such as Long or DateTime, and renamed fields.

Step 4

Copy or download

Copy the result or download it as a .graphql file, then add the remaining fields of your documents and write the resolvers that run the MongoDB query.

Frequently Asked Questions

How does a MongoDB filter become GraphQL arguments?

Each condition becomes one argument on the collection field. An equality like status: "active" becomes status: String, and operators get a suffix: age: { $gte: 21 } becomes age_gte: Int, $in becomes a list argument such as tags_in: [String!], and $exists becomes a Boolean. .sort() becomes an orderBy list with ASC/DESC, .limit() becomes limit and .skip() becomes offset. The resolver still has to turn the arguments back into a MongoDB filter.

Which MongoDB filter parts are left out?

$or, $nor, $text, $expr, $where, $elemMatch, geo operators and similar can't be expressed as one flat set of arguments, so they are left out and named in the notes at the top. $and is merged into the other arguments. Regular expressions become a String argument with the pattern; their flags (like i) are mentioned in the notes.

Why do long IDs and dates use custom scalars?

GraphQL Int is a 32-bit number, so a value like 8901240544102066246 or a NumberLong does not fit and would be rejected by a spec-following server. Whole numbers past 32 bits get a Long scalar (and BigInt beyond 64 bits), NumberDecimal gets Decimal, and ISODate gets DateTime. ObjectId becomes the built-in ID type. Each custom scalar is declared with a description, and libraries such as graphql-scalars have ready implementations. Numbers are copied as typed, so 14.50 stays 14.50 and "00501" stays a string.

Where do the fields of the generated type come from?

From the projection when there is one, plus _id unless you exclude it. A query doesn't show your whole document, so fields whose type isn't visible in the filter become String and are listed in the notes. To get complete types, paste some real documents instead: every document in the list is read, and a field missing or null in any of them is left nullable.

What happens to field names GraphQL doesn't allow?

GraphQL names can only use letters, digits and underscores and can't start with a digit or two underscores. A field like first-name becomes first_name, and the original MongoDB name is written into the field description so you can map it in a resolver. Names that clash get a number, and type names never reuse built-ins such as String.

Is my query or data sent anywhere?

No. The conversion runs in your browser while you type, nothing is uploaded, and nothing is kept. Reload the page and the editor starts empty.