解决Express GraphQL中的嵌套数据 [英] Resolving nested data in express GraphQL

查看:258
本文介绍了解决Express GraphQL中的嵌套数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试解析一个简单的配方列表,其中包含对食材的引用.

I am currently trying to resolve a simple recipe list that has a reference to ingredients.

数据布局如下:

type Ingredient {
  name: String!
  amount: Int!
  unit: Unit!
  recipe: Recipe
}

type Recipe {
  id: Int!
  name: String!
  ingredients: [Ingredient]!
  steps: [String]!
  pictureUrl: String!
}

据我了解,我的解析器应如下所示: 第一个解析配方,第二个解析配方中的成分字段.根据我的理解,它可以使用配方提供的参数.在我的配方对象中,该成分由id(int)引用,因此这应该是参数(至少我是这样认为的).

As I understand it, my resolvers should look like this: The first one resolves the recipes and second one resolves the ingredient field in the recipe. It can (from my understanding) use the argument provided by recipe. In my recipe object, the ingredient is referenced by id (int), so this should be the argument (at least that's what I think).

var root = {
  recipe: (argument) => {
       return recipeList;
  },
  Recipe: {
    ingredients: (obj, args, context) => {
        //resolve ingredients
    }
  },

这些解析器通过以下方式传递到应用程序:

These resolvers are passed to the app like this:

app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true,
  rootValue: root,
}));

但是,我的解析器似乎没有被调用.我希望在查询中即时解决所有成分.

However, my resolver does not seem to be called. I would like the ingredients to be resolved on the fly when queried in my query.

该端点有效,但是一旦我查询成分,就会返回此消息"message": "Cannot return null for non-nullable field Ingredient.name.",的错误.

The endpoint works, but as soon as I query for ingredients, an error with this message "message": "Cannot return null for non-nullable field Ingredient.name.", is returned.

当尝试在解析器中记录传入的参数时,可以看到它从未执行过.不幸的是,我找不到像我一样使用express-graphql时如何使用它的示例.

When trying to log the incoming arguments in my resolver, I can see that it is never executed. Unfortunately, I can't find examples on how to do this with express-graphql when using it like I am.

如何在express-graphQL中为嵌套类型编写单独的解析器?

推荐答案

只能通过root定义用于查询和变异的解析器,即使那样,这也是一种不好的做法.我猜想您正在使用buildSchema构建架构,因为生成的架构将仅使用默认解析器..

Only resolvers for queries and mutations can be defined through root, and even then this is bad practice. I'm guessing you're building your schema using buildSchema, which is generally a bad idea since the generated schema will only use default resolvers.

使用纯GraphQL.js时,为ingredients之类的字段定义解析器的唯一方法是不使用buildSchema.您可以通过编程方式定义它(而不是从字符串生成模式)(即定义GraphQLSchema及其使用的所有类型).

The only way to define resolvers for a field like ingredients when using plain GraphQL.js is to not use buildSchema. Instead of generating your schema from a string, you would define it programatically (i.e. defining a GraphQLSchema and all the types it uses).

执行上述操作非常麻烦,尤其是如果您已经在字符串或文档中定义了架构的话​​.因此,替代方法是使用graphql-tools的 makeExecutableSchema ,这样您就可以像尝试那样将那些解析器注入到类型定义中. makeExecutableSchema返回一个GraphQLSchema对象,因此您可以将其与现有代码一起使用(不需要,无需将中间件更改为apollo服务器).

Doing the above is a huge pain, especially if you already have your schema defined in a string or document. So the alternative option is to use graphql-tools' makeExecutableSchema, which lets you inject those resolvers into your type definitions like you're trying to do. makeExecutableSchema returns a GraphQLSchema object, so you can use it with your existing code (you don't have to change your middleware to apollo-server if you don't want to).

这篇关于解决Express GraphQL中的嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆