在 GraphQL 中是否可以在对象类型级别上使用解析器?

In GraphQL is it possible to have resolvers on object type level?

我们正在处理一个相当复杂的 GraphQL 架构,其中我们有几个属于各种微服务的对象类型,其中每个对象类型都有一个我们可以查询的自然 API 端点。因此,如果可以直接为某些对象类型定义特定的解析器,那将是非常方便的,就像这样:

const typeDefs = gql`
    type Query {
      getBook(bookId: ID!): BookPayload
    }

    type BookPayload {
        book: Book
        userErrors: UserError
    }

    type Book {
      id: ID!
      title: String
      author: String
    }
`;

const resolvers = {
    Query: {
        getBook: (parent, args, context, info) => {
            return {
                book: { id: args.bookId }
        }
    },
    Book: (parent) => {  // this object type level resolver doesn't seem to work
        return {
            id: parent.id,
            ...fetchBookMetadata(parent.id)
        };
    }
};

我知道这是一个微不足道的例子,可能看起来有点过度设计,但当架构开始变得非常复杂且到处都有数百个交叉引用时,它确实更有意义(至少对我们而言)。现在有什么好的方法可以解决吗?

是的,您应该可以使用指令执行此操作或非常类似的操作,请查看:

https://www.apollographql.com/docs/graphql-tools/schema-directives.html#Fetching-data-from-a-REST-API

我会从字面上 post 引用本文中的示例。

Suppose you’ve defined an object type that corresponds to a REST resource, and you want to avoid implementing resolver functions for every field

const typeDefs = `
directive @rest(url: String) on FIELD_DEFINITION

type Query {
  people: [Person] @rest(url: "/api/v1/people")
}`;

class RestDirective extends SchemaDirectiveVisitor {
  public visitFieldDefinition(field) {
    const { url } = this.args;
    field.resolve = () => fetch(url);
  }
}

根据规范,GraphQL 执行引擎在选择集上运行,这些选择集被分解为各个字段。将检查每个字段的值或现有解析器。

似乎如果您定义一个指令,例如上面的指令,您不会更改此基本行为,但您确实拦截并添加了一个额外的自定义步骤,以便在进一步解析之前执行。

也许自定义标量可以实现类似的功能,但这不适用于模式设计。