简体   繁体   English

如何从 nodejs function 正确执行 GraphQL 更新突变

[英]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.

相关问题 Lambda AppSync GraphQL - 从突变请求中获取字符串 - 未找到 - Lambda AppSync GraphQL - get string from mutation request - Not Found GraphQL:以对象作为输入调用突变 - GraphQL: Call a mutation with objects as inputs spanner.Mutation 如何理解更新哪一行 - How does spanner.Mutation understand which row to update aws amplify appsync 中的 Graphql 变异错误 - Graphql mutation error in aws amplify appsync 在返回所有异步函数以更新 firestore 文档之前,如何等待 NodeJS 中的多个异步函数? - How do I wait for multiple async functions in NodeJS before returning them all to update a firestore document? AWS graphql 不更新 - AWS graphql does not update 如何将 GraphQL 查询从 python 发送到 AppSync? - How to send a GraphQL query to AppSync from python? 如何使用 React 和 Firebase V9 更新 function? - How to do the update function using React and Firebase V9? 只有 GraphQL 的 ModelMutations.update() 不能从 Flutter 工作 - ONLY ModelMutations.update() for GraphQL not working from Flutter AWS Amplify CLI 生成的 GraphQL 突变中的 $condition 输入参数是什么? - What is the $condition input parameter for in a GraphQL mutation generated by AWS Amplify CLI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM