Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 64 additions & 11 deletions gql-schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ schema {
query: TwitterAPI
}

# An object with an ID
interface Node {
# The id of the object.
id: ID!
}

# Information about pagination in a connection.
type PageInfo {
# When paginating forwards, are there more items?
hasNextPage: Boolean!

# When paginating backwards, are there more items?
hasPreviousPage: Boolean!

# When paginating backwards, the cursor to continue.
startCursor: String

# When paginating forwards, the cursor to continue.
endCursor: String
}

# Retweet of a tweet
type Retweet {
id: ID
Expand All @@ -25,23 +46,38 @@ type Tweet {
retweets(limit: Int = 5): [Retweet]
}

# A connection to a list of items.
type TweetConnection {
# Information to aid in pagination.
pageInfo: PageInfo!

# A list of edges.
edges: [TweetEdge]
}

# An edge in a connection.
type TweetEdge {
# The item at the end of the edge
node: Tweet

# A cursor for use in pagination
cursor: String!
}

# The Twitter API
type TwitterAPI {
# Fetches an object given its ID
node(
# The ID of an object
id: ID!
): Node
tweet(
# Unique ID of tweet
id: String!
): Tweet

# Returns a collection of relevant Tweets matching a specified query.
search(
# A UTF-8, URL-encoded search query of 500 characters maximum, including
# operators. Queries may additionally be limited by complexity.
q: String!

# 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.
count: Int
): Viewer
search: Viewer
}

# Twitter user
Expand All @@ -66,6 +102,23 @@ type TwitterUser {
tweets(limit: Int = 10): [Tweet]
}

type Viewer {
tweet: [Tweet]
type Viewer implements Node {
# The ID of an object
id: ID!

# Returns a list of tweets matching the query, to be specified as an argument
tweetConnection(
after: String
first: Int
before: String
last: Int

# A UTF-8, URL-encoded search query of 500 characters maximum, including
# operators. Queries may additionally be limited by complexity.
q: String

# 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.
count: Int
): TweetConnection
}
74 changes: 58 additions & 16 deletions gql-schema/schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import {
GraphQLSchema,
GraphQLObjectType,
Expand All @@ -7,9 +8,38 @@ import {
GraphQLInt,
GraphQLList
} from 'graphql';

import {
globalIdField,
fromGlobalId,
nodeDefinitions,
connectionDefinitions,
connectionArgs,
connectionFromPromisedArray,
mutationWithClientMutationId
} from "graphql-relay";

import * as twitterCli from '../twitter-cli';
import * as Weather from '../weather';

class Store {}
let store = new Store();

let nodeDefs = nodeDefinitions(
(globalId) => {
let {type} = fromGlobalId(globalId);
if (type === 'Viewer') {
return store
}
return null;
},
(obj) => {
if (obj instanceof Store) {
return viewerType;
}
return null;
}
);
let UserType = new GraphQLObjectType({
name: 'TwitterUser',
description: 'Twitter user',
Expand Down Expand Up @@ -95,21 +125,43 @@ let RetweetType = new GraphQLObjectType({
})
});

let tweetConnection = connectionDefinitions({
name: 'Tweet',
nodeType: TweetType
});

let viewerType = new GraphQLObjectType({
name: 'Viewer',
fields: {
tweet: {
type: new GraphQLList(TweetType),
resolve: (viewer) => viewer,
},
id: globalIdField("Viewer"),
tweetConnection: {
type: tweetConnection.connectionType,
description:"Returns a list of tweets matching the query, to be specified as an argument",
args: {
...connectionArgs,
q: {
type: 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."
}
},
resolve: (_, args) => connectionFromPromisedArray(twitterCli.searchTweets(args),args)
},
},
interfaces: [nodeDefs.nodeInterface]
});



let twitterType = new GraphQLObjectType({
name: 'TwitterAPI',
description: 'The Twitter API',
fields: {
tweet: {
node: nodeDefs.nodeField,
tweet: {
type: TweetType,
args: {
id: {
Expand All @@ -122,17 +174,7 @@ let twitterType = new GraphQLObjectType({
search: {
type: viewerType,
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."
}
},
resolve: (_, searchArgs) => twitterCli.searchTweets(searchArgs)
resolve: () => store
}
}
});
Expand Down
Loading