Skip to content

Commit

Permalink
Add in a sub-query to get content matching an individual tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fredex42 committed Jul 15, 2024
1 parent 53bd6b0 commit 2ca11de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object RootQuery {
Field("endCursor", OptionType(StringType), Some("The last record cursor in the set"), resolve = _.value.endCursor),
Field("hasNextPage", BooleanType, Some("Whether there are any more records to retrieve"), resolve = _.value.hasNextPage),
Field("nodes", ListType(com.gu.contentapi.porter.graphql.Tags.Tag), Some("The actual tags returned"), resolve = _.value.nodes),
Field("matching_content", ArticleEdge, Some("Content which matches any of the tags returned"),
Field("matchingAnyTag", ArticleEdge, Some("Content which matches any of the tags returned"),
arguments= ContentQueryParameters.AllContentQueryParameters,
resolve = { ctx=>
ctx.ctx.repo.marshalledDocs(ctx arg ContentQueryParameters.QueryString,
Expand Down
27 changes: 24 additions & 3 deletions src/main/scala/com/gu/contentapi/porter/graphql/Tags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.time.format.DateTimeFormatter
import io.circe.generic.auto._
import io.circe.syntax._
import com.gu.contentapi.porter.model
import datastore.GQLQueryContext

object Tags {
import Content.Reference
Expand All @@ -15,10 +16,30 @@ object Tags {
implicit val PodcastCategory = deriveObjectType[Unit, model.PodcastCategory]()
implicit val TagPodcast = deriveObjectType[Unit, model.TagPodcast]()

val Tag = deriveObjectType[Unit, model.Tag](
val Tag = deriveObjectType[GQLQueryContext, model.Tag](
ReplaceField("type", Field("type", OptionType(TagQueryParameters.TagTypes), resolve=_.value.`type`)),
ReplaceField("alternateIds", Field("alternateIds", ListType(StringType), arguments = AlternateIdParameters.AllAlternateIdParameters, resolve= AlternateIdParameters.TagResolver[Unit])),
ReplaceField("alternateIds", Field("alternateIds", ListType(StringType), arguments = AlternateIdParameters.AllAlternateIdParameters, resolve= AlternateIdParameters.TagResolver[GQLQueryContext])),
ReplaceField("tagCategories", Field("tagCategories", ListType(StringType), resolve = _.value.tagCategories.map(_.toSeq).getOrElse(Seq()))),
ReplaceField("entityIds", Field("entityIds", ListType(StringType), resolve = _.value.tagCategories.map(_.toSeq).getOrElse(Seq())))
ReplaceField("entityIds", Field("entityIds", ListType(StringType), resolve = _.value.tagCategories.map(_.toSeq).getOrElse(Seq()))),
AddFields(
Field("matchingContent", RootQuery.ArticleEdge, description=Some("Articles and other content which have this tag"),
arguments= ContentQueryParameters.AllContentQueryParameters.filterNot(_ == ContentQueryParameters.TagArg),
resolve = { ctx=>
ctx.ctx.repo.marshalledDocs(ctx arg ContentQueryParameters.QueryString,
queryFields=ctx arg ContentQueryParameters.QueryFields,
atomId = None,
forChannel = ctx arg ContentQueryParameters.ChannelArg,
userTier = ctx.ctx.userTier,
tagIds = Some(Seq(ctx.value.id)),
excludeTags = ctx arg ContentQueryParameters.ExcludeTagArg,
sectionIds = ctx arg ContentQueryParameters.SectionArg,
excludeSections = ctx arg ContentQueryParameters.ExcludeSectionArg,
orderDate = ctx arg PaginationParameters.OrderDate,
orderBy = ctx arg PaginationParameters.OrderBy,
limit = ctx arg PaginationParameters.Limit,
cursor = ctx arg PaginationParameters.Cursor,
)
})
)
)
}

0 comments on commit 2ca11de

Please sign in to comment.