Javascript 相互依赖的graphQl类型
我想设计一个graphQl模式,其中两种类型是相互依赖的。基本上,我有一个数据库结构,其中:Javascript 相互依赖的graphQl类型,javascript,graphql,Javascript,Graphql,我想设计一个graphQl模式,其中两种类型是相互依赖的。基本上,我有一个数据库结构,其中: User.hasOne(搜索) 我希望能够进行如下graphQl查询: // note that 'me' has User type // search has Search type query{ me{ email, search{ content, user{ id } } } } 因此,您可以看到,我们请求一个
User.hasOne(搜索)
我希望能够进行如下graphQl查询:
// note that 'me' has User type
// search has Search type
query{
me{
email,
search{
content,
user{
id
}
}
}
}
因此,您可以看到,我们请求一个用户
,它的搜索
,以及拥有该搜索
的用户(在这种情况下,这是没有用的)
以下是用户类型的定义:
import {
GraphQLObjectType as ObjectType,
GraphQLID as ID,
GraphQLString as StringType,
GraphQLNonNull as NonNull,
GraphQLInt as IntType,
} from 'graphql';
const UserType = new ObjectType({
name: 'User',
fields: {
id: { type: new NonNull(ID) },
username: { type: StringType },
search: {
type: SearchType,
resolve(user){
return user.getSearch();
}
},
},
});
const SearchType = new ObjectType({
name: 'Search',
fields: {
id: { type: new NonNull(ID) },
user: {
type: UserType,
resolve(search){
return search.getUser();
}
},
content: { type: StringType },
},
});
以下是我对搜索类型的定义:
import {
GraphQLObjectType as ObjectType,
GraphQLID as ID,
GraphQLString as StringType,
GraphQLNonNull as NonNull,
GraphQLInt as IntType,
} from 'graphql';
const UserType = new ObjectType({
name: 'User',
fields: {
id: { type: new NonNull(ID) },
username: { type: StringType },
search: {
type: SearchType,
resolve(user){
return user.getSearch();
}
},
},
});
const SearchType = new ObjectType({
name: 'Search',
fields: {
id: { type: new NonNull(ID) },
user: {
type: UserType,
resolve(search){
return search.getUser();
}
},
content: { type: StringType },
},
});
不幸的是,这不起作用,我想是由于UserType
ansSearchType
之间的相互依赖,我得到了以下错误:
User.search field type must be Output Type but got: function type() {
return _SearchType2.default;
}
有什么方法可以实现用户
和搜索
之间的这种相互依赖关系吗?
看看PersonType是如何依赖自身的
var PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: { type: GraphQLString },
bestFriend: { type: PersonType },
})
});
你要找的东西是
fields: () => ({
...
})
从本页
看看PersonType是如何依赖自身的
var PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: { type: GraphQLString },
bestFriend: { type: PersonType },
})
});
你要找的东西是
fields: () => ({
...
})