Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.41.x.
What you'll learn
  • how to add a custom route
  • how to add a custom route handler

Introduction
anchor

Starting with the Webiny version 5.31.0, when we added the Fastifyexternal link, users can quite easily add new routes to our existing Lambdas.

Users can add as many routes as they want, with possibility for them to be a POST, GET, OPTIONS, PUT, PATH, DELETE and HEAD method routes.

Adding a Route to the API Gateway
anchor

First, we need to add the route to the API Gateway. Without this part, the route will not be known by the API Gateway and users will get a 404 response.

apps/api/webiny.application.ts

After these changes, feel free to deploy the api part of your project to test if new route is working. Use your favorite HTTP client to test it out.

Dynamic API Gateway Route
anchor

To have a API Gateway route which accepts dynamic parameters, you must specify the parameter in the path, in our case the {id}.

apps/api/webiny.application.ts

Adding a Route to the Handler
anchor

After you added the route to the API Gateway, you can move onto creating a route in our existing handler.

apps/api/graphql/src/index.ts

Dynamic Webiny Handler Route
anchor

To have a Fastify route which accepts dynamic parameters, you must specify the parameter in the path, in our case the :id.

apps/api/graphql/src/index.ts

In the createApiGatewayRoute method callback, there are multiple properties available:

  • onPost - register a POST method route
  • onOptions - register an OPTIONS method route
  • onDelete - register a DELETE method route
  • onPatch - register a PATCH method route
  • onGet - register a GET method route
  • onHead - register a HEAD method route
  • onPut - register a PUT method route
  • onAll - register a route which will catch all the existing methods
  • context - our Webiny internal context with all our applications attached to it

The requestexternal link and replyexternal link objects are the original ones from the Fastify, so you can look into their documentation about what they contain and what you can use.

Why only the graphql handler example?

We will eventually remove the headlessCMS Lambda and its handler, so do not add routes there. Everything you might need from the system is available in the GraphQL Lambda handler.