Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit cdfc054

Browse files
committed
fix issues with binary and empty responses, some cleanups
1 parent e5f5812 commit cdfc054

File tree

7 files changed

+76
-33
lines changed

7 files changed

+76
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
- Set User-Agent header for all requests #6
66
- Improved exploration #4
77
- Dependency updates
8-
- Fix binary command
8+
- Fix binary
9+
- Fix issue with empty response bodies
10+
- Add examples
911

1012
## v0.2.0
1113

examples/anilist.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TestOptionsInput } from '../src/options';
2+
3+
export function gqlm(): TestOptionsInput {
4+
return {
5+
url: 'https://graphql.anilist.co',
6+
count: 50,
7+
data: {
8+
name: 'Stryk3r117'
9+
}
10+
};
11+
}

examples/github.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TestOptionsInput } from '../src/options';
2+
3+
export function gqlm(): TestOptionsInput {
4+
return {
5+
url: 'https://api.github.com/graphql',
6+
requestOptions: {
7+
url: 'ignored',
8+
headers: {
9+
authorization: 'Bearer <token>'
10+
}
11+
}
12+
};
13+
}

examples/metaphysics.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TestOptionsInput } from '../src/options';
2+
3+
export function gqlm(): TestOptionsInput {
4+
return {
5+
url: 'https://metaphysics-production.artsy.net/',
6+
count: 50,
7+
timeout: 5000
8+
};
9+
}

src/console.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export class ConsoleReporter {
110110
this.print(
111111
chalk.red(
112112
indent(
113-
result.requestError.message ||
114-
result.requestError.stack ||
113+
result.requestError.stack ||
114+
result.requestError.message ||
115115
'Request Error',
116116
4
117117
)

src/introspection.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,38 @@ export async function introspect(
1414
url: string,
1515
requestOptions?: Options
1616
): Promise<IntrospectionQuery> {
17-
const body = await request({
18-
...requestOptions,
19-
headers: {
20-
'User-Agent': `graphql-monkey/${version}`,
21-
...(requestOptions ? requestOptions.headers : undefined)
22-
},
23-
url,
24-
body: {
25-
query: getIntrospectionQuery({ descriptions: false })
26-
},
27-
json: true,
28-
method: 'POST'
29-
});
30-
31-
if (body.errors && body.errors.length > 0) {
32-
throw new Error(
33-
`Introspection failed: ${body.errors
34-
.map((err: GraphQLError) => err.message)
35-
.join(', ')}`
36-
);
37-
}
38-
39-
if (!body.data) {
40-
throw new Error('Introspection failed; no data');
41-
}
42-
43-
return body.data;
17+
try {
18+
const body = await request({
19+
...requestOptions,
20+
headers: {
21+
'User-Agent': `graphql-monkey/${version}`,
22+
...(requestOptions ? requestOptions.headers : undefined)
23+
},
24+
url,
25+
body: {
26+
query: getIntrospectionQuery({ descriptions: false })
27+
},
28+
json: true,
29+
method: 'POST'
30+
});
31+
32+
if (body.errors && body.errors.length > 0) {
33+
throw new Error(
34+
`Introspection failed: ${body.errors
35+
.map((err: GraphQLError) => err.message)
36+
.join(', ')}`
37+
);
38+
}
39+
40+
if (!body.data) {
41+
throw new Error('Introspection failed: No data');
42+
}
43+
44+
return body.data;
45+
} catch (err) {
46+
err.message = `Introspection failed: ${err.message}`;
47+
throw err;
48+
}
4449
}
4550

4651
export class IntrospectionHelper {

src/session.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,23 @@ export class Session {
126126
timeout
127127
});
128128

129-
const errors = response.body.errors || [];
129+
const data = response.body && response.body.data;
130+
const errors = response.body && response.body.errors || [];
130131

131132
const unexpectedErrors: GraphQLError[] = errors.filter(
132133
(error: GraphQLError) =>
133134
!errorCallback || errorCallback(error, this.options)
134135
);
135136

136-
this.memory.write([], response.body.data);
137+
if (data) {
138+
this.memory.write([], data);
139+
}
137140

138141
result = {
139142
query,
140143
queryAst,
141144
statusCode: response.statusCode,
142-
data: response.body.data,
145+
data,
143146
errors,
144147
responseTime: Date.now() - t,
145148
unexpectedErrors,

0 commit comments

Comments
 (0)