
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
rest-graphql
Advanced tools
Middleware for Express to adapt REST requests to GraphQL queries
rest-graphql
provides middleware that lets you define mappers from REST requests to graphql queries that fetch the same data, letting you normalize all client queries into something your GraphQL server can handle.
Install the package
npm install --save rest-graphql
Let's say you're building out a new profile page and have defined a GraphQL schema for it.
You can fetch the necessary data via:
query PresidentQuery {
presidents {
name
}
}
Create a new RestAdapter and add the middleware to your express server:
import RestAdapter from 'rest-graphql';
/* Build a new adapter
*
* isError - Detect is the graphql query has failed.
* transformError - Transform the failed query response into a RestAdapterResponse.
*
*/
const adapter = new RestAdapter({
isError: (response) => !!response.errors,
transformError: (response) => response.errors[0].__http_secret__,
});
/* Add endpoints to the adapter:
*
* path - The REST endpoint.
* getQuery - Function returning a Graphql query as a String.
* transformSuccess - Transform the successful query response into a RestAdapterResponse.
*/
adapter.addEndpoint({
path: '/presidents',
getQuery: (request) => PRESIDENTS_QUERY,
transformSuccess: response => ({ status: 200, body: response.presidents }),
});
const graphql = express();
graphql.use(adapter.app);
graphql.use('/graphql', graphqlExpress(/* ... */));
Which would result in:
1. HTTP Request:
GET https://api.test.com/presidents
2. Graphql Query:
query PresidentQuery {
presidents {
name
}
}
3. Graphql Response:
{
presidents: [
{ name: "Jacques Chirac" },
{ name: "George Washington" }
]
}
4. HTTP Response:
["Jacques Chirac", "George Washington"]
To transform Graphql query errors into the REST responses we recommend using something similar to apollo-server formatError options. In the above example we format the HTTP errors like the following:
{
"presidents": null,
"errors": [
{
"message": "Internal server error",
"__http_secret__": {
"status": 500,
"body": {
"message": "Internal server error"
}
},
},
]
}
FAQs
Middleware for Express to adapt REST requests to GraphQL queries
The npm package rest-graphql receives a total of 5 weekly downloads. As such, rest-graphql popularity was classified as not popular.
We found that rest-graphql demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.