错误:Note.user 字段类型必须是输出类型,但得到:[object Object]

Posted

技术标签:

【中文标题】错误:Note.user 字段类型必须是输出类型,但得到:[object Object]【英文标题】:Error: Note.user field type must be Output Type but got: [object Object] 【发布时间】:2018-03-28 14:15:49 【问题描述】:

我是graphql的新手,我正在玩一个简单的案例,但无法让它工作,案例是简单的一对多,用户有很多笔记,一个笔记属于用户。 我的问题是“NoteType”我无法返回“UserType”,我可以返回创建 Note 但不是 UserType 的用户的用户名

代码(重要删除一些字段)

笔记类型

当我取消注释 de user 我有错误 Error: Note.user field type must be Output Type but got: [object Object].强>

import 
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLBoolean,
 from 'graphql';

import UserType from '../User/UserType';
import User from '../User/User';

const NoteType = new GraphQLObjectType(
  name: 'Note',
  description: 'This represents a Note',
  fields: () => (
    id: 
      type: GraphQLInt,
      resolve: (note) => note.id,
    ,
    userId: 
      type: GraphQLInt,
      resolve: (note) => note.userId,
    ,
    title: 
      type: GraphQLString,
      resolve: (note) => note.note,
    ,
    username: 
      type: GraphQLString,
      resolve: (note) => (
        User
          .findOne(
            where: 
              id: note.userId,
            ,
          ).then(user => user.username)
      ),
    ,
    /* user:   ***PROBLEM HERE!!***
      type: UserType,
      resolve: (note) => (
        User
          .findOne(
            where: 
              id: note.userId,
            ,
          ).then(user => user)
      ),
    ,*/
  ),
);

module.exports = NoteType;

用户类型

import 
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLList,
 from 'graphql';

import NoteType from '../Note/NoteType';
import Note from '../Note/Note';

import LocationType from '../Location/LocationType';
import Location from '../Location/Location';

const UserType = new GraphQLObjectType(
  name: 'User',
  description: 'This represents a User',
  fields: () => (
    id: 
      type: GraphQLInt,
      resolve: (user) => user.id,
    ,
    username: 
      type: GraphQLString,
      resolve: (user) => user.username,
    ,
    email: 
      type: GraphQLString,
      resolve: (user) => user.email,
    ,
    notes: 
      type: new GraphQLList(NoteType),
      resolve: (user) => (
        Note
          .findAll(
            where: 
              userId: user.id,
            ,
          )
      ),
    ,
  ),
);

module.exports = UserType;

笔记查询

import 
  GraphQLInt,
  GraphQLString,
  GraphQLList,
 from 'graphql';

import NoteType from '../../models/Note/NoteType';
import Note from '../../models/Note/Note';

import UserType from '../../models/User/UserType';
import User from '../../models/User/User';

const noteQuery = 
  type: new GraphQLList(NoteType),
  args: 
    id: 
      name: 'id',
      type: GraphQLInt,
    ,
    userId: 
      name: 'userId',
      type: GraphQLInt,
    ,
    user: 
      name: 'user',
      type: GraphQLString,
    ,
    note: 
      name: 'note',
      type: GraphQLString,
    ,
    createdAt: 
      name: 'createdAt',
      type: GraphQLString,
    ,
    updatedAt: 
      name: 'updatedAt',
      type: GraphQLString,
    ,
  ,
  resolve: (user, args) => Note.findAll( where: args ),
;

module.exports = noteQuery;

用户查询

import 
  GraphQLInt,
  GraphQLString,
  GraphQLList,
 from 'graphql';

import UserType from '../../models/User/UserType';
import User from '../../models/User/User';

const userQuery = 
  users: 
    type: new GraphQLList(UserType),
    args: 
      id: 
        name: 'id',
        type: GraphQLInt,
      ,
      username: 
        name: 'username',
        type: GraphQLString,
      ,
      email: 
        name: 'email',
        type: GraphQLString,
      ,
      createdAt: 
        name: 'createdAt',
        type: GraphQLString,
      ,
      updatedAt: 
        name: 'updatedAt',
        type: GraphQLString,
      ,
    ,
    resolve: (user, args) => User.findAll( where: args ),
  ,
  user: 
    type: UserType,
    args: 
      id: 
        name: 'id',
        type: GraphQLInt,
      ,
      username: 
        name: 'username',
        type: GraphQLString,
      ,
      email: 
        name: 'email',
        type: GraphQLString,
      ,
      createdAt: 
        name: 'createdAt',
        type: GraphQLString,
      ,
      updatedAt: 
        name: 'updatedAt',
        type: GraphQLString,
      ,
    ,
    resolve: (user, args) => User.findOne( where: args ),
  ,
;

module.exports = userQuery;

任何帮助或提示提前感谢!

【问题讨论】:

【参考方案1】:

看起来像一个循环依赖问题。您将 User 模块导入到 Note 模块中,同时将 Note 模块导入到 User 模块中。 user 字段的解析器正在寻找 GraphQLObjectType 的实例,但它只接收导出类型的未完成副本。

您可以查看this question 以获得更全面的解释和一些解决方法。

但是,我建议使用buildSchema 使用字符串文字声明您的架构,并通过根对象传递您的解析器。或者使用graphql-tool的makeExecutableSchema,更简单。无论哪种方式,您都无需为处理循环依赖而头疼,并使您的架构更具可读性。

【讨论】:

以上是关于错误:Note.user 字段类型必须是输出类型,但得到:[object Object]的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Graphql 项目抛出错误:字段类型必须是输出类型但得到:未定义

Edge.node 字段类型必须是输出类型但得到:未定义

节点 - GraphQL - Ad.user 字段类型必须是输出类型,但得到:未定义

Graphql 日期字段类型必须是输出类型但得到:未定义

带有 Keystone 的 GraphQL 需要字段类型必须是输出类型,但得到:未定义

“文件”类型的 GraphQL 错误字段“图像”必须具有子字段选择。您的意思是“图像 ... ”吗?