Schema
Update your graphql.schema by adding the following
graphqltype HashtagTrend {hashtag: String!tweetCount: Int!}type TopicTrendQuote {title: String!description: String!imageUrl: String!}type TopicTrend {topic: String!tweetCount: Int!quote: TopicTrendQuote}union Trend = TopicTrend | HashtagTrend
and add add a new top-level query
difftype Query {+ trends: [Trend!]!}
In client/src/App.tsx add the following to the GET_CURRENT_USER query
tstrends {... on TopicTrend {tweetCounttopicquote {titleimageUrldescription}}... on HashtagTrend {tweetCounthashtag}}
Run yarn codegen in both the client and server sub-projects to get
updated TS types that include this change to the schema and query
Server
Create a new transform for this Trend object in server/src/transforms.ts
tsimport { DbTrend } from "./db"import { Trend } from "./resolvers-types.generated"export const trendTransform = (t: DbTrend): Trend => {const { tweetCount } = tif (t.kind === "topic") {const { topic, quote } = treturn { tweetCount, topic, quote }} else {const { hashtag } = treturn { tweetCount, hashtag }}}
And add the new top-level Query resolver in server/src/resolvers/Query.ts
tsimport { trendTransform } from "../transforms"
tstrends: (_, __, { db }) => {return db.getAllTrends().map(trendTransform);},
Create a new Trend resolver module server/src/resolvers/Trend.ts
tsimport { TwitterResolverContext } from "../resolvers"import { TrendResolvers } from "../resolvers-types.generated"const trendTwitterResolver: TrendResolvers<TwitterResolverContext> ={__resolveType(obj, _context, _info) {// Only Author has a name field// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-anyif (typeof (obj as any).hashtag === "string") {return "HashtagTrend"} else return "TopicTrend"return null // GraphQLError is thrown},}export default trendTwitterResolver
and import it into server/src/resolvers.ts, adding it to the exported object
tsimport TwitterTrendResolver from "./resolvers/Trend"
diffTweet: tweetTwitterResolver,User: userTwitterResolver,+ Trend: trendTwitterResolver,
UI
In client/src/App.tsx use destructured assignment to grab trends
diff- const { currentUser, suggestions = [] } = data;+ const { currentUser, suggestions = [], trends = [] } = data;
Add this line before the function returns
tsconst filteredTrends = trends.filter(isDefined)
diff- <RightBar trends={TRENDS} suggestions={suggestions} />+ <RightBar trends={filteredTrends} suggestions={suggestions} />
You’ll know that everything is working if you see additional “trending things” in the right sidebar of the UI