Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Fn added #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
90 changes: 65 additions & 25 deletions gql-schema/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
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
}

type Query {
# Fetches an object given its ID
node(
# The ID of an object
id: ID!
): Node
store: Store
}

# Retweet of a tweet
Expand All @@ -13,35 +39,53 @@ type Retweet {
user: TwitterUser
}

# A tweet object
type Tweet {
id: ID
created_at: String
text: String
retweet_count: Int
user: TwitterUser
# A connection to a list of items.
type SearchLinkConnection {
# Information to aid in pagination.
pageInfo: PageInfo!

# Get a list of retweets
retweets(limit: Int = 5): [Retweet]
# A list of edges.
edges: [SearchLinkEdge]
}

# The Twitter API
type TwitterAPI {
tweet(
# Unique ID of tweet
id: String!
): Tweet
# An edge in a connection.
type SearchLinkEdge {
# The item at the end of the edge
node: Tweet

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

type Store implements Node {
# The ID of an object
id: ID!
searchConnection(
after: String
first: Int
before: String
last: Int

# 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!
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
): SearchLinkConnection
}

# A tweet object
type Tweet {
id: ID
created_at: String
text: String
retweet_count: Int
user: TwitterUser

# Get a list of retweets
retweets(limit: Int = 5): [Retweet]
}

# Twitter user
Expand All @@ -65,7 +109,3 @@ type TwitterUser {
# Get a list of tweets for current user
tweets(limit: Int = 10): [Tweet]
}

type Viewer {
tweet: [Tweet]
}
118 changes: 82 additions & 36 deletions gql-schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@ 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 === "Store"){
return store;
}
return null;
},

(obj) => {
if(obj instanceof Store){
return storeType;
}
return null;
}
);

let UserType = new GraphQLObjectType({
name: 'TwitterUser',
description: 'Twitter user',
Expand Down Expand Up @@ -95,48 +127,62 @@ let RetweetType = new GraphQLObjectType({
})
});

/*let viewer = {};
let viewerType = new GraphQLObjectType({
name: 'Viewer',
fields: {
fields: () => ({
id: globalIdField("Viewer"),
tweet: {
type: new GraphQLList(TweetType),
resolve: (viewer) => viewer,
},
},
});
resolve: (viewer) => viewer
}
})
});*/

let twitterType = new GraphQLObjectType({
name: 'TwitterAPI',
description: 'The Twitter API',
fields: {
tweet: {
type: TweetType,
args: {
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'Unique ID of tweet'
}
},
resolve: (_, { id: tweetId }) => twitterCli.getTweet(tweetId)
},
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."
let searchConnection = connectionDefinitions({
name:'SearchLink',
nodeType:TweetType
})

let storeType = new GraphQLObjectType({
name: 'Store',
fields: () => ({
id: globalIdField("Store"),
searchConnection: {
type: searchConnection.connectionType,
id: globalIdField("Search"),
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) => {
return (connectionFromPromisedArray(
twitterCli.searchTweets(args),args))
}
},
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)
}
}
});

}),
interfaces:[nodeDefs.nodeInterface]
});



export const Schema = new GraphQLSchema({
query: twitterType
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeDefs.nodeField,
store: {
type: storeType,
resolve: () => store
}
})
})
});
Loading