graphQL : Use variable in string - graphql

I'm using strapi V4 plugin system and I want to query all comments relative to a specific post.
The default graphql request look like that :
query () {
findAllFlat (relation: "api::post.post:77") {
data {
...
}
}
}
But I need to add a variable instead of 77, ideally I would like to do something like this :
query ($postId:ID!) {
findAllFlat (relation: "api::post.post:${postId}") {
data {
...
}
}
}
But of course, string template doesn't work with graphQL. Is there another way ?

Related

WPGraphQL "where" is not case-sensitive

Hi there I am using the WPGraphQL Plugin, however, I am having trouble with the query clause where:
query getCaseStudy {
caseStudies(where: {name : "something-like-this" }) {
nodes {
databaseId
}
}
}
the above code returns a valid and correct response, however, when I passed on the parameter name, like this:
query getCaseStudy {
caseStudies(where: {name : "something-like-this-" }) { //additional special characters
nodes {
databaseId
}
}
}
The results were still the same and the response was valid. Is this how the WPGraphQL works? I want to make it case-sensitive; is there a way to do it?

Graphql define variable as field

I'm querying a scheme that looks like this:
{
collections {
listings {
tokenid
price
}
tokens {
id
collection
}
}
}
How can I loop through the tokens field and get listings with the tokenid?
I imagine it looks something like this:
{
collections {
tokens {
$id: id
collection
listings(where: {tokenid: $id}) {
tokenid
price
}
}
}
}
If the API supports introspection, you should be able to view the schema documentation and see how it works. You can try to use a GraphQL-capable client, like Insomnia or GraphiQL.
While the exact implementation might require a different structure, my guess will be something like this:
query($id: ID!) {
collections {
listings(tokenid: $id) {
tokenid
price
}
}
}
and you'll need to specify id as a parameter inside the client.

Strapi GraphQL search by multiple attributes

I've got a very simple Nuxt app with Strapi GraphQL backend that I'm trying to use and learn more about GraphQL in the process.
One of my last features is to implement a search feature where a user enters a search query, and Strapi/GraphQL performs that search based on attributes such as image name and tag names that are associated with that image. I've been reading the Strapi documentation and there's a segment about performing a search.
So in my schema.graphql, I've added this line:
type Query {
...other generated queries
searchImages(searchQuery: String): [Image
}
Then in the /api/image/config/schema.graphql.js file, I've added this:
module.exports = {
query: `
searchImages(searchQuery: String): [Image]
`,
resolver: {
Query: {
searchImages: {
resolverOf: 'Image.find',
async resolver(_, { searchQuery }) {
if (searchQuery) {
const params = {
name_contains: searchQuery,
// tags_contains: searchQuery,
// location_contains: searchQuery,
}
const searchResults = await strapi.services.image.search(params);
console.log('searchResults: ', searchResults);
return searchResults;
}
}
}
},
},
};
At this point I'm just trying to return results in the GraphQL playground, however when I run something simple in the Playground like:
query($searchQuery: String!) {
searchImages(searchQuery:$searchQuery) {
id
name
}
}
I get the error: "TypeError: Cannot read property 'split' of undefined".
Any ideas what might be going on here?
UPDATE:
For now, I'm using deep filtering instead of the search like so:
query($searchQuery: String) {
images(
where: {
tags: { title_contains: $searchQuery }
name_contains: $searchQuery
}
) {
id
name
slug
src {
url
formats
}
}
}
This is not ideal because it's not an OR/WHERE operator, meaning it's not searching by tag title or image name. It seems to only hit the first where. Ideally I would like to use Strapi's search service.
I actually ran into this problem not to recently and took a different solution.
the where condition can be combined with using either _and or _or. as seen below.
_or
articles(where: {
_or: [
{ content_contains: $dataContains },
{ description_contains: $dataContains }
]})
_and
(where: {
_and: [
{slug_contains: $categoriesContains}
]})
Additionally, these operators can be combined given that where in this instance is an object.
For your solution I would presume you want an or condition in your where filter predicate like below
images(where: {
_or: [
{ title_contains: $searchQuery },
{ name_contains: $searchQuery }
]})
Lastly, you can perform a query that filters by a predicate by creating an event schema and adding the #search directive as seen here

How do I query the GitHub v4 API for directory contents at a certain tag?

How do I query the GitHub API v4 for the contents of a certain directory of a repository at a certain tag?
This is the best I've come up with so far:
query {
repository(owner:"example", name:"example") {
refs(refPrefix: "tags") {
}
}
}
From this post, you can get the GitObject with object to filter on branch:/path/folder and print the Tree. The following will get the tree from gson folder from tag gson-2.4 and print name, type and mode :
query {
repository(owner:"google", name:"gson") {
object(expression: "gson-2.4:gson") {
... on Tree{
entries{
name
type
mode
}
}
}
}
}

GraphQL arguments to connection sub results

I'm need help passing arguments to collections/connections/arrays in GraphQL syntax.
I'm just learning it, playing with the SWAPI at http://graphql.org/swapi-graphql/
I can pass an id argument to a single type, like this:
query getANewHope {
film(id: "ZmlsbXM6MQ==") {
id
title
}
}
But I don't know how to query the results of a collection/connection
query starships {
allStarships(id: "c3RhcnNoaXBzOjI=") { # this doesn't work
edges {
node(id: "c3RhcnNoaXBzOjI=") { # ...nor this.
id
}
}
}
}
I want to query collections because, I'd like to connect the two ideas like "All Starfighter type ships in A New Hope"?
query filmStarships {
film(id: "ZmlsbXM6MQ==") {
title
starshipConnection { #How to limit this? I can't use (starshipClass: "Starfighter") here...
edges {
node { # ...nor here..
starshipClass # ...nor here.
}
}
}
}
}
query starships2 {
starship (id: "c3RhcnNoaXBzOjI=") { # This doesn't work either
id # even without an arugment abovce, it says "Unknown argument \"id\" on field \"node\" of type \"StarshipsEdge\"."
}
}
Arguments like you're asking for have to be implemented in the schema. The SWAPI does does not expose an argument to filter by starshipClass. Click Docs on the top right of the GraphiQL window to explore the provided schema.
If you are implementing your own schema it would be very easy to add a filter on starshipClass in the resolvers.

Resources