Skip to content

Commit 0c3e4ba

Browse files
LekoArtsgatsbybot
and
gatsbybot
authored
chore(docs): Add example to link directive (#30253)
* chore(docs): Add example to link directive * chore: format Co-authored-by: gatsbybot <[email protected]>
1 parent b0e9b08 commit 0c3e4ba

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/docs/reference/graphql-data-layer/schema-customization.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,42 @@ type AuthorJson implements Node {
435435
}
436436
```
437437

438+
This example assumes that your markdown frontmatter is in the shape of:
439+
440+
```markdown
441+
---
442+
reviewers:
443+
- jane@example.com
444+
- doe@example.com
445+
---
446+
```
447+
448+
And your author JSON looks like this:
449+
450+
```json
451+
[
452+
{
453+
"name": "Doe",
454+
"firstName": "Jane",
455+
"email": "[email protected]"
456+
},
457+
{
458+
"name": "Doe",
459+
"firstName": "Zoe",
460+
"email": "[email protected]"
461+
}
462+
]
463+
```
464+
438465
You provide a `@link` directive on a field and Gatsby will internally
439466
add a resolver that is quite similar to the one written manually above. If no
440467
argument is provided, Gatsby will use the `id` field as the foreign-key,
441468
otherwise the foreign-key has to be provided with the `by` argument. The
442469
optional `from` argument allows getting the field on the current type which acts as the foreign-key to the field specified in `by`.
443470
In other words, you `link` **on** `from` **to** `by`. This makes `from` especially helpful when adding a field for back-linking.
444471

472+
For the above example you can read `@link` this way: Use the value from the field `Frontmatter.reviewers` and match it by the field `AuthorJson.email`.
473+
445474
Keep in mind that in the example above, the link of `posts` in `AuthorJson` works because `frontmatter` and `author` are both objects. If, for example, the `Frontmatter` type had a list of `authors` instead (`frontmatter.authors.email`), it wouldn't work since the `by` argument doesn't support arrays. In that case, you'd have to provide a custom resolver with [Gatsby Type Builders](/docs/reference/graphql-data-layer/schema-customization/#gatsby-type-builders) or [createResolvers API](/docs/reference/graphql-data-layer/schema-customization/#createresolvers-api).
446475

447476
> Note that when using `createTypes` to fix type inference for a foreign-key field

0 commit comments

Comments
 (0)