I'm investigating a possibility to use GraphQL between React.js client application and server application which is built on top of relational SQL database. A query should be created on client side including complex SQL-style statements like:
WHERE Customer.Age BETWEEN 22 AND 25
AND Order.Status = 'Active'
OR Product.Name LIKE '%foo%'
It means the client should usually receive only a small subset of records (for example 10 instead of 10M).
This looking-good Phil Sturgeon article declares strange things:
I was hoping GraphQL could help clients define their own scopes, filtering these includes to be the appropriate data themselves, which would help identify the scoped includes the API should add as convenience methods.
It seems like GraphQL doesn't help API developers in this instance, but there does seem to be talk of adding @filter to do this in the future.
In the future? No filtering in GraphQL right now? I continued research and have found this SO question and this amazing interactive Graphcool documentation. Both examples use a feature called filter
with a set of postfixes like _gte
:
query combineMovies {
allMovies(filter: {
OR: [{
AND: [{
releaseDate_gte: "2009"
}, {
title_starts_with: "The Dark Knight"
}]
}, {
title: "Inception"
}]
}) {
title
releaseDate
}
}
However, there is no specification for the filter
keyword at http://graphql.org.
I even checked Relay docs and have found no good examples of complicated filtering (maybe because of I have no React experience).
Please clarify GraphQL's abilities to build complex SQL WHERE-like queries. Is it a part of standard or just a weakly supported side feature?
I continued research and have found this SO question and this amazing interactive Graphcool documentation.
Thanks for the kind words! I'm the author of this article, glad it was helpful! Let me answer your question from the perspective of the Graphcool API.
GraphQL queries are built using fields. Here we use the id
field defined on the allMovies
field:
query allMovies {
allMovies {
id
}
}
I recommend reading this article for more information on the terminology surrounding GraphQL queries.
GraphQL defines so called query arguments that can be attached to fields. These arguments encode additional information for the GraphQL Server and influence how the field will be resolved. A common example is the first
query argument:
query firstMovie {
allMovies(first: 1) {
id
}
}
Lets investigate further!
query darkKnightMovies {
allMovies(filter: {
title_contains: "Dark Knight"
}) {
id
}
}
In the above query, filter
is an argument to the allMovies
field. The syntactical aspect of this is defined in the GraphQL specification as seen above. However, GraphQL doesn't specify how this argument should alter the resolving of the allMovies
field. This is only determined by the changed behaviour of the resolver for the allMovies
field in the GraphQL backend.
GraphQL provides all the necessary concepts (queries, fields, arguments) to support server-side filtering. However, using these concepts to encode specific behaviour like filtering is up to the developer that creates a GraphQL API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With