Loving Tina? us on GitHub0.0k

文档

学习

v.Latest
Documentation
查询单个文档
目录

查询单个文档

获取单个文档,提供其 relativePath 作为参数。

  • relativePath 是相对于 collection 路径的部分路径。

在这个例子中,post 集合的路径是 content/posts,你的文档可以在 content/posts/voteForPedro.md 找到,给出 relativePath: "voteForPedro.md"。如果你的项目在 content/posts/nested-folder/voteForPedro.md,你需要指定:relativePath: "nested-folder/voteForPedro.md"

我们上面 schema 的集合命名为 "post" 和 "author",因此我们可以使用 postauthor 查询每一个。

示例

仅查询作者。

{
author(relativePath: "napolean.json") {
name
}
}
{
"data": {
"author": {
"name": "Napolean"
}
}
}

查询文章和作者。

{
post(relativePath: "voteForPedro.json") {
title
category
author {
... on Author {
name
}
}
}
}
{
"data": {
"post": {
"title": "Vote For Pedro",
"category": "politics",
"author": {
"name": "Napolean"
}
}
}
}

在作者上查询文章。

{
author(relativePath: "napolean.json") {
name
post {
edges {
node {
title
}
}
}
}
}
{
"data": {
"author": {
"name": "Napolean",
"post": {
"edges": [
"node": {
"title": "Vote for Pedro",
}
]
}
}
}
}

常见字段

在一个集合中,有一些字段是所有文档共有的。这些字段是:id_values_sysid 字段是文档的唯一标识符。_values 字段在编辑模式中内部使用,不用于外部。_sys 字段包含关于文档的元信息。

_sys 字段是一个包含以下字段的对象:

  • filename: 文件名,不包括扩展名
  • basename: 文件名,包括扩展名
  • path: 文件相对于项目根目录的完整路径
  • breadcrumbs: 文件的父文件夹数组
  • relativePath: 文件相对于集合路径的路径
  • extension: 文件的扩展名
  • template: 文档的模板(如果不使用 templates,则为集合的名称)
  • collection: 关于集合的信息

带有嵌套文件夹的示例

{
post(relativePath: "nested/anotherPost.json") {
id
_sys {
filename
basename
path
breadcrumbs
relativePath
extension
template
}
}
}
{
"data": {
"post": {
"id": "content/posts/nested/anotherPost.json",
"_sys": {
"filename": "anotherPost",
"basename": "anotherPost.json",
"path": "content/posts/nested/anotherPost.json",
"breadcrumbs": [
"nested",
"anotherPost"
],
"relativePath": "nested/anotherPost.json",
"extension": ".json",
"template": "post"
}
}
}
}

没有嵌套文件夹的示例

{
post(relativePath: "anotherPost.json") {
id
_sys {
filename
basename
path
breadcrumbs
relativePath
extension
template
}
}
}
{
"data": {
"post": {
"id": "content/posts/anotherPost.json",
"_sys": {
"filename": "anotherPost",
"basename": "anotherPost.json",
"path": "content/posts/anotherPost.json",
"breadcrumbs": [
"anotherPost"
],
"relativePath": "anotherPost.json",
"extension": ".json",
"template": "post"
}
}
}
}
上次编辑: August 15, 2024