簡體   English   中英

GraphQL + Apollo 錯誤:未捕獲(承諾)

[英]GraphQL + Apollo Error: Uncaught (in promise)

我正在嘗試通過單擊按鈕將新數據提交到我的本地 graphQL API,但出現錯誤:錯誤錯誤:未捕獲(承諾):錯誤:網絡錯誤: http://localhost:4000/ 的Http 失敗響應graphql :400 錯誤請求。

這個想法是當你按下按鈕時,新數據被提交

  <button type="submit" (click)="onSubmit()" [disabled]="!personForm.valid" id="buttonSubmit" mat-raised-button color="accent"class="floated">Send</button>

onSubmit 函數調用變異

  onSubmit() 
  {
    let id = 5;
    let vorname = 'user';
    let name =  'name';
    let email =  'testt@hotmail.com';
    let status =  'true';
    let activity =  'Office';

    this.apollo.mutate<CreateUserMutationResponse>({
      mutation: CREATE_USER_MUTATION,
      variables: {
        id: id,
        vorname: vorname,
        name: name,
        email: email,
        status: status,
        activity: activity
      }
    }).subscribe((response) => {

    });

    console.log('submitted');

  }

我的 types.ts 中的突變看起來像這樣

export const CREATE_USER_MUTATION = gql`
  mutation CreateUserMutation($id: ID!, $vorname: String!, $name: String!, $email: String!, $status: String!, $activity: String) {
    createUser(id: $id,vorname: $vorname, name: $name, email: $email, status: $status, activity: $activity) {
      id
      vorname
      name
      email
      status
      activity
    }
  }
`;

export interface CreateUserMutationResponse {
  createUser: String;
}

當我發現你的問題時,我實際上遇到了同樣的問題。 這方面的文檔很難找到,但是, 這個例子讓我知道如何解決這個問題。 我在try catch 語句中包圍了突變后,它對我有用 像這樣的事情我很確定也會為你解決:

onSubmit() {

  [...]

  try {
    this.$apollo.mutate <CreateUserMutationResponse>({
      mutation: CREATE_USER_MUTATION,
      variables: {
        id: id,
        vorname: vorname,
        name: name,
        email: email,
        status: status,
        activity: activity
       }
     }).subscribe((response) => { });
   } catch (e) {
      console.error(e);
   }

  console.log('submitted');

該頁面還幫助我首先設置了突變(看起來您已經正確地做了)。

暫無
暫無

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

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