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

apollo-prophecy

v0.2.3-0

Published

Generate Spec compliant and shareable errors for Apollo Client/Server from JSON or Graphql Schema

Downloads

8

Readme

📟 Features

  • Generate Server-side throwable errors in your resolvers like throw new NotAProphetError()
  • Expose machine readable graphql errors through your api documentation
  • Generate Client-side Apollo errors consumable like errorHere(error).isNotAProphetError ?

📋 Table of Contents

Installation

First, install apollo-prophecy globaly

npm install -g apollo-prophecy

Usage

Usage: apollo-prophecy [command]

Commands:
  apollo-prophecy generate <json file> [--out]
  apollo-prophecy ask <graphql endpoint> [--type] [--out]

Options:
  -h, --help     Show help                                             [boolean]
  -v, --version  Display version number                                [boolean]

Server

generate command

Creates Error.ts from a JSON input file. Using --out param you can change the name and location.

apollo-prophecy generate errors.json

Input file entries should at least contains the keys message and code.

For example given the following errors.json as input:

{
  "AuthenticationRequiredError": {
    "message": "You must be logged in to do this",
    "code": "AUTH_REQUIRED"
  },
  "UserNotFoundError": {
    "message": "No user found",
    "code": "USER_NOT_FOUND"
  },
}

Apollo Prophecy will generate the following Errors.ts

...
export class AuthenticationRequiredError extends ProphecyError {
  constructor(properties?: Record<string, any>) {
    super("AuthenticationRequiredError", "You must be logged in to do this","AUTH_REQUIRED", properties);
  }
}
  
export class UserNotFoundError extends ProphecyError {
  constructor(properties?: Record<string, any>) {
    super("UserNotFoundError", "No user found", "USER_NOT_FOUND", properties);
  }
}
...

Now you can use it the following way throw new UserNotFoundError() in your resolvers.

apollo-prophecy also exposes a definitions object and a graphql type definition named PropheticError so that you can expose all your errors descriptions through a resolver, see Client.

...
export const definitions = [{
    "name": "AuthenticationRequiredError"
    "message": "You must be logged in to do this",
    "extensions": {
      "code": "AUTH_REQUIRED"
    }
  }, {
    "name": "UserNotFoundError"
    "message": "No user found",
    "extensions": {
      "code": "USER_NOT_FOUND"
    }
  }
}];

export const errorType = `
  type PropheticErrorExtensions {
    code: String
  }

  type PropheticError {
    message: String?
    extensions: PropheticErrorExtensions
  }
`;
...

Client

ask command

Queries the errors field on the specified graphql endpoint and creates an Errors.ts file containing helpers with check methods (see Helpers) for all the errors exposed through the server api documentation.

apollo-prophecy ask http://localhost:3000/graphql

Helpers

In order to easily handle erros with Apollo-Client, the generated Errors.ts exposes two helpers methods errorHere and isThis, both methods takes one paramater of type ApolloError or GraphQLError.

errorHere() function

errorHere returns an object that has a property named after each errors. You can perform a simple boolean check on the error argument by calling the approiate key.

import { errorHere } from `./_generated/Errors.ts`;

...(error) => {
  if(errorHere(error).isUserNotFoundError){
    // Do something
  } else if(errorHere(error).isNotAProphetError){
    // Do something else
  }
}
isThis() function

isThis returns an object that has a handler method for each errors. It perfoms a simple check on the error argument, if the it succeed the corresponding handler is called otherwise nothing happens.

Note: Handlers can return a values.

import { isThis } from `./_generated/Errors.ts`;

...(error) => {
  isThis(error)
  .UserNotFoundError(() => ...)
  .NotAProphetError(() => ...)
  .handle()
}

React example:

import { isThis } from `./_generated/Errors.ts`;

...(error) => {
  return <p style={{color: '#FF495C'}}>
    {
      isThis(error)
      .UserNotFoundError(() => <span>Could not find a user with tha name</span>)
      .NotAProphetError(() => <span>Only Prophets can perfom this kind of actions...</span>)
      .handle();
    }
  </p>
}

Contributing

Build status

TODO

Running tests locally:

npm test
yarn test