查询时出现此错误:
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
,但是如果我包含另一个属性,查询将失败。
答案 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。”,
要解决此问题: