javascript - GraphQL + Apollo 错误 : Uncaught (in promise)

标签 javascript typescript graphql apollo

我正在尝试通过单击按钮将新数据提交到本地 graphQL API,但收到错误:错误错误:未捕获( promise 中):错误:网络错误:http://localhost:4000/graphql 的 Http 失败响应: 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 =  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="334756404747735b5c475e525a5f1d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>';
    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;
}

最佳答案

当我发现你的问题时,我实际上也遇到了同样的问题。关于此的文档很难获得,但是,this example告诉我如何解决这个问题。当我将突变包围在 try catch statement 中后,它对我有用。 。 我确信这样的事情也会为您解决:

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

This page还帮助我首先设置了突变(看来你已经做得正确了)。

关于javascript - GraphQL + Apollo 错误 : Uncaught (in promise),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48885492/

相关文章:

javascript - 多个类上的 querySelectorAll 不起作用

javascript - 幻影 Node 不传递 page.evaluate 函数的返回值

angular - 在 Angular4 和 Material2 中提交表单

Angular 使用 Apollo : Unit testing: Error: Client has not been defined yet

javascript - 如何更改 javascript 字符串的一个单词的背景颜色?

javascript - 如何在不使用 lambda 函数的情况下将参数发送到 React 渲染组件中的处理程序事件?

typescript - 从已解析的 Promise 返回 json 对象并在加载后分配它

typescript - 为什么返回类型 `null` (或任何其他类型)可分配给返回类型 `void` ?

functional-programming - 如何将数据连接到 react-router-relay 中的深层组件?

java - GraphQL java : Throwing exceptions when request has unused variables