[英]How to properly do a GraphQL update mutation from a nodejs function
I need to know how to do an update mutation by calling aws-amplify graphql api from my nodejs lambda,我需要知道如何通过从我的 nodejs lambda 调用 aws-amplify graphql api 来进行更新突变,
My create mutation looks like this and it works perfectly,我的 create mutation 看起来像这样,而且效果很好,
const query = /* GraphQL */ `
mutation CREATE_DRIVER($input: CreateDriverInput!) {
createDriver(input: $input) {
id
name
_version
createdAt
updatedAt
_lastChangedAt
}
}
`;
const variables = {
input: {
name: 'John',
}
};
const options = {
method: 'POST',
headers: {
'x-api-key': GRAPHQL_API_KEY
},
body: JSON.stringify({ query, variables })
};
const request = new Request(GRAPHQL_ENDPOINT, options);
response = await fetch(request);
body = await response.json();
console.log(body);
And my update mutation is as follows but It doesn't work,我的更新突变如下但它不起作用,
const query = /* GraphQL */ `
mutation UPDATE_DRIVER($input: UpdateDriverInput!) {
updateDriver(input: $input) {
id
name
_version
createdAt
updatedAt
_lastChangedAt
}
}
`;
const variables = {
input: {
id: ID
name: 'New name',
}
};
const options = {
method: 'POST',
headers: {
'x-api-key': GRAPHQL_API_KEY
},
body: JSON.stringify({ query, variables })
};
const request = new Request(GRAPHQL_ENDPOINT, options);
response = await fetch(request);
body = await response.json();
Given above is my update mutation code and it doesn't work.上面给出的是我的更新突变代码,它不起作用。 How can I fix this?
我怎样才能解决这个问题?
My update mutation was not working It only updated the updatedAt
, _lastChangedAt
.我的更新突变不起作用它只更新了
updatedAt
, _lastChangedAt
。
This is because I wasn't passing _version
in my variables.这是因为我没有在变量中传递
_version
。 So now my variables look like this:所以现在我的变量看起来像这样:
const variables = {
input: {
id: initialObject.id
name: 'New name',
_version: initialObject._version
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.