GraphQL 解析器参数的错误顺序(根、参数、上下文) [英] Wrong order of GraphQL resolver arguments (root, args, context)

查看:18
本文介绍了GraphQL 解析器参数的错误顺序(根、参数、上下文)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我的参数似乎在我的 GraphQL 解析器中切换.我正在使用 express-graphql.

I'm wondering why my arguments seem to be switched around inside my GraphQL resolver. I'm using express-graphql.

一个解析器的例子:

  getLocalDrivers: async (parent, args, ctx) => {
    console.log(ctx);
  }

我已经按照文档中出现的方式编写了参数名称:http://graphql.org/学习/执行/

I've written the argument names as they appear in the docs: http://graphql.org/learn/execution/

但是当我调试和检查对象时,似乎 args 对象是第一个,上下文是第二个,父/根对象是第三个.

But when I debug and inspect the objects, it seems the args object is 1st, the context is 2nd, and the parent/root is 3rd.

父母:

Object {location: "020202"}

参数:

IncomingMessage {_readableState: ReadableState, readable: false, domain: null, …}

上下文:

Object {fieldName: "getLocalDrivers", fieldNodes: ....

一些服务器代码:

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

我的 rootResolver:

My rootResolver:

var rootResolver = {
     getLocalDrivers: async (obj, args, ctx) => {
       console.log(ctx);
  }
}

架构:

var { buildSchema } = require("graphql");
var schema = buildSchema(`
  type Query {
    getLocalDrivers(location: String): [Driver]
  }

  type Driver {
    name: String
    location: String    
  }`);

推荐答案

如果为一个字段定义了一个resolve函数,那么当GraphQL解析那个字段时,它会通过四个 该函数的参数:

If a resolve function is defined for a field, when GraphQL resolves that field, it will pass four parameters to that function:

  1. 父字段解析的值(通常称为 objroot)
  2. 该字段的参数
  3. 上下文
  4. 描述整个 GraphQL 请求的信息对象

如果某个特定字段没有解析函数,GraphQL 将使用默认解析器,它会简单地搜索父字段上的属性,并在找到时使用该属性.

If a resolve function is absent for a particular field, GraphQL will utilize the default resolver, which simply searches for a property on the parent field and uses that if it's found.

所以你的 getLocalDrivers 查询可以返回一个 Driver 对象的数组,只要 Driver 对象有一个 name 属性,name field 将解析为该属性的值.

So your getLocalDrivers query could return an array of Driver objects, and as long as the Driver object has a name property, the name field will resolve to that property's value.

巧合的是,Driver 对象上的 name 属性也可以是一个函数.在这种情况下,GraphQL 将调用该函数以获取其返回值.与解析器非常相似,GraphQL 将一些信息作为参数传递给该函数,即 1) 参数,2) 上下文和 3) 信息对象.以这种方式解析字段时,省略obj"参数.

Coincidentally, the name property on that Driver object could also be a function. In that case, GraphQL would call that function to get its return value. And very much like a resolver, GraphQL passes some information to this function as parameters, namely 1) the arguments, 2) the context and 3) the info object. When fields are resolved in this way, the "obj" parameter is omitted.

好的,那么根呢?

根对象只是用作查询和突变的父字段值"的对象,这些字段与其他所有字段一样.

The root object is just the object that serves as the "parent field value" that's given to queries and mutations, which are fields like everything else.

因此,如果您尚未为 getLocalDrivers 定义解析"函数(例如,因为您使用 buildQuery 编译了架构),GraphQL 将使用默认解析器,并使用您传入的根对象作为父字段值".它看到一个getLocalDrivers,但如上所述,因为这是一个函数,它使用上述三个参数调用该函数.

So, if you haven't defined a "resolve" function for getLocalDrivers (because you compiled you schema with buildQuery for example), GraphQL will utilize the default resolver, and use the root object you passed in as the "parent field value". It sees a getLocalDrivers, but as described above, because this a function, it calls that function with the above-mentioned three parameters.

那么这里的教训是什么?

不要使用 root.

说真的.将您的架构定义为对象,或者如果您想使用 GraphQL 架构语言编写架构,请使用 graphql-tools -- makeExecutableSchema 使处理解析器变得更加容易.

Seriously. Either define your schema as an object, or if you want to write the schema out using GraphQL schema language, use graphql-tools -- makeExecutableSchema makes dealing with resolvers much much easier.

const typeDefs = `
  type Query {
    getLocalDrivers(location: String): [Driver]
  }

  type Driver {
    name: String
    location: String    
  }
`
const resolvers = {
  Query: {
    getLocalDrivers: (obj, args, ctx) => {
       console.log({obj, args, ctx})
    }
  }
}
const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
})

这篇关于GraphQL 解析器参数的错误顺序(根、参数、上下文)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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