DEV Community

Cover image for Deprecating AppSync Queries
Ben Force
Ben Force

Posted on • Originally published at justwriteapps.com on

3 2

Deprecating AppSync Queries

How do you handle the awkward transition phase when you're replacing a query in the backend, but the frontend hasn't been updated to use the new query? The answer is @deprecated!

Well, the answer isn't deprecated itself 🤦. You can annotate the old query with @deprecated and the schema documentation will be updated to show the query as deprecated and show the provided reason.

type Query {
    oldQuery(input: OldInputType): Result! @deprecated(reason: "Use the newQuery")
    newQuery(input: NewInputType): Result!
}

Enter fullscreen mode Exit fullscreen mode

Now when you view the schema documentation, you will get a deprecation notice.

Deprecation Notice

The problem with stopping here is there's no indication of this returned to the client.

{
  "data": {
    "oldQuery": {
      "value": "Old query result"
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

If you want the client to get this deprecation notice, you can add the following statement to the response resolver template.

$util.appendError("oldQuery is deprecated, use newQuery instead", "DEPRECATED")

Enter fullscreen mode Exit fullscreen mode

Now when you run the query, you get an error in the results.

{
  "data": {
    "oldQuery": {
      "value": "Old query result"
    }
  },
  "errors": [
    {
      "path": [
        "oldQuery"
      ],
      "data": null,
      "errorType": "DEPRECATED",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "oldQuery is deprecated, use newQuery instead"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

If you want to experiment with deprecation, or appsync in general, I've created a sample project.

https://github.com/just-write-apps-blog/deprecated-query

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay