簡體   English   中英

如何從 nodejs function 正確執行 GraphQL 更新突變

[英]How to properly do a GraphQL update mutation from a nodejs function

我需要知道如何通過從我的 nodejs lambda 調用 aws-amplify graphql api 來進行更新突變,

我的 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);

我的更新突變如下但它不起作用,

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();

上面給出的是我的更新突變代碼,它不起作用。 我怎樣才能解決這個問題?

我的更新突變不起作用它只更新了updatedAt_lastChangedAt
這是因為我沒有在變量中傳遞_version 所以現在我的變量看起來像這樣:

const variables = {
  input: {
    id: initialObject.id
    name: 'New name',
    _version: initialObject._version
  }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM