@directus/gatsby-source-directus

9.16.2 • Public • Published

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: "johndoe@directus.cloud",
					// 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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
9.16.2521latest

Version History

VersionDownloads (Last 7 Days)Published
9.16.2521
9.16.11
9.16.01
9.15.1226
9.15.08
9.14.81
9.14.71
9.14.60
9.14.40
9.14.31
9.14.20
9.14.1120
9.14.00
9.13.05
9.12.20
9.12.10
9.12.00
9.11.10
9.11.01
9.10.01
9.9.11
9.9.00
9.8.01
9.7.11
9.7.01
9.6.01
9.5.22
9.5.11
9.5.01
9.4.30
9.4.21
9.4.10
9.4.00
9.3.01
9.2.20
9.2.11
9.2.00
9.1.20
9.1.10
9.1.01
9.0.12
9.0.00
9.0.0-rc.1011
9.0.0-rc.1001
9.0.0-rc.991
9.0.0-rc.980
9.0.0-rc.971
9.0.0-rc.961
9.0.0-rc.951
9.0.0-rc.941
9.0.0-rc.931
9.0.0-rc.920
9.0.0-rc.9110
9.0.0-rc.901
9.0.0-rc.890
9.0.0-rc.880
9.0.0-rc.870
9.0.0-rc.860
9.0.0-rc.851
9.0.0-rc.841
9.0.0-rc.830
9.0.0-rc.821
9.0.0-rc.810
9.0.0-rc.801
9.0.0-rc.790
9.0.0-rc.780
9.0.0-rc.772
9.0.0-rc.761
9.0.0-rc.750
9.0.0-rc.741
9.0.0-rc.730
9.0.0-rc.721
9.0.0-rc.710
9.0.0-rc.701
9.0.0-rc.691
9.0.0-rc.680
9.0.0-rc.672
9.0.0-rc.661
9.0.0-rc.651
9.0.0-rc.641
9.0.0-rc.631
9.0.0-rc.621
9.0.0-rc.610
9.0.0-rc.601
9.0.0-rc.591
9.0.0-rc.581
9.0.0-rc.571
9.0.0-rc.560
9.0.0-rc.551
9.0.0-rc.541
9.0.0-rc.530
9.0.0-rc.520
9.0.0-rc.511
9.0.0-rc.500
9.0.0-rc.490
9.0.0-rc.481
9.0.0-rc.460
9.0.0-rc.450
9.0.0-rc.441
9.0.0-rc.430
9.0.0-rc.420
9.0.0-rc.410
9.0.0-rc.400
9.0.0-rc.391
9.0.0-rc.380
9.0.0-rc.371
9.0.0-rc.360
9.0.0-rc.350
0.2.946
0.2.81
0.3.01
0.2.70
0.2.60
0.2.51
0.2.40
0.2.31
0.2.20
0.2.10
2.3.00
0.2.1-01
0.2.0-00
0.1.2-01
0.1.1-00
0.1.01

Package Sidebar

Install

npm i @directus/gatsby-source-directus

Weekly Downloads

205

Version

9.16.2

License

MIT

Unpacked Size

19.9 kB

Total Files

11

Last publish

Collaborators

  • benhaynes
  • rijk