使用 Apollo 客户端发出弃用警告
Emit deprecation warnings with Apollo client
背景
我们正在进行一个相当大的 Apollo 项目。我们的 api 的一个非常简化的版本如下所示:
type Operation {
foo: String
activity: Activity
}
type Activity {
bar: String
# Lots of fields here ...
}
我们已经意识到拆分 Operation
和 Activity
没有任何好处并且增加了复杂性。我们想合并它们。但是在代码库中有很多查询采用这种结构。为了使过渡渐进,我们添加 @deprecated
指令:
type Operation {
foo: String
bar: String
activity: Activity @deprecated
}
type Activity {
bar: String @deprecated(reason: "Use Operation.bar instead")
# Lots of fields here ...
}
真题
有什么方法可以突出显示这些弃用的内容吗?最好在(在测试环境中)运行 查询使用弃用字段时在浏览器控制台中打印警告?
所以两年后回到 GraphQL 我才发现 schema directives can be customized(现在?)。所以这是一个解决方案:
import { SchemaDirectiveVisitor } from "graphql-tools"
import { defaultFieldResolver } from "graphql"
import { ApolloServer } from "apollo-server"
class DeprecatedDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field ) {
field.isDeprecated = true
field.deprecationReason = this.args.reason
const { resolve = defaultFieldResolver, } = field
field.resolve = async function (...args) {
const [_,__,___,info,] = args
const { operation, } = info
const queryName = operation.name.value
// eslint-disable-next-line no-console
console.warn(
`Deprecation Warning:
Query [${queryName}] used field [${field.name}]
Deprecation reason: [${field.deprecationReason}]`)
return resolve.apply(this, args)
}
}
public visitEnumValue(value) {
value.isDeprecated = true
value.deprecationReason = this.args.reason
}
}
new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
deprecated: DeprecatedDirective,
},
}).listen().then(({ url, }) => {
console.log(` Server ready at ${url}`)
})
这适用于服务器而不是客户端。它应该打印所有需要的信息来追踪客户端上的错误查询。从维护的角度来看,将它放在服务器日志中似乎更可取。
背景
我们正在进行一个相当大的 Apollo 项目。我们的 api 的一个非常简化的版本如下所示:
type Operation {
foo: String
activity: Activity
}
type Activity {
bar: String
# Lots of fields here ...
}
我们已经意识到拆分 Operation
和 Activity
没有任何好处并且增加了复杂性。我们想合并它们。但是在代码库中有很多查询采用这种结构。为了使过渡渐进,我们添加 @deprecated
指令:
type Operation {
foo: String
bar: String
activity: Activity @deprecated
}
type Activity {
bar: String @deprecated(reason: "Use Operation.bar instead")
# Lots of fields here ...
}
真题
有什么方法可以突出显示这些弃用的内容吗?最好在(在测试环境中)运行 查询使用弃用字段时在浏览器控制台中打印警告?
所以两年后回到 GraphQL 我才发现 schema directives can be customized(现在?)。所以这是一个解决方案:
import { SchemaDirectiveVisitor } from "graphql-tools"
import { defaultFieldResolver } from "graphql"
import { ApolloServer } from "apollo-server"
class DeprecatedDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field ) {
field.isDeprecated = true
field.deprecationReason = this.args.reason
const { resolve = defaultFieldResolver, } = field
field.resolve = async function (...args) {
const [_,__,___,info,] = args
const { operation, } = info
const queryName = operation.name.value
// eslint-disable-next-line no-console
console.warn(
`Deprecation Warning:
Query [${queryName}] used field [${field.name}]
Deprecation reason: [${field.deprecationReason}]`)
return resolve.apply(this, args)
}
}
public visitEnumValue(value) {
value.isDeprecated = true
value.deprecationReason = this.args.reason
}
}
new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
deprecated: DeprecatedDirective,
},
}).listen().then(({ url, }) => {
console.log(` Server ready at ${url}`)
})
这适用于服务器而不是客户端。它应该打印所有需要的信息来追踪客户端上的错误查询。从维护的角度来看,将它放在服务器日志中似乎更可取。