在GraphQL中构造中继规范查看器
继电器的Schema.graphql,这是我试图实现的结构在GraphQL中构造中继规范查看器,graphql,relayjs,graphql-js,Graphql,Relayjs,Graphql Js,继电器的Schema.graphql,这是我试图实现的结构 type Root { viewer: Viewer } type Viewer { categories(id: ID): Category subCategories(CategoryId: ID!) : [SubCategory] items(SubCategoryId: ID): Item shopItems(ShopId: ID, SubCategoryId:ID): Item } 现在服务器my vi
type Root {
viewer: Viewer
}
type Viewer {
categories(id: ID): Category
subCategories(CategoryId: ID!) : [SubCategory]
items(SubCategoryId: ID): Item
shopItems(ShopId: ID, SubCategoryId:ID): Item
}
现在服务器my viewer.js看起来像这样
const Viewer = new GraphQLObjectType({
name: 'viewer',
fields: {
categories,
subCategories,
Items,
shopItems,
},
});
const RootQuery = new GraphQLObjectType({
name: 'query',
fields: {
viewer: {
type: Viewer,
resolve(viewer, args) {
return {viewer: viewer}
}
},
},
});
而根源是,
const RootQuery = new GraphQLObjectType({
name: 'query',
fields: {
viewer: {
type: Viewer,
},
},
});
它提供了我想要的结构,但我无法从rootQuery解析任何内容,查看器只返回null,因为我没有解析rootQuery中的查看器类型。如何正确实施继电器规范结构,有什么想法吗?谢谢大家,在没有显示解析器代码的情况下(也许您使用的是默认值),这应该会有所帮助 您正在使用一种可接受的模式,即拥有一个根
viewer
对象,其“type”是真正的根查询类型。该类型需要为其字段提供一种解决实际体系结构的方法。例如:
const subCategories = {
type: GraphQLList({ ofType: SubCategory }),
arguments: {
CategoryId: { type: GraphQLNonNull({ ofType: GraphQLID }),
},
resolve(rootValue, { CategoryId }, context, info) {
// look stuff up based on CategoryId and return it
},
};
const Viewer = new GraphQLObjectType({
name: 'viewer',
fields: {
categories,
subCategories,
Items,
shopItems,
},
});
在没有显示解析器的代码的情况下(可能您使用的是默认值),这应该会有所帮助 您正在使用一种可接受的模式,即拥有一个根
viewer
对象,其“type”是真正的根查询类型。该类型需要为其字段提供一种解决实际体系结构的方法。例如:
const subCategories = {
type: GraphQLList({ ofType: SubCategory }),
arguments: {
CategoryId: { type: GraphQLNonNull({ ofType: GraphQLID }),
},
resolve(rootValue, { CategoryId }, context, info) {
// look stuff up based on CategoryId and return it
},
};
const Viewer = new GraphQLObjectType({
name: 'viewer',
fields: {
categories,
subCategories,
Items,
shopItems,
},
});
愚蠢的我,通过这样解决问题
const Viewer = new GraphQLObjectType({
name: 'viewer',
fields: {
categories,
subCategories,
Items,
shopItems,
},
});
const RootQuery = new GraphQLObjectType({
name: 'query',
fields: {
viewer: {
type: Viewer,
resolve(viewer, args) {
return {viewer: viewer}
}
},
},
});
愚蠢的我,通过这样解决问题
const Viewer = new GraphQLObjectType({
name: 'viewer',
fields: {
categories,
subCategories,
Items,
shopItems,
},
});
const RootQuery = new GraphQLObjectType({
name: 'query',
fields: {
viewer: {
type: Viewer,
resolve(viewer, args) {
return {viewer: viewer}
}
},
},
});
我已经做到了。这些字段已经定义,我正在导入它们。Grapqhl希望我解析根查询中的查看器字段。我显然不能这样做。当然你可以这样做,只需为
图形ql
提供你自己的解析器函数即可。我必须解析查看器类型,其中包含已解析的字段类别、子类别等。如果我不解析查看器,查询将返回null,因为它希望解析查看器。您尝试过这里给出的模式吗?Viewer
的各个字段将具有如上所述的解析器函数,这些函数将返回适当创建的JS对象。这些字段已经定义,我正在导入它们。Grapqhl希望我解析根查询中的查看器字段。我显然不能这样做。当然你可以这样做,只需为图形ql
提供你自己的解析器函数即可。我必须解析查看器类型,其中包含已解析的字段类别、子类别等。如果我不解析查看器,查询将返回null,因为它希望解析查看器。您尝试过这里给出的模式吗?Viewer
的各个字段将具有如上所述的解析器函数,这些函数将返回适当创建的JS对象。