This plugin for Hasura DDN (Distributed Data Network) allows you to add RESTified GraphQL endpoints to the DDN supergraph. It transforms GraphQL queries into REST-like endpoints, making it easier to integrate with systems that prefer REST APIs.
- Transform GraphQL queries into REST-like endpoints
- Configurable endpoint mapping
- Authentication support
- Variable extraction from URL parameters, query strings, and request bodies
- OpenTelemetry integration for tracing
- The plugin starts a server that listens for incoming requests.
- When a request is received, it checks if it matches any configured RESTified endpoints.
- If a match is found, the plugin:
- Extracts variables from the request (URL parameters, query string, body)
- Executes the corresponding GraphQL query with the extracted variables
- Returns the GraphQL response as a REST-style JSON response
Configure the plugin in src/config.ts
:
graphqlServer
: GraphQL server settings (URL, headers)headers
: Authentication headersrestifiedEndpoints
: Array of RESTified endpoint configurations
Example configuration:
export const Config = {
graphqlServer: {
url: "http://localhost:3000/graphql",
headers: {
additional: {
"Content-Type": "application/json",
},
forward: ["X-Hasura-Role"],
},
},
headers: {
"hasura-m-auth": "zZkhKqFjqXR4g5MZCsJUZCcoPyZ",
},
restifiedEndpoints: [
{
path: "/v1/restified/:offset",
method: "GET",
query: `
query MyQuery($limit: Int = 10, $offset: Int = 10) {
Album(limit: $limit, offset: $offset) {
Title
}
}
`,
},
// Add more RESTified endpoints here
],
};
Note: We are using Cloudflare wrangler for local development and deployment. However, you can use any other tool for the same. You will have to modify the files accordingly.
To run the plugin locally, you can use the following steps:
-
Install wrangler:
npm install -g wrangler
-
Clone this repository:
git clone https://github.com/your-org/engine-plugin-restified-endpoint.git cd engine-plugin-restified-endpoint
-
Install dependencies:
npm install
-
Start the local development server:
npm start
The above command will start a local server that listens for incoming requests. The server runs on port 8787 by default. The URL of the local server will be displayed in the terminal.
For cloud deployment, you can use the following steps in addition to the local development steps:
-
Create an account on Cloudflare.
-
Login to Cloudflare:
wrangler login
-
Deploy to Cloudflare:
npm run deploy
The above command should deploy the RESTified endpoints plugin (as a lambda) using Cloudflare workers. The URL of the deployed plugin will be displayed in the terminal.
Update the metadata to add the plugin-related config (in global subgraph). Also, add the env vars for the URL of local dev and cloud deployment:
kind: LifecyclePluginHook
version: v1
definition:
name: restified_endpoints
url:
valueFromEnv: RESTIFIED_ENDPOINTS_URL
pre: route
config:
match: "/v1"
request:
headers:
additional:
hasura-m-auth:
value: <your-secret-token>
forward:
- Authorization
- X-Hasura-Role
session: {}
rawRequest:
path: {}
query: {}
method: {}
Build DDN supergraph:
ddn supergraph build create
Note: For end-to-end tracing, you would have to update the wrangler.toml
file to add the Hasura PAT in OTEL_EXPORTER_PAT
var.
To add new RESTified endpoints, update the restifiedEndpoints
array in src/config.ts
. For example:
restifiedEndpoints: [
{
path: "/v1/rest/users/:id",
method: "GET",
query: `
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`,
},
{
path: "/v1/rest/posts",
method: "GET",
query: `
mutation CreatePost($title: String!, $content: String!) {
createPost(input: { title: $title, content: $content }) {
id
title
}
}
`,
},
];
After adding new endpoints, redeploy the plugin for the changes to take effect.
- The plugin currently supports only GET methods. Support for other HTTP methods will be added in the future.
- Currently, the plugin supports basic variable extraction. More complex scenarios might require additional implementation.
- OpenAPI Spec documentation generation is not yet implemented.
- Rate limiting is not currently supported within the plugin.
Contributions are welcome! Please feel free to submit a Pull Request.