在GraphQL中构建中继规范查看器

时间:2017-09-18 20:21:53

标签: 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
}

现在服务器,我的viewer.js看起来像这样,

const Viewer = new GraphQLObjectType({
  name: 'viewer',
  fields: {
    categories,
    subCategories,
    Items,
    shopItems,
  },
});

,根是,

const RootQuery = new GraphQLObjectType({
  name: 'query',
  fields: {
    viewer: {
      type: Viewer,
    },
  },
});

它给了我想要的结构,但是我无法从rootQuery解析任何东西,查看器只返回null,因为我没有在rootQuery中解析查看器类型。任何想法如何正确实现这个中继规范结构?谢谢你们

2 个答案:

答案 0 :(得分:0)

如果没有显示你的解析器的代码(也许你正在使用默认代码),这应该会有所帮助。

您正在使用已接受的根viewer对象模式,其“类型”是真正的根查询类型。该类型需要为其字段提供一种解决您的真实体系结构的方法。例如:

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,
  },
});

答案 1 :(得分:0)

愚蠢的我,通过像这样解析观众来解决它,

const RootQuery = new GraphQLObjectType({
  name: 'query',
  fields: {
    viewer: {
      type: Viewer,
      resolve(viewer, args) {
       return {viewer: viewer}
     }
    },
  },
});
相关问题