---
description:
  Handle Webhooks with GraphQL Subscriptions in your unified schema using GraphQL Mesh. Use PubSub
  implementation to bind webhook paths to specific topics and consume them as subscription root
  fields. You can also generate GraphQL type definitions from sample JSON responses using the JSON
  Schema Handler.
---

import { Callout } from '@theguild/components'

# Handle Webhooks with GraphQL Subscriptions

GraphQL Mesh can consume Webhooks as GraphQL Subscriptions in the unified schema by using the
built-in PubSub implementation

## Add a new Subscription field

You can use `additionalTypeDefs` and `resolveTo` directive to add subscription root fields to your
unified schema.

If you subscribe a pubSub topic with `webhook:EXPECTED_METHOD:EXPECTED_PATH`, the server will
automatically bind that path to that topic.

```yaml filename=".meshrc.yaml"
additionalTypeDefs: |
  # If you don't have Subscription type defined anywhere
  # You have to extend subscription definition
  extend schema {
    subscription: Subscription
  }
  type Subscription {
    todoAdded: Todo @resolveTo(
      pubsubTopic: "webhook:post:todo-added"
    # result: "data.someProp.someOtherProp", # You can get nested fields
    # filterBy: "root.userId === args.userId" # You can filter the payload by `userId` for example
    )
  }
```

We can use existing types from our unified schema, and this root field is subscribed to our specific
`topic` in our PubSub service.

### Use JSON Schema Handler instead

You can also use the JSON Schema handler if you don't want to write an extra GraphQL type
definition. You can generate GraphQL type definitions from sample JSON response;

Just add the following to your existing JSON schema handler configuration in `.meshrc.yml` file;

```yaml filename=".meshrc.yaml"
- type: Subscription
  field: todoAdded
  pubsubTopic: webhook:post:todo-added
  responseSample: ./todo.json
```

<Callout>You can check out our example</Callout>

<iframe
  src="https://codesandbox.io/embed/github/ardatan/graphql-mesh/tree/master/examples/json-schema-subscriptions?fontsize=14&hidenavigation=1&theme=dark&module=%2F.meshrc.yml"
  className="w-full mt-6 h-[500px] rounded-md"
  title="json-schema-subscriptions"
  allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
  sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
/>
