项目作者: theGlenn

项目描述 :
🔮 GraphQL error management made Easy, generate custom machine-readable errors for Apollo Client/Server from the CLI
高级语言: TypeScript
项目地址: git://github.com/theGlenn/apollo-prophecy.git
创建时间: 2018-06-03T22:13:08Z
项目社区:https://github.com/theGlenn/apollo-prophecy

开源协议:MIT License

下载


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:

  1. npm install -g apollo-prophecy

Install with yarn:

  1. yarn global add apollo-prophecy

Usage

  1. Usage: apollo-prophecy [command]
  2. Commands:
  3. apollo-prophecy generate <json file> [--out]
  4. apollo-prophecy ask <graphql endpoint> [--type] [--out]
  5. Options:
  6. -h, --help Show help [boolean]
  7. -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.

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

  1. {
  2. "AuthenticationRequiredError": {
  3. "message": "You must be logged in to do this",
  4. "code": "AUTH_REQUIRED"
  5. },
  6. "UserNotFoundError": {
  7. "message": "No user found",
  8. "code": "USER_NOT_FOUND"
  9. },
  10. }

Apollo Prophecy will generate the following Errors.ts

  1. ...
  2. export class AuthenticationRequiredError extends ProphecyError {
  3. constructor(properties?: Record<string, any>) {
  4. super("AuthenticationRequiredError", "You must be logged in to do this","AUTH_REQUIRED", properties);
  5. }
  6. }
  7. export class UserNotFoundError extends ProphecyError {
  8. constructor(properties?: Record<string, any>) {
  9. super("UserNotFoundError", "No user found", "USER_NOT_FOUND", properties);
  10. }
  11. }
  12. ...

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.

  1. ...
  2. export const definitions = [{
  3. "name": "AuthenticationRequiredError"
  4. "message": "You must be logged in to do this",
  5. "extensions": {
  6. "code": "AUTH_REQUIRED"
  7. }
  8. }, {
  9. "name": "UserNotFoundError"
  10. "message": "No user found",
  11. "extensions": {
  12. "code": "USER_NOT_FOUND"
  13. }
  14. }
  15. }];
  16. export const errorType = `
  17. type PropheticErrorExtensions {
  18. code: String
  19. }
  20. type PropheticError {
  21. message: String?
  22. extensions: PropheticErrorExtensions
  23. }
  24. `;
  25. ...

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.

  1. 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.

  1. import { errorHere } from `./_generated/Errors.ts`;
  2. ...(error) => {
  3. if(errorHere(error).isUserNotFoundError){
  4. // Do something
  5. } else if(errorHere(error).isNotAProphetError){
  6. // Do something else
  7. }
  8. }
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.

  1. import { isThis } from `./_generated/Errors.ts`;
  2. ...(error) => {
  3. isThis(error)
  4. .UserNotFoundError(() => ...)
  5. .NotAProphetError(() => ...)
  6. .handle()
  7. }

React example:

  1. import { isThis } from `./_generated/Errors.ts`;
  2. ...(error) => {
  3. return <p style={{color: '#FF495C'}}>
  4. {
  5. isThis(error)
  6. .UserNotFoundError(() => <span>Could not find a user with tha name</span>)
  7. .NotAProphetError(() => <span>Only Prophets can perfom this kind of actions...</span>)
  8. .handle();
  9. }
  10. </p>
  11. }

Contributing

Build status








Grab an issue
🍴fork develop
👨‍💻Code
🛠Test
📩Pull Request

💥💥💥


TODO

Run tests

  1. npm test
  1. yarn test