npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-mockfiles-express-middleware

v0.0.3

Published

A GraphQL mockserver based on files and a typeDefinition

Downloads

9

Readme

GraphQL MockFiles Server

A GraphQL mockserver based on files and a typeDefinition

Getting started

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

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.

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 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:

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

GraphQL Mockserver Client

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)

[
  "/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

Further work and new features

  • [ ] make the mockfiles-middleware work for Mutations

Changelog

0.0.3

  • Update README.md

0.0.2

  • Fixed an issue where values that should be in arrays weren't put in array's

0.0.1

  • Initial version