Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which syntax for filtering queries in Graphiql?

New to graphql and started playing with graphiql. I have a Product data type

  type Product {
    _id: String
    name: String
    price: Float
  }

I have mongodb populated with some products

{ "_id" : ObjectId("5a61b31e009c0bef5d724a47"), "name" : "GROUND COFFEE", "price" : 3.06 }
{ "_id" : ObjectId("5a61b31e009c0bef5d724a48"), "name" : "PORK SAUSAGES 500g",  "price" : 4.39 }
{ "_id" : ObjectId("5a61b31e009c0bef5d724a49"), "name" : "MILK UHT 1LT", "price" : 1.29 }
{ "_id" : ObjectId("5a61b31e009c0bef5d724a4a"), "name" : "HEINEKEN PREMIUM LIGHT 33CL", "price" : 1.61 }

In graphiql I can execute a query like this

query {
  products{
     name,
    price
  }
}

Query result is

{
  "data": {
    "products": [
      { "name" : "GROUND COFFEE", "price" : 3.06 },
      { "name" : "PORK SAUSAGES 500g",  "price" : 4.39  },
      { "name" : "MILK UHT 1LT", "price" : 1.29 },
      { "name" : "HEINEKEN PREMIUM LIGHT 33CL", "price" : 1.61 }
    ]
  }
}

Wow, that's fine. So next step is: now I want only products with price greater than 2.0 And I face two problems I do not know how to solve

1) Which is the correct syntax to implement filtering?
2) How comparison operators have to be specified? And how this specification maps to mongodb comparison operators?

I tried with

query {
  products(filter:{price: {gt:2.0} }){
     name,
    price
  }
}

but I get an error for "filter":

Unknown argument "filter" on field "products" of type "Query". I find no way to do this ...

UPDATE @diego

const typeDefs = [`
  type Query {
    product(_id: String): Product
    products: [Product]
  }

  type Product {
    _id: String
    name: String
    price: Float
  }

  type Mutation {
    createProduct(name: String, price: Float): Product
  }

  schema {
    query: Query
    mutation: Mutation
  }
`];

const resolvers = {
  Query: {
    product: async (root, {_id}) => {
      return prepare(await Products.findOne(ObjectId(_id)))
    },
    products: async () => {
      return (await Products.find({}).toArray()).map(prepare)
    },
  },
  Mutation: {
    createProduct: async (root, args, context, info) => {
      const res = await Products.insert(args)
      return prepare(await Products.findOne({_id: res.insertedIds[1]}))
    },
  },
}
like image 313
kranz Avatar asked Jan 19 '18 11:01

kranz


People also ask

How do you filter a query in GraphQL?

You can filter query results to find objects with one or more specified values using the in keyword. This keyword can find matches for fields with the @id directive applied. The in filter is supported for all data types such as string , enum , Int , Int64 , Float , and DateTime .

How do you query in GraphiQL?

The GraphiQL client allows you to create variables for use in your queries. To add a query variable, click the Query Variables pane and enter a JSON object that defines your variable. To use a variable in your query, prepend the $ character to your variable name and use it to replace the desired value.

How do you use a query filter?

To filter data in a query, open it in Datasheet View, click the down-arrow at the top of a column, and select a filter option. Here's how it looks in an Access app: You can select multiple values from the list, but in an app, the filter list closes each time you select an option.

Does GraphQL support filtering?

Searching and filtering is a standard part of any GraphQL API. In this article, we'll learn how to add filtering capabilities to your API by walking through a few real-world examples.


1 Answers

First you would need to change the Query type definition

products(filter: String): [Product]

Now it accepts a string filter and you can use it in the resolver

products: async (_, {filter}) => {
  const query = JSON.parse(filter)
  return (await Products.find(query).toArray()).map(prepare)
}

Then you can call your query like:

query {
  products(filter: "{\"price\": {\"gt\":2.0} }") {
    name,
    price
  }
}

I do not know if there is a way to do this without changing the query itself.

like image 78
Diego Avatar answered Sep 28 '22 21:09

Diego