Search code examples
graphqlapolloapollo-client

Emit deprecation warnings with Apollo client


Background

We are working on a fairly large Apollo project. A very simplified version of our api looks like this:

type Operation {
    foo: String
    activity: Activity
}

type Activity {
    bar: String
    # Lots of fields here ...
}

We've realised splitting Operation and Activity does no benefit and adds complexity. We'd like to merge them. But there's a lot of queries that assume this structure in the code base. In order to make the transition gradual we add @deprecated directives:

type Operation {
    foo: String
    bar: String
    activity: Activity @deprecated
}

type Activity {
    bar: String @deprecated(reason: "Use Operation.bar instead")
    # Lots of fields here ...
}

Actual question

Is there some way to highlight those deprecations going forward? Preferably by printing a warning in the browser console when (in the test environment) running a query that uses a deprecated field?


Solution

  • GraphQL schema directives can be customized. So below is a solution that prints a warning on the server (Edit 2023: And here's a plugin that propagates the warning to the client):

    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}`)
    })