具有变量的Apollo角度查询无法解析

时间:2018-11-05 08:47:45

标签: angular typescript graphql angular7 apollo-angular

我有用于分页的服务器API

 {
  accounts (offset: 2, limit: 1) {
    key
    name
  }
}

使用Graphiql都能正常工作

{
  "data": {
    "accounts": [
      {
        "key": "fde3d97d-6a44-4c36-bc59-149a3e8e51ac",
        "name": "a0f353611567c83e"
      }
    ]
  }
}

我按照Apollo文档将Web客户端连接到后端。 默认查询有效,但是当我尝试添加变量时,在服务器端出现此错误。

WARN  g.GraphQL - Query failed to validate : 'query Accounts($offset: Int, $limit: Int) {
      accounts(offset: $offset, limit: $limit) {
        key
        name
        __typename
   }
 }

我的角度(7)代码如下:

import { Component, OnInit } from "@angular/core";
import { Apollo, QueryRef } from "apollo-angular";
import { DocumentNode } from "graphql";
import { Observable, Subscription } from "rxjs";
import gql from "graphql-tag";

const ACCOUNTS = gql`
  query Accounts($offset: Int, $limit: Int) {
    accounts(offset: $offset, limit: $limit) {
      key
      name
    }
  }
`;

@Component({
  selector: "app-accounts",
  templateUrl: "./accounts.component.html",
  styleUrls: ["./accounts.component.scss"]
})
export class AccountsComponent implements OnInit {

  private data: Observable<any>;

  constructor(private apollo: Apollo) {}

  ngOnInit() {
    this.data = this.apollo.watchQuery({
      query: ACCOUNTS,
      variables: {
        offset: 0,
        limit: 3
      }
    }).valueChanges;
  }        
}

当引入参数(变量)时,似乎根本没有解析GQL。

有什么建议吗?

编辑

转到新的Apollo代码生成器后,我具有从服务器获取的所有类型以及为我生成的所有类型。

仍然,我不知道为什么查询不适用于变量。

import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";

import { AccountsGQL, Account } from "../generated/graphql";

@Component({
  selector: "app-accounts",
  templateUrl: "./accounts.component.html",
  styleUrls: ["./accounts.component.scss"]
})
export class AccountsComponent implements OnInit {
  private data: Observable<Account[]>;

  constructor(private accountsGQL: AccountsGQL) {}

  ngOnInit() {
    this.data = this.accountsGQL
      .watch({ offset: 0, limit: 1 })
      .valueChanges.pipe(map(result => result.data.accounts));
  }
}
  

'offset'已将NonNull类型'Int!'的Null值强制转换为

0 个答案:

没有答案