I am using GatsBy GraphiQL to write a query to return a single element, but the query returns all of the elements.
Here is my testing data:
{
"data": {
"mKT": {
"data": [
{
"name": "Apple",
"description": "apple's desc"
},
{
"name": "Orange",
"description": "orange's desc"
},
{
"name": "Banana",
"description": "banana's desc"
}
]
}
},
"extensions": {}
}
And, here is the GraphQL query:
query MyQuery {
mKT(data: {elemMatch: {name: {eq: "Apple"}}}) {
data {
name
description
}
}
}
I expect to get:
{
"name": "Apple",
"description": "apple's desc"
}
However, running the query returns all of the data.
Any idea how to fix this issue?
Simply use:
query MyQuery {
mKT(data: {filter: {name: {eq: "Apple"}}}) {
data {
name
description
}
}
}
The filter keyword should do the trick.
Check the GraphQL Reference (in Gatsby docs) for further details.
I believe this is the same case as:
GraphQL query to access first item in an array?
You cannot use filters, limits, etc to an structure if it have not been defined on the source.
If you have access to the source, add something like limit to the field data.
If not, you will get the full array, and you have to process it on your side.
Related
Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.
I have been working with apollo-nuxt to query my graphql, but run into some weird issues while doing so. First of all this is my query:
{
getOrder(quantity: 2) {
id
createdAt
content {
amount
productId
}
}
}
which returns the following at /graphqli:
{
"data": {
"getOrder": [
{
"id": null,
"createdAt": 1592224281,
"content": [
{
"amount": 5,
"productId": "Salad"
},
{
"amount": 2,
"productId": "Water"
}
]
},
{
"id": null,
"createdAt": 1592224264,
"content": [
{
"amount": 5,
"productId": "Whine"
},
{
"amount": 3,
"productId": "Water"
}
]
}
]
}
}
this is as expected. Now using the same query in apollo (nuxt code):
<script>
import gql from 'graphql-tag'
export default {
apollo: {
orders: gql` query order {
orders: getOrder(quantity: 2) {
id
createdAt
content {
amount
productId
}
}
}`
},
...
will produce this result:
I cannot explain why the same query produces two different results. One with the two different objects in the array and the apollo one with two times the same object in an array. When adding more (by increasing the quantity) I just get more of the same object in the array. What am I missing here?
Your id for both orders is the same value (null) so Apollo ends up using the cache key for both orders (Order:null) and the orders field ends up referring to the same key twice. You need to fix your backend so that id is resolved correctly to a unique value for each Order.
Knowing the schema (fetched via getIntrospectionQuery), how could I get the type of a particular field?
For example, say I run this query:
query {
User {
name
lastUpdated
friends {
name
}
}
}
and get this result:
{
"data": {
"User": [
{
"name": "alice",
"lastUpdated": "2018-02-03T17:22:49+00:00",
"friends": []
},
{
"name": "bob",
"lastUpdated": "2017-09-01T17:08:49+00:00",
"friends": [
{
"name": "eve"
}
]
}
]
}
}
I'd like to know the types of the fields and construct something like this:
{
"name": "String",
"lastUpdated": "timestamptz",
"friends": "[Friend]"
}
How could I do that without extra requests to the server?
After retrieving the schema, you can build it into a JSON object (if your graphql framework does not do it already for you).
Using a JSON parser, you can retrieve the the types of each field.
I will not enter into the detail, as it would depend on the technology your are using.
For example my graphql's query is:
products {
name,
price,
brand {
name
}
}
Which produces this response:
{
"data": {
"products": [
{
"name": "Black Dress",
"price": 520,
"brand": {
"name": "Coach"
}
}
]
}
}
Can this
"brand": {
"name": "Coach"
}
be simplified to this somehow?
"brandName": "Coach"
Currently, without any additional dependencies and if you don't want to update schema, you cannot control the shape of the object returned by GraphQL if you like to control the shape that can be possible by graphql-leveler.
The graphql-leveler will provide a method call _get through which you can manage the shape of your object you can do simply:
{
brandName: _get(products: 'brand.name')
}
Note: to use graphql-leveler, simply add graphql-leveler to your dependencies, require LevelerObjectType, and use it instead of GraphQLObjectType. Done!
Im trying to get just entrys which have in a specific value in a array (myArray: [String]).
Displaying of this Array is not Problem:
query ($lang: [String!], $filter: String!) {
nodes(lang: $lang, filter: {schema: {is: myObj}, fields: {myObj: {name: {regex: $filter}}}}) {
elements {
uuid
language
availableLanguages
fields {
... on module {
name
myArray
}
}
node {
language
fields {
... on module {
name
myArray
}
}
}
}
}
}
Result:
{
"data": {
"nodes": {
"elements": [
{
"uuid": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"language": "de",
"availableLanguages": [
"de"
],
"fields": {
"name": "ASDF",
"myArray": [
"CAT_1",
"CAT_2"
]
},
"node": null
}
]
}
}
}
How can I filter on myArray? That I just et elements with the value uf $filter in the array myArray?
In the GraphiQL I can't find the myArray in the docs under the filter - fields.
GraphQL-Filtering for list types is not supported yet. In the GraphiQL docs you will only find supported fields for now.
See supported field types here: https://getmesh.io/docs/beta/graphql.html#_filtering_limitations
Here is the open issue on Github regarding this feature: https://github.com/gentics/mesh/issues/27