forked from clayallsopp/graphqlhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
179 lines (166 loc) · 5.57 KB
/
twitter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import _ from 'lodash';
import * as twitter from './apis/twitter';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLNonNull,
GraphQLInt,
GraphQLList,
GraphQLScalarType,
GraphQLEnumType
} from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';
let UserType = new GraphQLObjectType({
name : 'TwitterUser',
description : 'Twitter user',
fields : () => ({
created_at : { type: GraphQLString },
description : { type: GraphQLString },
id : { type: GraphQLID }, // GraphQLInt would return null
screen_name : { type: GraphQLString },
name : { type: GraphQLString },
profile_image_url : { type: GraphQLString },
url : { type: GraphQLString },
tweets_count : {
type : GraphQLInt,
resolve : ({ statuses_count }) => statuses_count
},
followers_count : { type: GraphQLInt },
tweets : {
type : new GraphQLList(TweetType),
description : 'Get a list of tweets for current user',
args : {
limit: {
type : GraphQLInt,
defaultValue : 10
}
},
// user args
resolve: ({ id: user_id }, { limit }) => twitter.getTweets(user_id, limit)
}
})
});
let TweetType = new GraphQLObjectType({
name : 'Tweet',
description : 'A tweet object',
fields : () => ({
id : { type: GraphQLID },
created_at : { type: GraphQLString },
text : { type: GraphQLString },
retweet_count : { type: GraphQLInt },
user : { type: UserType },
retweets : {
type : new GraphQLList(RetweetType),
description : 'Get a list of retweets',
args : {
limit: {
type : GraphQLInt,
defaultValue : 5
}
},
// passing integer 'id' here doesn't work surprisingly, had to use 'id_str'
resolve: ({ id_str: tweetId }, { limit }) => twitter.getRetweets(tweetId, limit)
}
})
});
let RetweetType = new GraphQLObjectType({
name : 'Retweet',
description : 'Retweet of a tweet',
fields : () => ({
id : { type: GraphQLID },
created_at : { type: GraphQLString },
in_reply_to_tweet_id : {
type : GraphQLString,
resolve : ({ in_reply_to_status_id }) => in_reply_to_status_id
},
in_reply_to_user_id : { type: GraphQLInt },
in_reply_to_screen_name : { type: GraphQLString },
retweeted_status : { type: TweetType },
user : { type: UserType }
})
});
let userIdentityType = new GraphQLScalarType({
name : 'UserIdentity',
description : 'Parse user provided identity',
serialize : value => value,
parseValue : value => value,
parseLiteral : ast => {
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
throw new GraphQLError("Query error: Can only parse Integer and String but got a: " + ast.kind, [ast]);
}
return ast.value;
}
});
let userIdentifierType = new GraphQLEnumType({
name : 'UserIdentifier',
description : 'Either user unique ID, or screen name',
values : {
'id' : { value: 'user_id' },
'name' : { value: 'screen_name' }
}
});
let searchReponseType = new GraphQLEnumType({
name : 'SearchReponse',
description : 'Type of search response.',
values: {
mixed : { value: 'mixed' },
recent : { value: 'recent' },
popular : { value: 'popular' }
}
});
let twitterType = new GraphQLObjectType({
name : 'TwitterAPI',
description : 'The Twitter API',
fields : {
user : {
type : UserType,
args : {
identifier: {
description : 'Either user_id or screen_name',
type : new GraphQLNonNull(userIdentifierType)
},
identity: {
description : 'User ID (Integer) or Screen name (String) to identify user',
type : new GraphQLNonNull(userIdentityType)
},
},
resolve: (_, { identifier, identity }) => twitter.getUser(identifier, identity)
},
tweet: {
type : TweetType,
args : {
id : {
type : new GraphQLNonNull(GraphQLString),
description : 'Unique ID of tweet'
}
},
resolve: (_, { id: tweetId }) => twitter.getTweet(tweetId)
},
search: {
type : new GraphQLList(TweetType),
description : "Returns a collection of relevant Tweets matching a specified query.",
args: {
q: {
type : new GraphQLNonNull(GraphQLString),
description : "A UTF-8, URL-encoded search query of 500 characters maximum, including operators. Queries may additionally be limited by complexity."
},
count: {
type : GraphQLInt,
description : "The number of tweets to return per page, up to a maximum of 100. This was formerly the “rpp” parameter in the old Search API."
},
result_type: {
type: searchReponseType,
description: `Specifies what type of search results you would prefer to receive. Valid values include:
* mixed: Include both popular and real time results in the response.
* recent: return only the most recent results in the response
* popular: return only the most popular results in the response.`
}
},
resolve: (_, searchArgs) => twitter.searchFor(searchArgs)
}
}
});
export const QueryObjectType = twitterType;