-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
40 lines (33 loc) · 995 Bytes
/
schema.js
File metadata and controls
40 lines (33 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Imports
const {makeExecutableSchema} = require('graphql-tools');
const {importSchema} = require('graphql-import');
const typeDefs = importSchema('./schema.graphql'); // import schema definition
const fetch = require('node-fetch');
// Definitions
const BASE_URL = 'https://api.github.com'; // should be an env var
// Resolvers
const resolvers = {
Query: {
async user(parent, args, ctx, info) {
if (!args.username) {
throw new Error('username is required')
}
const userInfo = await fetch(`${BASE_URL}/users/${args.username}`);
return userInfo.ok ? userInfo.json() : null;
}
},
User: {
async repositories(parent, args, ctx, info) {
if (!parent['repos_url']) {
throw new Error('missing repos_url in parent');
}
const reposInfo = await fetch(parent['repos_url']);
return reposInfo.ok ? reposInfo.json() : [];
},
}
};
// Export Schema
module.exports = makeExecutableSchema({
typeDefs,
resolvers,
});