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.
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;
- type: Subscription
field: todoAdded
pubsubTopic: webhook:post:todo-added
responseSample: ./todo.json