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

时间:2017-10-17 00:33:26

标签: node.js graphql

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

代码(重要删除部分字段)

NoteType

当我取消注释de 用户时出现错误 错误:注意。用户字段类型必须是输出类型但得到:[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;

NoteQuery

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;

UserQuery

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 个答案:

答案 0 :(得分:3)

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

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

但是,我建议只使用buildSchema使用字符串文字声明您的模式,然后通过根对象传入解析器。或使用graphql-tool' makeExecutableSchema,这更容易。无论哪种方式,您都可以省去处理循环依赖关系的麻烦,并使您的架构更具可读性。