This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-schema.js
62 lines (59 loc) · 1.65 KB
/
reddit-schema.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull
} = require('graphql');
const { loadSubreddit } = require('./reddit-api');
const subredditType = require('./types/Subreddit');
const userType = require('./types/User');
const redditSchema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RedditAPI',
description: 'Call the Reddit API via GraphQL',
fields: {
subreddit: {
type: subredditType,
args: {
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'The name of the subreddit'
}
},
resolve: (root, { name }) => {
if (name.includes('+')) {
const subreddits = name.split('+');
const subredditName = subreddits[0].replace(/[\W^+]/, '');
return loadSubreddit(subredditName);
} else {
const subredditName = name.replace(/[^A-Za-z0-9_+]/, '');
return loadSubreddit(subredditName);
}
}
},
user: {
type: userType,
args: {
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'The name of the user'
},
after: {
description: 'Load posts after this fullname id',
type: GraphQLString
},
limit: {
description: 'Load this many posts',
type: GraphQLInt
}
},
resolve: (root, { name, ...args }) => {
const username = name.replace(/\W/, '');
return { username, ...args };
}
}
}
})
});
module.exports = redditSchema;