
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
graphql-mockfiles-express-middleware
Advanced tools
A GraphQL mockserver based on files and a typeDefinition
A GraphQL mockserver based on files and a typeDefinition
This is a mockserver that can be used as expressjs middleware. It serves mocks based on GraphQL type definitions and a query posted to the server. It can handle aliases.
Be aware that this mock-middleware currently does not support mutations. It could be added in the future
To make sure the mockserver can provide you with content to your queries,
prepare your directory that contains the responses. The names of each response
needs to be ok.json
.
Let's take this GraphQL type definition as an example.
type Query {
posts: [Post]
}
type Post {
title: String
content: String
date: Date
author: Author
}
type Author {
name: String
}
So this typedef would result in the following directory structure.
take notion of the
author
in the second item in the array. The mockfiles-middleware will addauthor
-prop"name": "Albert"
to the first item, and leaveauthor
-prop"name": "John Doe"
in place
mocks/posts/ok.json
[
{
"title": "A great post",
"content": "Lorem ipsum dolor sit amet, [...]",
"dateCreated": "2019-08-13T15:21:37.978Z"
},
{
"title": "Another post",
"content": "Suspendisse lectus ligula, pharetra [...]",
"dateCreated": "2019-08-11T12:21:03.23Z",
"author": {
"name": "John Doe"
}
}
]
mocks/posts/author/ok.json
{
"name": "Albert"
}
Now setup the express server to serve the mockfiles. Take your typeDefs and the path and pass it to the mock-middleware:
const bodyParser = require('body-parser');
const path = require('path');
const app = require('express')();
const {
createGraphQlMockfilesMiddleware,
} = require('graphql-mockfiles-express-middleware');
function main() {
const graphqlTypeDefs = `
type Query {
hello: String
me: Me
}
type Me {
posts: [Post]
}
type Post {
title: String
content: String
dateCreated: String
}`;
app.use(bodyParser.json());
app.use(
'/graphql',
// =====================> Mock Middleware
createGraphQlMockfilesMiddleware(
graphqlTypeDefs,
path.resolve(__dirname, './graphql-mocks')
)
// =====================> End Mock Middleware
);
app.listen(3000, () =>
console.log('graphQlMockServer launched at localhost:3000')
);
}
main();
Now, when executing a query you'd get the results from the mockserver from the files:
{
posts {
title
author {
name
}
}
}
Gives:
{
"data": {
"posts": [
{ "title": "A great post", "author": { "name": "Albert" } },
{ "title": "Another post", "author": { "name": "Albert" } }
]
}
}
Now execute a query by posting to the server using your favorite GraphQL client
The middleware requires you to give typeDefs
and the path
to the mock
directory. Then each request it basically generates a new middleware based on
the response of the paths of your separate queries. Each query creates a set of
paths. E.g. the query above creates the following paths (using
graphql-query-path
)
[
'/posts/'
'/posts/title'
'/posts/author/'
'/posts/author/name'
]
Technically each of these paths can be represented by an ok.json
in the
directory structure. Earlier paths are overriding later paths. So if
/posts/ok.json
already returns a complete object representing what's needed in
the query, it'll not get overriden from sub-paths.
This allows for powerful features like setting author
's specifically in a
Post
in /posts/ok.json
or generally in /posts/author/ok.json
. Have a look
at the differences in the
posts-mocks in ./graphql-mocks/me/posts/ok.json
and the
author-mocks in ./graphql-mocks/me/posts/author/ok.json
FAQs
A GraphQL mockserver based on files and a typeDefinition
We found that graphql-mockfiles-express-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.