Apollo serverのSchema directivesを使ってdeprecation warningを表示する
Apollo serverでリファクタリングや仕様変更などで、特定のフィールドをdeprecatedにしたい場合があります
Apollo serverの @deprecated
を使えば簡単にdeprecation warningを表示できます
では、やってみましょう
// src/index.js const { ApolloServer, gql } = require("apollo-server"); const typeDefs = gql` type Author { name: String } type Book { title: String author: Author authorName: String } type Query { books: [Book] } `; const resolvers = {}; const server = new ApolloServer({ typeDefs, mocks: true }); server.listen(4001).then(({ url }) => { console.log(`🚀 Server ready at ${url}`); });
BookのauthorName
をdeprecatedにしたいので、deprecation warningを表示したいと思います
type Book { title: String author: Author authorName: String @deprecated( reason: "\`authorName\` is deprecated. Use \`author\` instead." ) }
これだけです!超簡単!
Playgroundで確認してみましょう!
DOCSもみてみましょう
ちゃんと表示されてますね!
schemaDirectivesは自分で定義することもできるので、特定のフィールドを表示するかを認可で切り替えとかもできますね
Happy GraphQL ヘ(^o^)ノ