How can I modify a graphQL query based off of the variable passed to it? - graphql

I have built this query to request data from the Pokeapi graphQL server here
https://beta.pokeapi.co/graphql/console/
query getPokemon (
$typesList: [Int!],
) {
pokemon_v2_pokemon(
where: {
pokemon_v2_pokemontypes: { type_id: { _in: $typesList } }
}
) {
id
name
pokemon_v2_pokemontypes {
type_id
pokemon_v2_type {
name
}
}
}
}
How can I modify the query to return all results when the $typesList list is empty? I would like the query to return a filtered results list when $typesList has a list of ids, but it should return all values when the $typesList list is empty.
Is this something that is possible in graphQL?
Thank you

If you controlled the server you could write the resolver to function in the manner you're looking for but since you don't your only option is to wrap your use of the query with something that sets the default typesList variable to all possible types.

Related

GraphQL using a dynamic ID from set of returned results to render correct set of data

I am pretty new to graphql, so my question might sound a bit strange. it may not be correct way of doing what I need, but I can't think of any other way.
Basically, I would like to know how I can use an ID i am being returned from my graphql query and use that ID in the second query to render the data I need.
currently my full query looks like:
export const query = graphql`
query myQuery {
allSanityFrontpage {
nodes {
content {
... on SanityHero {
_key
_type
heroIntro
heroHeader
heroButtonText
heroProjectReference {
id
title
slug {
current
}
}
}
}
}
}
sanityProject(id: { eq: "-8571b76b-8cfd-55cf-b848-151586f5c57a" }) {
id
title
coverImage {
asset {
title
fluid(maxWidth: 800) {
...GatsbySanityImageFluid
}
}
}
}
}
`;
I am getting an ID returned from this part of the query
heroProjectReference {
id
And currently I am using that ID statically to render the content I need by doing this:
sanityProject(id: { eq: "-8571b76b-8cfd-55cf-b848-151586f5c57a" }) {
id
title
How do I replace that static ID with something dynamic, so I can update my CMS and see whatever project I have updated it to be?
Client-side you must call the chained queries serially, populating the ID in the dependent query with the output from the first query.
Server-side, of course, you can combine the two in the resolver.

Graphql Filter by query

So I'm trying to learn graphql I've been playing around with the ENS subgraph on the graph
I've figured out how to do simple filtering but when I try to write more complex filters they do not compile.
I'm trying to get the top 5 transactions for the each of the top 5 domains. (e.g for each domain I want the top 5 transactions)
{
#Sample Query to get the first 5 domains (not needed for question but used to validate results)
domains(first: 5) {
id
name
labelName
labelhash
}
#attempt to filter the transfer.domain.id by TOP 5 domains.id
transfers(where: { domain { id: domains(first: 5) { id } } }) {
id
domain {
id
}
blockNumber
transactionID
}
}
EDIT I'm going to attempt to simplify my request since I'm not sure nesting queries is possible. How can I filter an inner query by Id:
transfers(where: {domain.id: "0x9c0fc2519ae862cee27778e5c34714d6c7e3ca21ad572df47ad9f6fe530909bd"}) {
id
domain {
id
}
blockNumber
transactionID
}
NOTE: Domain.Id = does not compile how would I write a filtered query like that?
However, My filter doesn't compile syntactically. How can I write a query which filters by a child property?
You can query like this
query {
getPost(id: "0x1") {
title
text
datePublished
}
}
Got this from https://dgraph.io/docs/graphql/queries/search-filtering/

GraphQL-java add variables to query

I am new to GraphQL. I know this is a basic question but hope someone could help me to add variables to my query as I tried many times and failed :(
In my query, below schema is used:
type Query {
ContinentInfo(id: ID): Continent
}
type Continent {
id : ID
name: String
countries: [Country]
}
type Country {
id : ID
name : String
population: Float
}
Below query is executed successfully:
{
ContinentInfo(id: "continent01") {
name
countries {
name
population
}
}
}
Then I want to add more conditions in the query, for example add a variable "populationMoreThan" to filter the result. so the query may look like:
{
ContinentInfo(id: "continent01") {
name
countries(populationMoreThan: $populationMoreThan) {
name
population
}
}
}
but it always failed when I tried to add this variable in the schema and in the query.
Could anyone provide me an example of adding variable in my case?
Also, it looks I need to pass the parameter value into the query? Now I'm using graphql.GraphQL.execute(queryString) to pass the query string. How to pass the variable value here?
Finally found a way to filter the result.
Update the schema with:
type Continent {
id : ID
name: String
countries(populationMoreThan: Float = 0): [Country]
}
And query with:
{
ContinentInfo(id: "continent01") {
name
countries(populationMoreThan: 1.0) {
name
population
}
}
}

How can I do a WpGraphQL query with a where clause?

This works fine
query QryTopics {
topics {
nodes {
name
topicId
count
}
}
}
But I want a filtered result. I'm new to graphql but I see a param on this collection called 'where', after 'first', 'last', 'after' etc... How can I use that? Its type is 'RootTopicsTermArgs' which is likely something autogenerated from my schema. It has fields, one of which is 'childless' of Boolean. What I'm trying to do, is return only topics (a custom taxonomy in Wordpress) which have posts tagged with them. Basically it prevents me from doing this on the client.
data.data.topics.nodes.filter(n => n.count !== null)
Can anyone direct me to a good example of using where args with a collection? I have tried every permutation of syntax I could think of. Inlcuding
topics(where:childless:true)
topics(where: childless: 'true')
topics(where: new RootTopicsTermArgs())
etc...
Obviously those are all wrong.
If a custom taxonomy, such as Topics, is registered to "show_in_graphql" and is part of your Schema you can query using arguments like so:
query Topics {
topics(where: {childless: true}) {
edges {
node {
id
name
}
}
}
}
Additionally, you could use a static query combined with variables, like so:
query Topics($where:RootTopicsTermArgs!) {
topics(where:$where) {
edges {
node {
id
name
}
}
}
}
$variables = {
"where": {
"childless": true
}
};
One thing I would recommend is using a GraphiQL IDE, such as https://github.com/skevy/graphiql-app, which will help with validating your queries by providing hints as you type, and visual indicators of invalid queries.
You can see an example of using arguments to query terms here: https://playground.wpgraphql.com/#/connections-and-arguments

The significance of the string immediately after query type (query / mutation) GraphQL

I was wondering what the significance of the string that follows the query type, in this case "ProvisionQueues", it seems removing this from the string doesn't affect anything - is it just for logging or something. meta data?
mutation ProvisionQueues {
createQueue(name: "new-queue") {
url
}
}
That string is the operation name. If you don't specify a name, the operation is known as an anonymous operation. When practical I like to always specify an operation name though, because it makes it easier for doing things like reading stack traces.
it seems removing this from the string doesn't affect anything
You can only use an anonymous operation when you are performing a single operation. For instance, the following results in an error:
query {
user(id: 1) {
name
}
}
query {
user(id: 2) {
name
}
}
The error:
"message": "This anonymous operation must be the only defined operation."
If you want to learn more, you can check out the GraphQL spec:
If a document contains only one operation, that operation may be unnamed or represented in the shorthand form, which omits both the query keyword and operation name. Otherwise, if a GraphQL query document contains multiple operations, each operation must be named.
Adding to #Eric's answer with another example.
query allNotifications {
notifications {
success
errors
notifications {
id
title
description
attachment
createdAt
}
}
} ​
​
query {
users {
errors
success
users {
id
fullName
}
}
}
Notice above that the users query has no operation name. This can be resolved as below.
​
query allUsers {
users {
errors
success
users {
id
fullName
mohalla
}
}
}

Resources