Nesting queries in GraphQL - graphql

I'm trying to figure out if there is an easy way to nest a query in Graphql. I have two tables, one with the beach records and one with the definitions of the conditions.
What i'm trying to return is the conditionName and conditionDescription if the surfCondition matches (surfCondition = 2}
query MyQuery {
lifeguard {
beachID
id
surfCondition (match the second query)
conditionName (display)
conditionDescription (display)
updated_at
created_at
}
lifeguard_conditions {
surfCondition
conditionName
conditionDescription
}
}

Normally this would be handled on the GraphQL server to accept a filter.
So, for example, you can pass a variable to surfCondition such as
surfCondition(filter: 2)
…which would then return automatically the filtered data.

Related

Filtering a list of values by a field value in GraphQL

So I'm doing some tests with GraphQL, and I'm failing in doing something that I believe is fairly simple.
When going to the GraphQL demo site (https://graphql.org/swapi-graphql) I'm presented with a default query which goes like this:
{
allFilms {
films {
title,
director,
releaseDate
}
}
}
This works as expected and returns a list of films.
Now - I would like to modify this query to return only the films where the director is George Lucas, and for the life of me - I can't figure out how to do that.
I've tried using the where and filter expressions, and also change the second line to films: (director: "George Lucas") but keep getting error messages.
What's the correct syntax for doing that?
Thanks!
If you check the docs of the provided GraphQL schema, you'll see that this is not possible. Following is the definition of the allFilms field:
allFilms(
after: String
first: Int
before: String
last: Int
): FilmsConnection
As per the doc, it has 4 input arguments, which are after, first, before, and last. There is no way to filter this out using the director's name.
GraphQL is not SQL. You cannot use expressions like WHERE or FILTER in GraphQL. The schema is already defined and the filters are pre-defined too. If the schema does not allow you to filter values using a certain field, you just can't do it.
You can to see the graphql schema here https://github.com/graphql/swapi-graphql/blob/master/schema.graphql
The allFilms query does not contain a filter for the field director. Also i can't find other query with this filter.
Most likely you need to write a filter on the result of the query.

Group queries in GraphQL (not "group by")

in my app there are many entities which get exposed by GraphQL. All that entities get Resolvers and those have many methods (I think they are called "fields" in GraphQl). Since there is only one Query type allowed, I get an "endless" list of fields which belong to many different contexts, i.E.:
query {
newsRss (...)
newsCurrent (...)
userById(...)
weatherCurrent (...)
weatherForecast(...)
# ... many more
}
As you can see, there are still 3 different contexts here: news, users and weather. Now I can go on and prefix all fields ([contextName]FieldName), as I did in the example, but the list gets longer and longer.
Is there a way to "group" some of them together, if they relate to the same context? Like so, in case of the weather context:
query {
weather {
current(...)
forecast(...)
}
}
Thanks in advance!
If you want to group them together , you need to have a type which contain all fields under the same context . Take weather as an example , you need to have a type which contain currentWeather and forecastWeather field. Does this concept make sense to your application such that you can name it easily and users will not feel strange about it ? If yes , you can change the schema to achieve your purpose.
On the other hand, if all fields of the same context actually return the same type but they just filtering different things, you can consider to define arguments in the root query field to specify the condition that you want to filter , something like :
query {
weather(type:CURRENT){}
}
and
query {
weather(type:FORECAST){}
}
to query the current weather and forecast weather respectively.
So it is a question about how you design the schema.

Hasura GraphQL query order by nested array relationships (with only one element)?

From the Hasura documentation is not possible to order by nested array relationships, the thing is I'm using that relation to get only one element from the array (e.g. the latest entry in that table). There is any way to transform that array (with one element) into an object to be able to perform order by in the root query?. Example:
query GetMachinesQuery {
machines {
machine_id
machine_detail
last_upgrade: upgrades(order_by: { created_at: desc }, limit: 1) {
upgrade_state {
updated_at
status
}
}
}
}
Do I have any way to sort the root query by any of the fields (e.g. status) present in the last_upgrade? The possible workaround is create a view (doing the joins to get latest upgrade info for each machine) and then I can use an object relationship, any other alternatives with hasura?
Thank you !

Elasticsearch query not returning expected results for multiple should filters

I am performing an Elasticsearch query using the high-level-rest-api for Java and expect to see records that are either active or do not have a reference id. I'm querying by name for the records and if I hit the index directly with /_search?q=, I see the results I want.
Is my logic correct (pseudo-code):
postFilters.MUST {
Should {
MustNotExist {referenceId}
Must {status = Active}
}
Should {
MustNotExist {referenceId}
Must {type = Person}
}
}
What I get are records that are active with a reference id. But, I want to include records that also do not have a referenceId, hence why I have MustNotExist {referenceId}.
For simplicity, the second Should clause can be dropped (for testing) as the first one is not working as expected by itself.
In my case, I had to use a match query instead of a term query because the value I was querying for was not a primitive or a String. For example, the part where Must, type = Person, Person was an enum, and so looking for "Person" was not quite right, whereas match allowed it to "match".

GraphQL Query returns different result depends on argument

I would like to know if a query returns different result depends on an argument, let say
{
appDates(opposite:true){
createdBy
from
to
}
appDates(opposite:false){
date
}
}
In this case, I can either split the query into 2 different queries/schemas, or reuse the same schema since the SQL structure is similar. Which is a better approach for graphql?

Resources