使用GraphQL简写语法将字段标记为已弃用

9
我正在使用GraphQL缩写来指定模式中的类型,例如:
type Car {
  id: ID!
  make: String
  model: String
  description: String
}

使用这个缩写,有没有一种方法可以将字段标记为已弃用?比如我想把“描述”标记为已弃用。以下是我猜测如何实现此操作的代码:
type Car {
  id: ID!
  make: String
  model: String
  @deprecated
  description: String
}

但是没有成功。在GraphQL简写中,字段弃用是否可行?
谢谢!
2个回答

13

是的,您可以像这样将字段标记为已弃用:

type Car {
  id: ID!
  make: String
  model: String
  description: String @deprecated(reason: "Field is deprecated!")
}

太棒了!我需要使用特殊版本的工具来利用它吗?我正在使用graphql-tools中的makeExecutableSchema构建我的模式。var executableSchema = makeExecutableSchema({ typeDefs: schema, resolvers: resolvers }); - Ranj
看起来内省工具或GraphQL IntelliJ插件无法识别它。不确定是否有任何更改。 - 500865
2
它在GraphQL规范中有记录,链接在这里:https://spec.graphql.org/June2018/#sec--deprecated - Purplejacket

2
您可以这样弃用:

您可以像这样进行弃用

directive @deprecated(
  reason: String = "No longer supported"
) on FIELD_DEFINITION | ENUM_VALUE

type ExampleType {
  newField: String
  oldField: String @deprecated(reason: "Use `newField`.")
}

如需更多信息,请参考Apollo Server 文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接