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

@ember-graphql-client/mock

v0.5.2

Published

A package to allow mocking the GraphQL layer from @ember-graphql-client/client.

Downloads

68

Readme

@ember-graphql-client/mock

An addon to mock a GraphQL API for tests, demos or local development.

Compatibility

  • Ember.js v3.24 or above
  • Ember CLI v3.24 or above
  • Node.js v12 or above

Installation

ember install ember-graphql-client

Conditional usage

In most cases, you will only want to enable mocked GraphQL under certain situations, e.g. when running tests. In other cases, you'll want to avoid to include this addon, to not include the graphql tools necessary to make mocking work.

The easiest way to achieve this is by manually disabling this addon in your ember-cli-build.js for those cases. For example:

// ember-cli-build.js
let env = process.env.EMBER_ENV || 'development';
let isProduction = env === 'production';

let app = new EmberApp(defaults, {
  addons: {
    blacklist: isProduction ? ['@ember-graphql-client/mock'] : [],
  },
});

Usage

Usage in tests

import { mockGraphQLForTests } from '@ember-graphql-client/mock/test-support/helpers';
import schema from 'dummy/gql/schema.graphql';

module('Unit | mock-graphql-request-client', function (hooks) {
  setupTest(hooks);

  module('basic', function (hooks) {
    mockGraphQLForTests(hooks, schema, {
      Query: {
        post() {
          return {
            id: '99',
            title: 'test title',
            body: 'test body',
          };
        },
      },

      Mutation: {
        createPost() {
          return {
            id: '99',
            title: 'test title',
            body: 'test body',
          };
        },
      },
    });
  });
});

The third argument to mockGraphQLForTests is a resolver object.

The schema should be a full GraphQL schema.

Alternatively, you can also use the following code, if you want to setup the mock inside of a function:

import { setupMockGraphQLRequestClient } from '@ember-graphql-client/mock/test-support/helpers';

let graphql = this.owner.lookup('service:graphql');
setupMockGraphQLRequestClient(graphql, schema, resolvers);

Usage in application code

You can also setup a mocked GraphQL client for e.g. local development or demoing. This works similar as mocking for tests:

import { getMockGraphQLRequestClient } from '@ember-graphql-client/mock/mock-graphql-request-client';
import resolvers from 'my-app/mocked-graphql-resolvers';
import schema from 'my-app/gql/schema.graphql';

// e.g. in an instance initializer
export function initialize(appInstance) {
  let graphql = appInstance.lookup('service:graphql');
  let client = getMockGraphQLRequestClient(schema, resolvers);
  graphql.client = client;
}

export default {
  initialize,
};

GraphQL Resolvers

This addon uses standard GraphQL execution resolvers.

The simplest form of a resolver takes a Query and Mutation key that contains an object with a function for each query/mutation.

The function has the following signature:

fieldName: (parent, args, context, info) => data;

Often, you might not have/need the parent. The args are the inputs passed in.

An example for a mutation using the inputs would be:

let resolvers = {
  Mutation: {
    createPost: (_, args) => {
      let { id, title } = args;

      return {
        id,
        title,
        creationDate: new Date(),
      };
    },
  },
};

You can find details on how a resolver object works in the Apollo docs.

Mocking errors

You can also trigger errors from GraphQL. Note that schema validation will happen automatically and throw appropriate errors.

You can either just throw an error from a resolver:

let resolvers = {
  Query: {
    post() {
      throw new Error('could not fetch post');
    },
  },
};

Which will result in the following errors payload:

[
  {
    "locations": [],
    "message": "could not fetch post",
    "name": "GraphQLError",
    "path": ["post"]
  }
]

If you need more control, you can also generate a more detailed error:

import { mockGraphQLError } from '@ember-graphql-client/mock/mock-graphql-error';

let resolvers = {
  Query: {
    post() {
      throw mockGraphQLError({
        message: 'test extended error',
        extensions: { code: 'RESOURCE_NOT_FOUND' },
      });
    },
  },
};

Which will result in the following errors payload:

[
  {
    "locations": [],
    "message": "test extended error",
    "name": "GraphQLError",
    "path": ["post"],
    "extensions": {
      "code": "RESOURCE_NOT_FOUND"
    }
  }
]

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.