GraphQL,Apollo服务器联合架构

时间:2020-09-01 21:41:50

标签: graphql apollo-server apollo-federation

查询时出现此错误:

Event

这是查询:

document.querySelector('form').addEventListener('submit', e => {
    console.log(e.submitter);
    e.preventDefault();
});

此查询的架构为:

<form>
    <input placeholder="Type Enter here!">
    <button>Submit</button>
    <button>Just another submit button</button>
</form>

另一个架构具有"Cannot return null for non-nullable field Transaction.createdAt." 类型:

query getMyTransactions {
  getMyTransactions {
    id
    createdAt
  }
}

出什么问题了?

编辑:如果我查询:

extend type Transaction @key(fields: "id") {
  id: ID! @external
}

type Query {
  getMyTransactions: [Transaction!]!
}

好的,我可以获取查询的所有Transaction,但是如果我包含另一个属性,查询将失败。

1 个答案:

答案 0 :(得分:1)

简而言之-您将createdAt类型声明为non-null的值,但是数据createdAt中的某个位置是null

createdAt: String!

!在类型名称之后。这是我们的服务器始终期望的 为此字段返回一个非空值,如果最终得到一个 将实际触发GraphQL执行错误的null值, 让客户知道出了点问题。 GraphQl docs

示例

对于此远程/本地数据(createdAt 缺少第一个对象):

const Transaction = [
  {
    id: "1",
    /* createdAt: "01/09/2020" */ /* missing from the data */
  },
  {
    id: "2",
    createdAt: "02/09/2020"
  }
];

执行查询

query{
  getMyTransactions {
    id
    createdAt
    }
}

抛出错误:

“ message”:“无法为不可为空的字段返回null Transaction.createdAt。”,

enter image description here

要解决此问题:

  • 选项1::添加一些解析器逻辑和/或添加与所需数据相关的验证。
  • 选项2::从createdAt中删除要求(允许返回null): enter image description here