@directus/gatsby-source-directus

中文文档
9.16.2发布于 2023/09/20MIT近 30 天下载 4328

Source plugin for pulling data into Gatsby from a Directus API.

原始使用文档
文档目录8 节
  1. 5️⃣ Installation for gatsby 4 and 5
  2. gatsby@4 dependencies
  3. gatsby@5 dependencies
  4. Install the package
  5. Usage
  6. Options
  7. How to query
  8. Docs

gatsby-source-directus

Source plugin for pulling data into the Gatsby GraphQL Data Layer from a Directus API.

5️⃣ Installation for gatsby 4 and 5

Depending on the gatsby version you are using you will need to include the following dependencies to your project on these respective versions: @4if you are using gatsby@4 and @5 if you are using gatsby@5.

gatsby@4 dependencies

npm install --save gatsby-source-graphql@4 gatsby-source-filesystem@4

gatsby@5 dependencies

npm install --save gatsby-source-graphql@5 gatsby-source-filesystem@5

Install the package

npm install --save @directus/gatsby-source-directus

Usage

  1. Go to gatsby-config.js on your Gatsby project
  2. Add new entry to plugins properly configured with your settings
module.exports = {
    // ... some gatsby configuration
    plugins: [
        // ... some gatsby plugins

        // You can take advantage of the following plugins with gatsby-source-directus

        // `gatsby-plugin-image`,
        // `gatsby-transformer-sharp`,
        // `gatsby-plugin-sharp`,

        // Finally our plugin
        {
            resolve: '@directus/gatsby-source-directus',
            options: {
                url: `https://myproject.directus.cloud`, // Fill with your Directus instance address
                auth: {
                    token: 'my_secret_token', // You can use a static token from an user

                    // Or you can use the credentials of an user
                    // email: "[email protected]",
                    // password: "mysecretpassword",
                },
            },
        },
    ],
};
  1. Request your data
query {
    # if you change `type.name`, remember to also rename following field
    directus {
        # the collection you want to query
        articles {
            # the fields you want to query from above collection
            title
            files {
                # since this is a M2M relationship, we need to reference the junction field
                directus_files_id {
                    # `id` is required to be fetched in order to be used with `gatsby-transformer-sharp`
                    id
                    imageFile {
                        # when using the plugin 'gatsby-transformer-sharp', you can query images with transformations
                        childImageSharp {
                            gatsbyImageData(width: 200)
                        }
                    }
                }
            }
        }
    }

    # it's also possible to query system collections
    directus_system {
        users {
            email
        }
        files {
            id
            imageFile {
                childImageSharp {
                    gatsbyImageData(width: 200)
                }
            }
        }
    }
}

Note: When using with gatsby-transformer-sharp you will need to query id of the asset (specified on DirectusData_directus_files type).

Options

  • url [Required] - should be a valid URL to your Directus instance

  • auth [Optional] - defines if requests will have authentication or not. You should define this if you want access to non-public content. View more about permissions

    • auth.token [Optional] - should be the static token of the user which will make the requests. You can define one on user detail page.

    • auth.email [Optional, but required with password and ignored if auth.token defined] - should be the email of the user which will make the requests.

    • auth.password [*Optional, but required with email and ignored if auth.token defined*] - should be the password of the user which will make the requests.

  • type [Optional] - defines the type and field name to be used on GraphQL. If you are using multiple instances of Directus, please ensure you have unique type and field names per instance.

    • type.name [Optional, defaults to DirectusData] - defines the GraphQL type name to be used for user defined collections

    • type.field [Optional, defaults to directus] - defines the GraphQL field name to query user defined collections. If you change this property, remember to query the proper field inside query.

    • type.system_name [Optional, defaults to DirectusSystemData] - defines the GraphQL type name to be used for Directus defined collections, i.e., directus_users, directus_files, etc.

    • type.system_field [Optional, defaults to directus_system] - defines the GraphQL field name to query Directus defined collections.

  • dev [Optional] - defines options for development environments

    • dev.refresh [Optional, defaults to 15s] - tells the refresh rate in seconds of the schema. Should be a number in seconds or a string supported by ms
  • graphql [Optional] - defines extra options to be passed into gatsby-source-graphql. Useful for advanced use cases.

  • concurrency [Optional, defaults to 10] - tells how much images tries to download concurrently

How to query

The default way to query data is to fetch items from directus field.

query {
    directus {
        items {
            my_collection {
                some_field
                other_field
            }
        }
    }
}

If you specify the type.field, you must query from that field instead.

query {
    # In this case `type.field` is "blog"
    blog {
        items {
            posts {
                id
                title
                slug
                status
            }
        }
    }
    # While in this case `type.field` is "portal"
    portal {
        items {
            pages {
                id
                title
                slug
                status
            }
        }
    }
}

Docs

View more about Directus

版本与发布记录

@directus/gatsby-source-directus 共发布 124 个版本,首次发布于 2020/01/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
9.16.2latest2023/09/203 年前
9.16.12023/07/113 年前
9.16.02023/07/103 年前
9.15.12023/01/213 年前
9.15.02022/12/063 年前
9.14.82022/12/013 年前

关于 @directus/gatsby-source-directus 的常见问题

@directus/gatsby-source-directus 是什么?

Source plugin for pulling data into Gatsby from a Directus API.

如何安装 @directus/gatsby-source-directus?

在项目目录运行 npm install @directus/gatsby-source-directus,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:eslint(7||8)、gatsby-source-graphql(4||5)、gatsby-source-filesystem(4||5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@directus/gatsby-source-directus 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/directus__gatsby-source-directus 是否存在。

@directus/gatsby-source-directus 可以商用吗?

@directus/gatsby-source-directus 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@directus/gatsby-source-directus 的下载量有多少?

根据 npm 官方下载统计,@directus/gatsby-source-directus 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,328 次,日均约 144 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@directus/gatsby-source-directus 还在维护吗?

最新版本 9.16.2 发布于 2023/09/20(3 年前);至今共发布 124 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。