Vet*_*ack 28 node.js graphql graphql-js
我目前正在尝试使用NodeJS的GraphQL,我不知道,为什么以下查询会出现此错误:
{
library{
name,
user {
name
email
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如果type我的resolveLibrary是正确的,因为在任何例子,我看了一下他们使用new GraphQL.GraphQLList(),但对我来说我真想回到一个用户对象,而不是用户的数组.
我的代码:
const GraphQL = require('graphql');
const DB = require('../database/db');
const user = require('./user').type;
const library = new GraphQL.GraphQLObjectType({
name: 'library',
description: `This represents a user's library`,
fields: () => {
return {
name: {
type: GraphQL.GraphQLString,
resolve(library) {
return library.name;
}
},
user: {
type: user,
resolve(library) {
console.log(library.user);
return library.user
}
}
}
}
});
const resolveLibrary = {
type: library,
resolve(root) {
return {
name: 'My fancy library',
user: {
name: 'User name',
email: {
email: 'test@123.de'
}
}
}
}
}
module.exports = resolveLibrary;
Run Code Online (Sandbox Code Playgroud)
错误:
Error: Expected Iterable, but did not find one for field library.user.
Run Code Online (Sandbox Code Playgroud)
所以我的library架构提供了一个user返回正确数据的字段(调用console.log).
Vin*_*han 44
我也遇到了这个问题,但似乎很明显为什么会发生这种情况.您从解析程序返回的内容似乎与架构中的返回类型不匹配.
特别是对于错误消息Expected Iterable, but did not find one for field library.user.,您的架构需要一个数组(Iterable)但您没有在解析器中返回一个数组
我在schema.js中有这个:
login(email: String, password: String): [SuccessfulLogin]
我把它改为:
login(email: String, password: String): SuccessfulLogin
注意"SuccessfulLogin"周围的方括号.无论您是想更新解析器返回类型还是更新架构的期望,都取决于您
我遇到了同样的问题,但我正在使用 GraphQL 和 Go。
解决方案: 我提到返回类型是列表(或者可以说是数组),但我的解析器函数返回一个接口而不是接口列表。
之前 =>
Type: graphql.NewList(graphqll.UniversalType)
Run Code Online (Sandbox Code Playgroud)
后来我把它改为=>
Type: graphqll.UniversalType
Run Code Online (Sandbox Code Playgroud)
graphqll.UniversalType :“graphqll”是我的用户定义包的名称,“UniversalType”是我创建的 GraphQL 对象。
graphql 对象以前的结构是:
var GetAllEmpDet = &graphql.Field{
Type: graphql.NewList(graphqll.UniversalType),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
...
...
// Your resolver code goes here, how you handle.
...
return models.Universal, nil // models.Universal is struct and not list of struct so it gave that error.
},
}
Run Code Online (Sandbox Code Playgroud)
当我将其更改为:
var GetAllEmpDet = &graphql.Field{
Type: graphqll.UniversalType,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
...
...
// Your resolver code goes here, how you handle.
...
return models.Universal, nil // models.Universal is struct and not list of struct so it gave that error.
},
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21185 次 |
| 最近记录: |