I'm using Strapi GraphQL as a backend for my Nuxt app, and am trying to perform a query where I find images using an array of IDs. Right now, supplying an empty array returns all images when instead I need the query to return an empty array of Images.
images.gql:
query($ids:[ID]) {
images(where:{id:$ids}) {
id
name
slug
src {
url
formats
}
}
}
query variables:
{
"ids": [] <-------- returns all images; need to return empty array of Images
}
{
"ids": [6] <--------- returns image with ID of 6 correctly
}
Looking at the Strapi documentation I've also tried other filters such as [field]_in but that is still returning all Images instead of an empty array.
Does anyone have a suggestion? Thanks!
Related
I have been unable to use any of the filter parameters on graphl query in strapi using the url (http://localhost:1337/graphql). I am using strapi 3.6.8 . I have a collection (Animals) that has an array of Habitat. When I try to limit the number of Habitat returned for each record to 3, it doesn't work with the query below.
query{
Animals {
id
Habitat(limit: 3) {
Feature {
Weight
Height
Lifespan
}
}
}
}
I get the error message: "Unknown argument "limit" on field "Animal.Habitat"."
the limit, where or any other filter does not work. They work however when I use them on the Animals itself. Kindly help
If you are on graphql playground, try pressing CTRL + SPACE and you will see the available parameters that you can use inside your query. In your case, I assume you want to use the pagination feature provided by Strapi. The following query should work.
query {
Animals {
id
Habitat(pagination: { limit: 3 }) {
Feature {
Weight
Height
Lifespan
}
}
}
}
I am using the rebing graphql laravel library and it works very fine returning json data. Currently I am trying to build a paginated table via reactjs frontend so I need to get a paginated data just the way it works normally with blade. I have used the paginate method in place of the get method but it somehow still gives same data format. This is what I have in my resolve method currently:
$models = YearModel
::with($with)
->select($select)
->where($where)
->orderByDesc('year_id');
if (isset($args['limit'])) {
$models = $models->paginate($args['limit']);
} else {
$models = $models->get();
}
return $models;
paginate and get returns same data to the frontend whereas I was expecting paginate to give some data which would include stuffs like currentPage, nextPage, previousPage as the LengthAwarePaginator class would normally do. So I believe the graphql library is somehow formating the paginated data as a normal data before returning it to the frontend. I am not sure why this is like this as I am new to graphql. Any idea or solution will be appreciated please. Thanks
I was able to get a paginated data response by changing my query return type from Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('YearModel')))) to GraphQL::paginate('YearModel') and modified my query this way:
query {
yearModels (limit: 10) {
data {
id
year
}
total
per_page
current_page
from
to
last_page
has_more_pages
}
}`
data, total, per_page, current_page, from, to, last_page, has_more_pages are fields coming from Rebing\GraphQL\Support\PaginationType getPaginationFields method.
I am trying to query an array of ids with graphQl. The query works with a single id as a variable. However it doesn't work when I enter an array of ids.
Here is my gql query with variables:
query GetAuthorContent($id: [ID]!, $idType: AuthorIdType) {
expert(id: $id, idType: $idType) {
excerpt
featuredImage {
node {
description
author {
node {
description
}
}
}
}
slug
}
}
{"id": ["author-1", "author-2", "author-3"], "idType": "SLUG" }
You can look at the definition of the graphql endpoint using a client and see if the Arrays are supported with query.
If it's supported, check the mutation signature and pass accordingly. In this case I think the services does not support querying using an Array.
Hi everyone and thank you for your help.
You guys were right, my DB doesn't allow an array of authors if it is per author singular. However it works with authors plural. This is the way my db works.
Hope it can help someone in the same situation.
Trying to use slug as an uuid to fetch single pages from a strapi backend v4.
my query to get pages works well. It's just I can't filter using slug:
my pages query:
query {
pages {
data {
attributes {
Name
slug
}
}
}
}
how to get single page using slug uuid within this schema ? every trails to implement ($slug: String!) fails as where to locate slug.
You can use filters. Just pass slug as argument and use the 'eq' keyword for comparison which stands for equals.
query pages($slug:String) {
pages(filters:{slug:{eq:$slug}}) {
data {
attributes {
Name
slug
}
}
}
}
Try this
query pages($id: ID) {
page(id: $id) {
data {
attributes {
name
slug
}
}
}
}
base on this blog, from strapi, a resolver must be coded
I Found a Blog for strapi v4
Sorry if this question is not being worded correctly. I have a website that calls a graph ql server, and Im trying to retrieve all images for a particular user, where the graph ql server is paginating the result.
Here is my query:
query UserPhotos ($userId: ID) {
user(input: { id: $userId }) {
edges {
node {
id
imageUrl
}
}
}
}
}
Where each edge implements the connection edge specification. This query would return to me a connection list of 50 edges, but there are more images (for a particular user, there should be like 100) to be retrieved in the graphql server. Does anyone know how I can change my query to simply get all of the images and how many images there are from the graphql server?
This should do it for you:
query UserPhotos ($userId: ID) {
user(input: { id: $userId }) {
id
imageUrl
}
}
The Query you wrote is used for pagination and Ideally you should not want to create an edge with a node unless you want an opaque cursor for the purpose of pagination.
The Query I provided does not try to paginate and thus, would provide you with all your image Objects.
you can refer this page for better understanding:
https://graphql.org/learn/pagination/