Giter VIP home page Giter VIP logo

apollo-prophecy's Introduction

Apollo Prophecy

๐Ÿ‘๐Ÿ“Ÿ๐Ÿ‘
You shall fail... successfully
Command tool to generate errors files for your Appolo Server and Client

๐Ÿ“Ÿ 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

Install with npm:

npm install -g apollo-prophecy

Install with yarn:

yarn global add 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

โš ๏ธ This Project is Only compatible with Apollo-Server v2

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

โœŠGrab an issue โฌ
๐Ÿดfork develop โฌ
๐Ÿ‘จโ€๐Ÿ’ปCode โฌ
๐Ÿ› Test โฌ
๐Ÿ“ฉPull Request โฌ

๐Ÿ’ฅ๐Ÿ’ฅ๐Ÿ’ฅ

TODO

  • See #2: Add support for third-party errors libraries like apollo-errors good first issue
  • See #12: Add errors.json file as ask command argument good first issue
  • See #16: Add language specific code generation Java/Kotlin/Swift/Javascript
    • See #13: Java/Kotlin good first issue
    • See #14: Swift good first issue
    • See #15: Javascript good first issue

Run tests

npm test
yarn test

apollo-prophecy's People

Contributors

dependabot[bot] avatar theglenn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

apollo-prophecy's Issues

Client side ask command

Add a new command ask or fetch that works the following way

apollo-prophecy ask http://localhost:3001/ --type appErrors
  • First param is the URL of a running GraphQL server
  • --type or -t param is the query type that exposes all errors, if not supplied the command will look for the errors type

Rename "errorHere()"

The fluent API seems to try to mimic natural language, e.g.

 isThis(error)
 .UserNotFoundError(() => ...)
 .NotAProphetError(() => ...)

errorHere(*error*) uses the following construct:

 if(errorHere(error).isNotAProphetError){
   // Do something
 } else if(errorHere(error).isProphetNotFoundWithId){
   // Do something else
 }

would

thisError(error).isNotAProphetError

or

checkError(error).isNotAProphetError

or

queryError(error).isNotAProphetError

be slightly nicer to read?

Add third party libraries support

The idea is to allow user to replace something like the following code:

export class UserNotFoundError extends PythianError {
  constructor(properties?: Record<string, any>) {
    super("UserNotFoundError", "No user found", "USER_NOT_FOUND", properties);
  }
}

With a custom definition like below using a library like apollo-errors:

export const UserNotFoundError = createError('UserNotFoundError', {
  message: "No user found"
});

Not sure how to implement this suggestion greatly welcomed.

Make framework-agnostic?

Move away from the currently Apollo-Only architecture and expand the scope of the project so that it can be used with any GraphQL server and client implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.