AWS Appsync mutation not updating - database

I'm really new to aws and graphql. I'm trying to update the "score" of my database. I'm able to set a value for "score" when I create a new mutation. However, when I try to update a current mutation the value of "score" doesn't change.
mutation update {
updateScore(input: {id: "3c8571ad-817a-4c7f-a2a4-95b1e664f105", score: 0}) {
id
score
}
}
mutation create {
createScore(input: {score: 1}) {
id
score
}
}
When query mutation update I'm still getting a score of 1

I found the answer, unfortunately it's not documented yet on their website. Basically you need to pass the _version along with the update request. You can get the _version when you query the data. I posted the complete answer in the following link: AWS Graphql API mutation does not update DataStore
It's also answered by contributors on Github:
https://github.com/aws-amplify/amplify-js/issues/10664

Related

Merge query results of queryOneEntityById and queryAllEntitiesByIds in Apollo by __typename

There are two queries to get Posts. One is to get specific post by id (returns object)
query PostById ($id : ID!) {
post (id: $id) {
title
description
}
}
another one is to get all posts by ids (returns array of objects)
query PostsByIds ($ids : ID!) {
posts (ids: $ids) {
title
description
}
}
Entities in both of them have same __typename though from what I see Apollo caches them separately.
Is there a way to tell Apollo to treat everything with same __typename as a part of one data pool? So for example if we already have Post with id===1 received with PostsByIds([1]) on the call to PostById(1) apollo leverage cached data and not making API call?

How to Update this mobile no field to firestore database when all the data have route to this screen

How do i update this mobile no field to firestore database i am unable to find the doc id. i tried many times but it showing me error that doc id is missing which doc id should i have to put please make a help for me in this case. Thankyou.
const ItemDetails = ({ route }) => {
const[values, setValue] = useState('')
let data = route.params
console.log(data)
const updateValue = () => {
db.collection("FinalData")
.doc()
.update({
mobile_no: values
})
.then(function () {
alert("Mobile Number Updated Successfully")
})
}
Okay, you do not know which doc id to put.
In Firestore, you can only update an existing document.
Right now, what you want to do with this line db.collection("FinalData").doc().update(...) is to update a document, but you have not told firestore which document to remove the old mobile no from and put the new mobile no in.
Another way to understand this is we can assume that your firestore database is a book. What you want to do with this line db.collection("FinalData").doc().update(...) is to tell firestore to please change mobile no to 'bla bla bla' on page (you didn't give any page number). So you see, firestore can not change anything because it does not which page to change.
So the doc id being referred to is the id of the document you want to correct.
This mobile no, is probably one of your users mobile number, so get the document (which could be something like user-settings, user-details or so) id.
Then you put that document id as shown below:
db.collection("FinalData").doc('PUT-DOC-ID-HERE').update(...)

Good way to query server object being in local cache

So I have a database that store users. When someone log on my website, it stores in apollo cache a user as currentUser. And I only store his id.
So I made a query to get a user by passing his id :
query {
user(id: "id") {
id
username
avatar
}
}
But everytime I wanna get data for that user I need to make two query (the first one locally to get back his id from the cache and a second one to the server).
const GET_CURRENT_USER = gql`
query getCurrentUser {
currentUser #client
}
`;
const GET_USER_DATA = gql`
query getUser($id: String!) {
user(id: $id) {
id
username
avatar
}
}
`;
const currentUserData = useQuery(GET_CURRENT_USER);
const { currentUser } = currentUserData.data;
const { data, loading } = useQuery(GET_USER_DATA, {
variables: { id: currentUser.id },
fetchPolicy: "cache-and-network"
});
Is here a way that I can reduce that to only one query (the one to the server) ?
id value stored in the cache can be read using readQuery, you can store it in other global store/state, f.e. redux.
If you're using apollo cache as global store then using queries is a natural part of this process.
Using readQuery you can read the value without querying (but doing the same). One query 'saved' ;)
Deeper integration (additional query, local resolver) is not a good thing - creating unnecessary dependencies.
If you want to reuse this "unneccessary query" extract it to some module or create a custom hook (id read/used/saved once during initialization) - probably the best solution for this scenario.
Another solutions:
make login process providing user data - for some inspiration take a look at apollo-universal-starter-kit - but this is for initial data only (login/avatar changing during session??) - further user querying still needs an id parameter - it must be stored and read somewhere in the app.
make id optional parameter (for getUser query - if you can change backend) - if not provided then return data for current user (id read from session/token)

Apollo is mixing results from a GraphQL Query in React

I am making a call that returns questions and their answers. When I make this directly to the server, the response is as expected. Additionally, when react makes the call and I check developer tools, the response is as expected.
Once I get a response from Apollo, the data is mixed and results are bleeding into one another.
You will notice in these two images, the results are the same after a few results are expanded. I am not sure what could be causing this.
Why would freeform be combining?
My setup is like this:
I import the query and then run this on the client which is Apollo:
this.props.client.query({ query: getCoreObjectsQuery, variables: { companyId: 1}})
.then((result) => {
console.log(result, 'getCoreObjectsQuery')
From there, when I go to look at getCoreObjectsQuery, it looks like the above images!
Wow! Turns out to be an Apollo Client caching error.
Kamranicus.com has a really helpful article on this, but to anybody looking for a quick fix:
__typename #skip(if: true)
questions{
id
__typename #skip(if: true)
coreId
question
isCore
type
answer:answerForCoreId{
id
__typename #skip(if: true)
freeform
url
choice {
id
__typename #skip(if: true)
}
}
}
id and _id cache!

Customising the API mapping for ng-admin, need a function to change "id" to "_id"

Currently I am using ng-admin to build an admin panel for my webapp.
http://ng-admin-book.marmelab.com/doc/API-mapping.html
This link talks about API mapping and the JSON response expected when ng-admin uses a GET request to get data.
The JSON response format is as follows:
{
"id": 12,
"name": "War and Peace",
"author_id": 345,
"publication_date": "2014-01-01T00:00:00Z",
}
for a book entity defined by name, author_id and publication date.
My REST API returns exactly the same except "_id" instead of "id".
I think this is the reason I can't retrieve data from my API. How can I solve this issue?
Use the entity identifier() method, as explained in the doc (http://ng-admin-book.marmelab.com/doc/API-mapping.html):
var post = nga.entity('posts')
.identifier(nga.field('_id'));

Resources