Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80ae047

Browse files
first commit
0 parents commit 80ae047

File tree

17 files changed

+1402
-0
lines changed

17 files changed

+1402
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
.npmignore

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###The MIT License
2+
3+
Copyright (c) 2018 EasyGraphQL
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎README.MD‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<h1 align="center">
2+
<img src="https://cdn.rawgit.com/EasyGraphQL/easygraphql-mock/baab331c/EasyGraphQL.png" alt="EasyGraphQL " width="350">
3+
<br>
4+
EasyGraphQL Format Error
5+
<br>
6+
<br>
7+
</h1>
8+
9+
Easy GraphQL Format Error is a node library used to handle the errors and return them with the respective
10+
status code.
11+
12+
## Installation
13+
```bash
14+
$ npm install easygraphql-format-error --save
15+
```
16+
17+
## Usage
18+
To get started with the format error, you might need to follow the next steps:
19+
20+
### Basic
21+
```js
22+
// App.js
23+
const FormatError = require('easygraphql-format-error')
24+
25+
const formatError = new FormatError()
26+
// pass the errorName on the context
27+
const errorName = formatError.errorName
28+
29+
app.use('/graphql', (req, res) => {
30+
graphqlHTTP({
31+
schema,
32+
rootValue: root,
33+
graphiql: true,
34+
context: { errorName },
35+
formatError: (err) => {
36+
return formatError.getError(err)
37+
}
38+
})(req, res)
39+
})
40+
41+
// Resolver
42+
userInformation: ({ isLoggedIn }, { errorName }) => {
43+
if (!isLoggedIn) {
44+
throw new Error(errorName.UNAUTHORIZED)
45+
}
46+
47+
return 'My username'
48+
}
49+
50+
// If the user is not loggedIn the response will be
51+
{
52+
"errors": [
53+
{
54+
"message": "Unauthorized",
55+
"statusCode": 401
56+
}
57+
],
58+
"data": null
59+
}
60+
```
61+
62+
### With Custom errors
63+
You can pass custom error and access those errors from the resolver, calling `errorName.YOUR_ERROR_NAME`
64+
65+
```js
66+
// App.js
67+
const FormatError = require('easygraphql-format-error')
68+
69+
const formatError = new FormatError([{
70+
name: 'INVALID_EMAIL',
71+
message: 'The email is not valid',
72+
statusCode: '400'
73+
}])
74+
// pass the errorName on the context
75+
const errorName = formatError.errorName
76+
77+
app.use('/graphql', (req, res) => {
78+
graphqlHTTP({
79+
schema,
80+
rootValue: root,
81+
graphiql: true,
82+
context: { errorName },
83+
formatError: (err) => {
84+
return formatError.getError(err)
85+
}
86+
})(req, res)
87+
})
88+
89+
// Resolver
90+
findUserByEmail: ({ email }, { errorName }) => {
91+
const re = /\S+@\S+\.\S+/;
92+
if (!re.test(email)) {
93+
throw new Error(errorName.INVALID_EMAIL)
94+
}
95+
96+
return email
97+
}
98+
99+
// If the email is not valid the response will be
100+
{
101+
"errors": [
102+
{
103+
"message": "The email is not valid",
104+
"statusCode": "400"
105+
}
106+
],
107+
"data": null
108+
}
109+
```
110+
111+
## Demo
112+
Here is an [Example](https://github.com/EasyGraphQL/easygraphql-format-error/tree/master/example) that can be useful!
113+
114+
# License
115+
### The MIT License
116+
117+
Copyright (c) 2018 EasyGraphQL
118+
119+
Permission is hereby granted, free of charge, to any person obtaining a copy
120+
of this software and associated documentation files (the "Software"), to deal
121+
in the Software without restriction, including without limitation the rights
122+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
123+
copies of the Software, and to permit persons to whom the Software is
124+
furnished to do so, subject to the following conditions:
125+
126+
The above copyright notice and this permission notice shall be included in
127+
all copies or substantial portions of the Software.
128+
129+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
130+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
132+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
133+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
134+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
135+
THE SOFTWARE.

‎example/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

‎example/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###The MIT License
2+
3+
Copyright (c) 2018 EasyGraphQL
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎example/README.MD‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h1 align="center">
2+
<img src="https://cdn.rawgit.com/EasyGraphQL/easygraphql-mock/baab331c/EasyGraphQL.png" alt="EasyGraphQL Mock " width="350">
3+
<br>
4+
EasyGraphQL Format Error Example
5+
<br>
6+
<br>
7+
</h1>
8+
9+
This is an example of `easygraphql-format-error`.
10+
11+
To run the demo yourself, git clone the repo and then
12+
```bash
13+
$ cd example
14+
$ npm install
15+
$ npm start
16+
```
17+
Point your browser at http://localhost:7000 to see the demo.
18+
19+
# License
20+
### The MIT License
21+
22+
Copyright (c) 2018 EasyGraphQL
23+
24+
Permission is hereby granted, free of charge, to any person obtaining a copy
25+
of this software and associated documentation files (the "Software"), to deal
26+
in the Software without restriction, including without limitation the rights
27+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
copies of the Software, and to permit persons to whom the Software is
29+
furnished to do so, subject to the following conditions:
30+
31+
The above copyright notice and this permission notice shall be included in
32+
all copies or substantial portions of the Software.
33+
34+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
THE SOFTWARE.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /