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

mock-relay-server

v0.1.2

Published

a simple library for generating functions to call in relay server mocks

Downloads

8

Readme

mock-relay-server

A tool for using simple data input to mock a GraphQL schema in a way that allow for full CRUD access

Example

import { getMockedResolvers } from 'mock-relay-server'
import { makeExecutableSchema } from 'graphql-tools'

// Your full schema in GraphQL type language
const typeDefs = `
  type User {
      id : ID!
      name: String
  }
  ...
`

// The data to initialize your mock database
const users = [
    {
        id: '1',
        name: 'User One',
    },
    {
        id: '1',
        name: 'User Two',
    },
]

const mocks = {
    User: {
        // connections are created based on any types that are provided a
        // a data key, in this case only user
        // this is your interface with the mock database
        data: users,
    },
    Query: {
        // this resolver functions similarly to resolvers in graphql-tools
        // with the only additional wrinkle that you're provided connections
        // as a way to interface with the database
        resolver: connections => ({
            getUser: async (query, { userId }) => connections.User.getNode(userId),
            getUsers: connections.User.paginate,
        }),
    },
    Mutation: {
        resolver: connections => ({
            updateUser: connections.Widget.update,
            addUser: connections.Widget.create,
            deleteUser: connections.Widget.delete,
        }),
    },
}

const resolvers = getMockedResolvers(mocks)

// at this point, you have a fully valid graphQL schema that can be
// be used to locally run queries
const schema = makeExecutableSchema({ typeDefs: [typeDefs], resolvers })

Creating mocks

The mocks in mock-relay-server are very similar to the resolver map from graphql-tools with a couple of key differences. First, you can provide an optional data key which gives you full CRUD on the array of items provided. Second, instead of providing resolver directly, it's provided on a resolver key that returns a function that provides you the initialized connections and returns the final resolver map.

Connections API

Connections are used to make it easy to implement resolvers. Most of the time, you should be able to use one of the helpers on a connection with out needing any configuration.

create / update / delete

These all should all work out of the box with very little customization. The only requirement is that the mutation that they're mocking only accepts an input object as a variable

getNode

Accepts a global id(the combination of the id and the nodes type) and returns the associated node

paginate

Accepts standard relay compliant input variables and returns the expected relay compliant connection format