🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

graphql-mockfiles-express-middleware

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-mockfiles-express-middleware - npm Package Compare versions

Comparing version

to
0.0.2

assets/graphql-client.png

15

graphql-mockfiles-middleware.js

@@ -1,3 +0,1 @@

#!/usr/bin/env node
const fs = require('fs-extra');

@@ -9,3 +7,3 @@ const path = require('path');

const deepmerge = require('deepmerge');
const objectPath = require('object-path');
const { setProps } = require('./setProps');

@@ -42,10 +40,5 @@ /**

objectPath.set(
res,
mock
.split('/')
.filter((x) => x.length)
.join('.'),
mockFile
);
const pathParts = mock.split('/').filter((x) => x.length);
setProps(pathParts, res, mockFile);
}

@@ -52,0 +45,0 @@ }

{
"name": "graphql-mockfiles-express-middleware",
"version": "0.0.1",
"version": "0.0.2",
"description": "A GraphQL mockserver based on files and a typeDefinition",

@@ -9,3 +9,9 @@ "main": "graphql-mockfiles-middleware.js",

},
"keywords": ["GraphQl", "mock", "mockserver", "mockfiles", "express"],
"keywords": [
"GraphQl",
"mock",
"mockserver",
"mockfiles",
"express"
],
"author": "Albert Groothedde <alber70g@gmail.com>",

@@ -15,8 +21,11 @@ "license": "ISC",

"deepmerge": "^4.0.0",
"fs-extra": "^8.1.0",
"graphql": "^14.4.2",
"graphql-query-path": "^1.1.2",
"graphql-tools": "^4.0.5",
"object-path": "^0.11.4"
"graphql-tools": "^4.0.5"
},
"devDependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"fs-extra": "^8.1.0"
}
}

@@ -5,2 +5,12 @@ # GraphQL MockFiles Server

- [GraphQL MockFiles Server](#graphql-mockfiles-server)
- [Getting started](#getting-started)
- [Prepare your mockfiles](#prepare-your-mockfiles)
- [Setup the express server](#setup-the-express-server)
- [How does it work](#how-does-it-work)
- [Further work and new features](#further-work-and-new-features)
- [Changelog](#changelog)
- [0.0.2](#002)
- [0.0.1](#001)
## Getting started

@@ -11,7 +21,18 @@

serves mocks based on
[GraphQL typeDef](./graphql-mocks/graphql-mock-schema.graphql) and a query
posted to the server.
[GraphQL type definitions](./graphql-mocks/graphql-mock-schema.graphql) and a
query posted to the server. It can handle
[aliases](https://graphql.org/learn/queries/#aliases).
Make your GraphQL typeDef:
> Be aware that this mock-middleware currently does not support
> [mutations](https://graphql.org/learn/queries/#mutations). It could be added
> in the future
### Prepare your mockfiles
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.
```graphql

@@ -25,7 +46,46 @@ type Query {

date: Date
author: Author
}
type Author {
name: String
}
```
And your express server:
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 add `author`-prop `"name": "Albert"` to the first
> item, and leave `author`-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"
}
```
### Setup the express server
Now setup the express server to serve the mockfiles. Take your typeDefs and the
path and pass it to the mock-middleware:
```js

@@ -60,2 +120,3 @@ const bodyParser = require('body-parser');

'/graphql',
// =====================> Mock Middleware
createGraphQlMockfilesMiddleware(

@@ -65,2 +126,3 @@ graphqlTypeDefs,

)
// =====================> End Mock Middleware
);

@@ -74,3 +136,75 @@ app.listen(3000, () =>

Now, when executing a query you'd get the results from the mockserver from the
files:
```graphql
{
posts {
title
author {
name
}
}
}
```
Gives:
```json
{
"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](https://altair.sirmuel.design/)
![GraphQL Mockserver Client](./assets/graphql-client.png)
# How does it work
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`)
```json
[
'/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](./graphql-mocks/me/posts/ok.json)
and the
[author-mocks in ./graphql-mocks/me/posts/author/ok.json](./graphql-mocks/me/posts/author/ok.json)
# Further work and new features
- [] make the mockfiles-middleware work for Mutations
# Changelog
## 0.0.2
- Fixed an issue where values that should be in arrays weren't put in array's
## 0.0.1
- Initial version