Simplest usage of GraphQL using WHERE, AND, OR while using a Query Parameter - graphql

I'm creating a search query for a simple search on a site using GraphQL.
I've been looking for the absolutely simplest working examples of GraphQL but the questions/examples I've found on Stack Overflow are either too simple, too specific, or way too technical including too many unnecessary bits from the API.
The patterns of when to use the {}, when to use the where, and when optional naming come into play seem to disrupt the patterns that are explained in the docs.
Any insight here would be much appreciated.

Great question.
Here's how I would start. I will assume you have setup a database with Products and those Products have a name and a description.
First - here's how you get all the products (you will be inputting this into the GraphQL playground).:
query {
allProducts {
name
description
}
}
Second - here's how you get a product with a specific name:
query {
allProducts (where: {name: "Nike Air VaporMax"}){
name
description
}
}
Third - here's how to introduce "contains" as in name or description contains "nike". The _i suffix means case insensitive.
query {
allProducts (where: {name_contains_i: "nike"}){
name
description
}
}
Fourth - here's how to introduce an OR (note the commas and the container curly brackets):
query {
allProducts (where: {
OR: [{description_contains_i:"shoes"}, {name_contains_i:"shoes"}]
}
)
{
name
description
}
}
Fifth - here's how to introduce the AND (same as above, note the comma and the curly brackets):
query {
  allProducts (where: {
AND: [{description_contains_i:"shoes"}, {name_contains_i:"shoes"}]
    }
  )
    {
    name
    description
  }
}
Sixth - here is how to start introducing variables - we'll use this with a WHERE + OR:
query ($varTest: String!) {
allProducts(
where: {
OR: [{ description_contains_i: "shoes" }, { name_contains_i: $varTest }]
}
) {
name
description
}
}
And !important! for the above, you will need to fill in the Query Variables:
{
"varTest": "Nike"
}
In case, you're not familiar with the placement of where to put the Query Variable, it will roughly look like this (look for the second window in which to place the Query Variables.)
Seventh - here is the kicker. You can optionally name these queries. The disruption in the pattern consistency threw me off initially. Let me add it here with a pretty obvious name so you can see it too:
query THIS_IS_MY_COOL_QUERY_NAME($varTest: String!) {
allProducts(
where: {
OR: [{ description_contains_i: "shoes" }, { name_contains_i: $varTest }]
}
) {
name
description
}
}
Eight - bonus. You won't need this BUT I want to introduce it here so it doesn't throw you off in the future. When you submit the query, you can assign your own name for the returned array of returned objects. Don't let this previous sentence confuse you, I'll give you examples of the returned array so it's clear.
Here is the Eight query (don't forget to use a Query Variable as you did in the Seventh example). I'll add a pretty obvious name directly in the query:
query THIS_IS_MY_COOL_QUERY_NAME($varTest: String!) {
resultsWillBeReturnedAsArrayWithThisName: allProducts(
where: {
OR: [{ description_contains_i: "shoes" }, { name_contains_i: $varTest }]
}
) {
name
description
}
}
The results from previous query (Seventh) will look like this:
{
"data": {
"allProducts": [
{
"name": "Air Jordan 1",
"description": "Wow - there are shoes!"
},
{
"name": "Nike Blazer Mid",
"description": "Very nice!"
},
{
"name": "Shoes",
"description": "These are shoes!"
}
]
}
}
But the results from the Eight Query will look like this (notice how the name you introduced will come back to you from GraphQL). :
{
"data": {
"resultsWillBeReturnedAsArrayWithThisName": [
{
"name": "Air Jordan 1",
"description": "Wow - there are shoes!"
},
{
"name": "Nike Blazer Mid",
"description": "Very nice!"
},
{
"name": "Shoes",
"description": "These are shoes!"
}
]
}
}
That should give you a solid building block to understanding GraphQL.

Related

Why does this AWS AppSync list operation using OR return an empty list even when part of the OR returns truthy values?

Context:
I am trying to query for all notifications sent or received by a user in my mobile app, and am getting results that (I think) show that AWS AppSync's OR filtering is slightly broken (or that I do not understand how it works)
Note that I am performing these queries using AWS AppSync Queries, but the results are consistent when using their GUI or by sending the queries from the React Native app
Here is my list query using the OR statement
query listAllNotifsForUser {
listNotifications(filter: {sentUserID: {eq: "arbitrary-id-1"}, or: {receivedUserID: {eq: "arbitrary-id-1"}}}) {
items {
id
}
nextToken
}
}
This query returns
"data": {
"listNotifications": {
"items": [],
"nextToken": null
}
Here is my query when listing specifically notifications that have the sentUserID equal to arbitrary-id-1 (no OR statement, only the first half of the OR filter from above)
query listAllNotifsForUser {
listNotifications(filter: {sentUserID: {eq: "arbitrary-id-1"}}) {
items {
id
}
nextToken
}
}
and here is the result from that query
{
"data": {
"listNotifications": {
"items": [
{
"id": "88d204c8-7346-4f69-bc6a-c1e5db1ce5f4"
},
{
"id": "29e03351-75f0-46b2-933b-c3cca43a6067"
},
{
"id": "e21cf81a-7cb3-4331-90af-6ef266f75820"
},
{
"id": "17b42150-ae7c-4852-a58c-85d73ed2e247"
}
],
"nextToken": null
}
}
}
Notice the ONLY difference between these two queries is the removal of the 'or' and the second half of the boolean check, which from basic knowledge of programming, one would not imagine this should ever limit the results compared to a single boolean statement
Any thoughts?
I did this on my AppSync console and it worked:
query MyQuery {
listJobListings(filter: {or: [{ city: {eq: "Chongqing City"} }, { city: {eq: "Beijing"} }]}) {
nextToken
items {
city
}
}
}
Which means you'll need to do this:
query listAllNotifsForUser {
listNotifications(filter: {or: [{ sentUserID: {eq: "user-id"} }, { sentUserID: {eq: "user-id"} }]}) {
items {
id
}
nextToken
}
}
More information here

grpc/protobuffer ask for specific fields

GraphQL lets you ask for specific fields, the response contains only the fields that you had asked for. For example:
a graphql query like:
{
hero {
name
}
}
will return:
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
where as a graphQl query like:
{
hero {
name
friends {
name
}
}
}
would return:
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke"
},
{
"name": "Han Solo"
},
{
"name": "Leia"
}
]
}
}
}
Is there a similar mechanism/library/pattern that can be used in gRPC to achieve the same?
FieldMask is similar in protobuf. It is a list of fields to retain, so the first example would be paths: "hero.name" and the second would be paths: ["hero.name", "hero.friends.name"].
It is probably most frequently used to specify which fields should be changed in an update. But it can equally be used to specify the fields that should be returned.
The server can either process the FieldMask directly (e.g., only using the listed fields in a SELECT SQL query), or it can retrieve all the information and filter the result using FieldMaskUtil.merge() to copy just the requested fields into a new proto message to return to the client.

How to get name/confidence individually from classify_text?

Most of the other methods in the language api, such as analyze_syntax, analyze_sentiment etc, have the ability to return the constituent elements like
sentiment.score
sentiment.magnitude
token.part_of_speech.tag
etc etc etc....
but I have not found a way to return name and confidence in isolation from classify_text. It doesn't look like it's possible but that seems weird. Am missing something? Thanks
The language.documents.classifyText method returns a ClassificationCategory object which contains name and confidence. If you only want one of the fields you can filter by categories/name or categories/confidence. As an example I executed:
POST https://language.googleapis.com/v1/documents:classifyText?fields=categories%2Fname&key={YOUR_API_KEY}
{
"document": {
"content": "this is a test for a StackOverflow question. I get an error because I need more words in the document and I don't know what else to say",
"type": "PLAIN_TEXT"
}
}
Which returns:
{
"categories": [
{
"name": "/Science/Computer Science"
},
{
"name": "/Computers & Electronics/Programming"
},
{
"name": "/Jobs & Education"
}
]
}
Direct link to API explorer for interactive testing of my example (change content, filters, etc.)

Using GraphQL structures to build complex database queries

I would like to specify inner constraints in a GraphQL query that would limit the results of the outermost query as part of a query / predicate builder I'm working on. I'm not sure if this is considered to be within GraphQL's capabilities but it makes sense to me as something that people would want to do.
For example, I might want to show a list of blog posts that were recently commented on like this:
{
posts{
title
date
comments(since: $earliestDate){
body
date
author {
name
}
}
}
}
The normal behaviour of this would be to bring back all blog posts and only comments that met the criteria.
{
"posts": [
{
"title": "Post 1",
"date": "2017-07-31"
"comments": [
]
},
{
"title": "Post 2",
"date": "2017-06-10",
"comments": [
{
"body": "Comment text",
"date": "2017-08-09",
"author": {
"name": "Michael"
}
}
]
}
]
}
But I want my query to prevent the retrieval of "Post 1" because it has no comments in the last month, but I'm not sure that's something GraphQL will make easy to do.
Is there functionality within GraphQL to support returning this result?
{
"posts": [
{
"title": "Post 2",
"date": "2017-06-10",
"comments": [
{
"body": "Comment text",
"date": "2017-08-09",
"author": {
"name": "Michael"
}
}
]
}
]
}
TL;DR - In general, you want the behavior of a field to only be defined by:
The arguments passed to that field
The identity of the object that is being queried
The global context of the query (eg, the identity of the user executing the query)
I think that the behavior you're after is not really ideal for GraphQL. At least as I've seen, GraphQL tends to treat fields as relatively independent from each other (even though they're nested).
For example, you would not expect arguments passed to a child field to change the behavior of its parent. And similarly, I think you would not expect the presence or absence of a child field to change the behavior of its parent.
The best way to accomplish the behavior you're after would be to add an argument to the posts field, indicating that only posts with comments should be returned:
{
posts(withCommentsOnly: true) {
title
date
comments(since: $earliestDate) {
body
date
author {
name
}
}
}
}
To understand the reasons why, just think about these kinds of scenarios:
{
authors {
id
posts {
comments { body }
}
}
}
If this query has the behavior you describe, for each author, you would only get the posts which also have comments. But then imagine that you re-query one of those authors later:
{
node(id: "author_id") {
... on Author {
posts { title }
}
}
}
Now would you get a different set of posts? That kind of behavior would make it very difficult for people to query your GraphQL schema using standard GraphQL clients, because client-side caching mechanisms would probably not be able to correctly update their caches.

How to query two different fields with different query terms in same request. ElasticSearch 5.x

new to ElasticSearch - start loving it. I am working on a Rails application (using elasticsearch-rails / elasticsearch-model).
I have two fields - both strings consisting of Tags.
about_me & about_you
Now I was to query the about_you of another user with the current users about_me.
At the same time, I wish to query the about_me of the other users with the about_you of the current user.
Does this make sense? Like two fields, two queries and each query is aimed at a particular field.
I just need a hint how this can be achieved in ES. For the sake of completeness, here is the part method I created in my rails model - it is incomplete:
def home_search(query_you, query_me)
search_definition =
{
query: {
multi_match: {
query: query_me,
fields: ['about_you']
}
..... SOMETHINGs MISSING HERE ..... ?
},
suggest: {
text: query,
about_me: {
term: {
size: 1,
field: :about_me
}
},
about_you: {
term: {
size: 1,
field: :about_you
}
}
}
}
self.class.__elasticsearch__.search(search_definition)
end
Any help, link or donations are welcome. Thank you!
I'm not sure I've understood your question but I can suggest two options:
First Use a bool query of type should and minimum_should_match=1. In this case you can write two queries for you'r searches. and If you want to distinguish between results you can pass a _name parameter in each query. something like this:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"multi_match": {
"query": "query_me",
"fields": [
"about_you"
],
"_name": "about_you"
}
},
{
"multi_match": {
"query": "query_you",
"fields": [
"about_me"
],
"_name": "about_you"
}
}
]
}
}
}
By providing _name you can see which queries are hitted in your search result.
The second approach could be a _msearch query which in which you can pass multiple queries to the endpoint and get the results back.
Here are some useful links:
Bool Query
Named Queries

Resources