nuxt-apollo issues while pulling array of objects - graphql

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.

Related

Why does FaunaDB output differ from Graphqli?

I have created a simple user.gql file
type Query {
users: [user]
userById(id:ID!):user
}
type user {
id: ID!
chat_data: String
}
My data is
[
{
"id": "0815960b-9725-48d5-b326-7718c4749cf5",
"chat_data": ""
}
]
When I run this on my local server and use the query
{users{id}}
I see the expected output
{
"data": {
"users": [
{
"id": "0815960b-9725-48d5-b326-7718c4749cf5"
}
]
}
}
I have created a user collection on FaunaDB with the data
{
"ref": Ref(Collection("user"), "324407037973758152"),
"ts": 1645691670220000,
"data": {
"id": "0815960b-9725-48d5-b326-7718c4749cf5",
"chat_data": ""
}
}
and uploaded my user.gql, but when I run the GraphQl query
{users{id}}
I get the error
{
"data": null,
"errors": [
{
"message": "Cannot query field 'id' on type 'userPage'. (line 3, column 5):\n id\n ^",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}
What am I doing wrong?
This is very unintuitive, but Fauna seems to be returning a paginated result. Read more about it here.
The best thing would be to GraphiQL to have a look at the schema of the Fauna GraphQL endpoint. Autocomplete should also work when you look for fields to query. The error basically says that you can't query the id directly. Try this:
{ users { data { id } } }

What is proper way to use graphql query variables to search for values matching two different searches?

I have a "users" table that is connected to "interestTags" table. I would like to be able to search users interestTags and return all users that match one or more tags, In this example I would like to be able to return all users that has interestTags of either "dog" or "apple.
The code below is only showing matches for "apple" and leaving out the "dog" interestTag users. I would like to get both "dog" users and "apple" users returned instead of one or the other. How would I go about doing this? Here is my code:
users(offset: $offset, limit: 30, order_by: {lastRequest: asc}, where: {dob: {_gte: $fromDate, _lte: $toDate}, interestTagsFromSenderId: {_or: [{tag: $tagList}]}}) {
id
displayName
profilePhotoUrl
dob
bio
location
interestTags: interestTagsFromSenderId {
tag
}
created_at
}
}
graphql query variables:
{
"offset": 0,
"fromDate": "1999-07-01",
"toDate": "2024-01-01",
"tagList":
{
"_eq": "dog", "_eq": "apple"
}
}
This is what graphql is returning:
{
"data": {
"users": [
{
"id": 31,
"displayName": "n00b account",
"profilePhotoUrl": "default.jpg",
"dob": "2021-07-15",
"bio": null,
"location": null,
"interestTags": [
{
"tag": "apple"
}
],
"created_at": "2021-07-15T06:57:23.068243+00:00"
}
]
}
}
to fix the issue:
I added $tagList: [interestTags_bool_exp!] to the query function
I changed the query to interestTagsFromSenderId: {_or: $tagList}}
And changed the variable query to { "tagList": [{"tag": {"_eq": "dog"}}, {"tag": {"_eq": "apple"}}]}

GraphQL filtering an array

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.

Strapi GraphQL query: "start" argument wouldn't work

I am running into a very strange problem with my queries in Strapi (version 3.0.0-alpha.26.2). I have a users collection with 3 documents that I'm trying to fetch via GraphQL. To fetch all users the query is:
users {
firstName
}
This returns the following:
{
"data": {
"users": [
{
"firstName": "Arnold"
},
{
"firstName": "Bill"
},
{
"firstName": "Vin"
}
]
}
}
3 names. Now, say, I wished to retrieve only the first 2 users. For such pagination use-cases, there's two arguments one could pass in a Strapi query: start (defines the index to start at) and limit (defines the number of elements to return). So now the query would be:
users(start: 0, limit: 2) {
firstName
}
This returns the first two names as expected:
{
"data": {
"users": [
{
"firstName": "Arnold"
},
{
"firstName": "Bill"
}
]
}
}
But what if I want the last 2 users here, i.e. Bill and Vin? Should be as straightforward as:
users(start: 1, limit: 2){
firstName
}
But this still returns Arnold and Bill, while you'd expect the following:
{
"data": {
"users": [
{
"firstName": "Bill"
},
{
"firstName": "Vin"
}
]
}
}
No matter what value I use for start, it always starts at the 0th item. You could do start: 200 (when there are only 3 items in the users collection) and it'd still return the exact same result! What sorcery is this??
The issue can be reproduced at https://dev.schandillia.com/graphql.

AWS AppSync GraphQL - using union as mutation return causes error

I'm creating a GraphQL schema using AWS AppSync and I want to use Union as mutation return. I'd like to write mutation this way:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
... on SteppedTariff {
endDate
}
... on SeasonalTimeTariff {
endDate
peakConsumption
}
}
}
}
But I'm getting this error:
Request failed with status code 400
The field values can be of the type SteppedTariff or SeasonalTimeTariff, depends the inputs of addUpdateTariff mutation. As far as I've searched, Union is only used in queries and I didn't find some documentation telling it cannot be used in a different way.
Am I missing something or I really cannot use Union this way?
Schema:
type Tariff {
id: ID!
type: TariffType!
values: [TariffValue!]
}
type SteppedTariff {
endDate: AWSDate
}
type SeasonalTimeTariff {
endDate: AWSDate
peakConsumption: Float
}
union TariffValue = SeasonalTimeTariff | SteppedTariff
Testing different schemas for Tariff values:
values: [String]
Query:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values
}
}
Response:
{
"data": {
"addUpdateTariff": {
"id": "843eadcf-48bd-4d58-93ec-8bbe96db3635",
"type": "SeasonalTime",
"values": [
"{endDate=2019-02-02}"
]
}
}
}
values: [SeasonalTimeTariff]
Query:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
endDate
}
}
}
Response:
{
"data": {
"addUpdateTariff": {
"id": "7f77c5d9-2b06-4bb8-a678-10996addc4e1",
"type": "SeasonalTime",
"values": [
{
"endDate": "2019-02-02"
}
]
}
}
}
You can definitely use Unions on the return types for mutations. Make sure you are including the field __typename on your values. Your resolver template should return something like this:
{
"id": 1,
"type": "?",
"values": [
{
"__typename": "StepTariff",
"endDate": "2019-01-01"
},
{
"__typename": "SeasonalTimeTariff",
"endDate": "2019-01-01",
"peakConsumption": 1.0
}
]
}

Resources