在 Apollo 中查找缺失的 GraphQL 解析器

Find Missing GraphQL Resolvers in Apollo

使用 apollo-server,如果我试图定义一个在我的架构中不存在的 graphql 解析器,计算机就会对我大喊大叫。

[Error: Query.foo defined in resolvers, but not in schema]

这个不错!

是否可以使用 apollo(或其他?)工具做相反的事情?也就是说,有没有办法让 apollo 告诉我是否存在在我的架构中定义但 在我的解析器中定义的查询、变更或订阅操作?

有一个可选的 resolverValidationOptions 对象可以传递给 makeExecutableSchema 以强制执行一些额外的解析器验证,如图 here:

  • requireResolversForArgs will cause makeExecutableSchema to throw an error if no resolve function is defined for a field that has arguments.
  • requireResolversForNonScalar will cause makeExecutableSchema to throw an error if a non-scalar field has no resolver defined. Setting this to true can be helpful in catching errors, but defaults to false to avoid confusing behavior for those coming from other GraphQL libraries.
  • requireResolversForAllFields asserts that all fields have a valid resolve function.
  • requireResolversForResolveType will require a resolveType() method for Interface and Union types. This can be passed in with the field resolvers as __resolveType(). False to disable the warning.
  • allowResolversNotInSchema turns off the functionality which throws errors when resolvers are found which are not present in the schema. Defaults to false, to help catch common errors.
其中

None 专门针对根类型上的 字段,但您可以使用一个或多个。

这些选项不会直接通过 ApolloServer 公开,但您始终可以这样做:

const schema = makeExecutableSchema({ typeDefs, resolvers, resolverValidationOptions })
const apollo = new ApolloServer({ schema })