【发布时间】:2018-10-14 03:13:33
【问题描述】:
编辑
添加我的解决方案作为答案
原始问题
我相信这个问题与循环依赖有关。我昨晚花了大半天,今天尝试了我在网上可以找到的所有东西,但似乎没有任何效果。
我尝试过的:
- 将
fieldsprop 转换为返回字段对象的函数 - 将相关字段(在 fields 属性内)转换为返回类型的函数
- 结合上述两种方法
- 最后以 require 语句代替使用引用类型的字段结束(似乎不正确,linter 对此有笔画)
这是文件结构:
代码如下:
userType.js
const graphql = require('graphql');
const Connection = require('../../db/connection');
const ConnectionType = require('../connection/connectionType');
const { GraphQLObjectType, GraphQLList, GraphQLString, GraphQLID } = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString },
email: { type: GraphQLString },
created: {
type: GraphQLList(ConnectionType),
resolve: ({ id }) => Connection.find({ owner: id }),
},
joined: {
type: GraphQLList(ConnectionType),
resolve: ({ id }) => Connection.find({ partner: id }),
},
}),
});
module.exports = UserType;
connectionType.js
const graphql = require('graphql');
const User = require('../../db/user');
const UserType = require('../user/userType');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInt } = graphql;
const ConnectionType = new GraphQLObjectType({
name: 'Connection',
fields: () => ({
id: { type: GraphQLID },
owner: {
type: UserType,
resolve: ({ owner }) => User.findById(owner),
},
partner: {
type: UserType,
resolve: ({ partner }) => User.findById(partner),
},
title: { type: GraphQLString },
description: { type: GraphQLString },
timestamp: { type: GraphQLString },
lifespan: { type: GraphQLInt },
}),
});
module.exports = ConnectionType;
【问题讨论】:
-
你的类型看起来不错,除了
GraphQLList(ConnectionType)应该是new GraphQLList(ConnectionType)因为那是一个类 -
感谢我在发布此内容后尝试了一下。它都不起作用,但对我来说看起来是正确的。我不明白
-
你从问题标题中究竟是从哪里得到这个错误的?
-
加载中的 graphiql 客户端
-
老实说无法判断问题可能是什么。你能在一个较小的独立示例上重现这个吗?
标签: graphql-js circular-reference express-graphql