9.14.4 • Published 2 years ago

@skuhnow/directus-gatsby-source-directus v9.14.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

gatsby-source-directus

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

Install

npm install --save @skuhnow/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: '@skuhnow/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

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

9.14.2

2 years ago

9.14.3

2 years ago

9.14.4

2 years ago

9.13.1

2 years ago

9.13.2

2 years ago

9.10.2

2 years ago

9.12.3

2 years ago

9.10.1

2 years ago

9.8.4

2 years ago

9.8.3

2 years ago

9.8.2

2 years ago

9.8.0

2 years ago