Apollo Prophecy
You shall fail... successfully
📟 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:
Apollo Prophecy will generate the following Errors.ts
... ...
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.
...; ;...
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.
; ...
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.
; ...
React example:
; ...
Contributing
🍴 fork develop ⤵
👨💻 Code ⤵
🛠 Test ⤵
📩 Pull Request ⤵
💥💥💥
TODO
- See #2: Add support for third party libraries errors like apollo-errors
Running tests locally:
npm test
yarn test