Skip to content

docs: Example @link directive with array (by elemMatch) #36330

@cpboyd

Description

@cpboyd

Preliminary Checks

Summary

Previous issues:

After looking into a custom createFieldExtension , I discovered that elemMatch works and subsequently found this:
#25336 (comment)

Steps to Resolve this Issue

The docs should clarify that this works:

type Author implements Node {
  id: ID!
  name: String!
  email: String!
- posts: [Post!]! @link(from: "email", by: "authors.email")
+ posts: [Post!]! @link(from: "email", by: "authors.elemMatch.email")
}

type Post implements Node {
  id: ID!
  title: String!
  body: String!
  authors: [Author!]! @link(by: "email")
  tags: [Tag!]! @link
}

type Tag implements Node {
  id: ID!
  label: String!
  description: String!
- posts: [Post!]! @link(from: "id", by: "tags.id")
+ posts: [Post!]! @link(from: "id", by: "tags.elemMatch.id")
}

This works because how the query is formed by splitting on .:

runQueryArgs.filter = options.by.split(`.`).reduceRight(
(acc: Record<string, any>, key: string) => {
const obj = {}
obj[key] = acc
return obj
},
Array.isArray(fieldValue) ? { in: fieldValue } : { eq: fieldValue }
)

Metadata

Metadata

Assignees

Labels

good first issueIssue that doesn't require previous experience with Gatsbyhelp wantedIssue with a clear description that the community can help with.type: documentationAn issue or pull request for improving or updating Gatsby's documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions