Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sourcing from Flotiq documentation entry #24721

Merged
merged 30 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a34b680
Add Sourcing from Flotiq documentation entry
Jun 2, 2020
293d0ac
fix whitespace
Jun 2, 2020
a6ae9b0
prettier
Jun 2, 2020
7815d16
fix prettier, again
Jun 2, 2020
4dc4019
add headless-cms table entry
Jun 2, 2020
ec2e304
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
00d6dfb
`Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
034f3c9
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
9158d46
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
d3bafaa
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
c5b95d8
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
9b70390
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
60c5b59
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
860f6a5
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
a3b8f7f
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
a8a26b4
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
46871e6
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
744ba4d
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
e088069
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
e380630
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
fdc8e3c
Apply suggestions from code review
andrzejwp Jun 9, 2020
8498630
fix improper API key reference
Jun 9, 2020
8a703be
Update docs/docs/sourcing-from-flotiq.md
andrzejwp Jun 9, 2020
b951755
add blogpost template
Jun 9, 2020
c7d36b6
simplify blog-post template
Jun 9, 2020
8cd9a08
add full gatsby-node.js example
Jun 9, 2020
9888ee7
Apply suggestions from code review
andrzejwp Jun 10, 2020
b3c7d56
prettier, update img alt attribute
Jun 10, 2020
2e491bf
remove empty paragraph
Jun 10, 2020
581eda8
add dotenv to gatsby-config example
Jun 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/headless-cms.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Here are more resources for guides, plugins, and starters for CMS systems you ca
| [Gentics Mesh](https://getmesh.io) | [guide](/docs/sourcing-from-gentics-mesh) | | |
| [Seams-CMS](https://seams-cms.com/) | [guide](/docs/sourcing-from-seams-cms) | | |
| [Builder.io](https://www.builder.io/) | [guide](/docs/sourcing-from-builder-io/) | [docs](/packages/@builder.io/gatsby/) | [starter](https://github.com/BuilderIO/gatsby-starter-builder) |
| [Flotiq](https://flotiq.com/) | [guide](/docs/sourcing-from-flotiq/) | [docs](/packages/gatsby-source-flotiq) | [starter](https://github.com/flotiq/gatsby-starter-blog) |

## How to add new guides to this section

Expand Down
182 changes: 182 additions & 0 deletions docs/docs/sourcing-from-flotiq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
title: Sourcing from Flotiq
---

This guide will help you understand how to setup Gatsby to pull data from [Flotiq](https://flotiq.com).
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

Flotiq is a headless CMS with a primary focus on developer experience and integration capabilities. Flotiq's cloud-based dashboard allows you to easily design your content types and work with your data, but Flotiq also provides support for powerful integrations. One of Flotiq's key principles is to provide an effortless way to consume your content in the applications you build. It's solved by supporting technologies like OpenAPI 3.0 or Zapier as well as by providing customized API docs and SDK libraries based on your content types.

Using a static website generator, like Gatsby, to consume content stored in Flotiq is one of the most frequent use cases. The integration is based on the [gatsby-source-flotiq](https://github.com/flotiq/gatsby-source-flotiq) source plugin, which bridges the dynamic REST API of your Flotiq account with Gatsby's GraphQL.
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

> This document will base on the simple [gatsby-starter-blog](https://github.com/flotiq/gatsby-starter-blog) blog starter, but remember to check out the more advanced starters we've prepared to work with Flotiq:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved
>
> - [Recipe website Gatsby starter](https://github.com/flotiq/gatsby-starter-recipes)
> - [Event calendar Gatsby starter](https://github.com/flotiq/gatsby-starter-event-calendar)
> - [Project portfolio Gatsby starter](https://github.com/flotiq/gatsby-starter-projects)
> - [Simple blog Gatsby starter](https://github.com/flotiq/gatsby-starter-blog)
> - [Gatsby and Snipcart boilerplate, sourcing products from Flotiq](https://github.com/flotiq/gatsby-starter-products)
> - [Gatsby and Snipcart, e-commerce with products and categories from Flotiq](https://github.com/flotiq/gatsby-starter-products-with-categories)

## Setup

### Quickstart
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

1. **Start project from template using Gatsby CLI**

```bash
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved
gatsby new my-blog-starter https://github.com/flotiq/gatsby-starter-blog
```

2. **Setup "Blog Post" Content Type in Flotiq**
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

Create your [Flotiq.com](https://flotiq.com) account. Next, create the `Blog Post` Content Type straight from the `Type definitions` page.

_Note: You can also create `Blog Post` using [Flotiq REST API](https://flotiq.com/docs/API/)._
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

3. **Configure application**

The next step is to configure your application to know from where it has to fetch the data.

You need to create a file called `.env` inside the root project directory, with the following structure:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

```shell:title=.env
GATSBY_FLOTIQ_BASE_URL=https://api.flotiq.com
GATSBY_FLOTIQ_API_KEY=YOUR FLOTIQ API KEY
```

4. **Start developing**

Navigate into your new site’s directory and start it up.

```shell
cd my-blog-starter/
npm install
gatsby develop
```

If you wish to import example blog posts to your account, before running `gatsby develop` run:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

```shell
node ./.flotiq/importExample.js
```

It will add 1 image and 2 blog posts to your Flotiq account.

_Note: You need to put your Read and write API key in `.env` for import to work. You don't need Blog Post content type in your account. If you already have posts with ids `blogpost-1` and `blogpost-2` they will be overwritten._
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

### Source plugin
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

The Blog starter you've just set up uses the [gatsby-source-flotiq](https://github.com/flotiq/gatsby-source-flotiq) source plugin to pull content from Flotiq.

Here are the steps to use this source plugin in other Gatsby projects:

> If you're using the provided starter - all the following steps have been already taken care of, you can dive into the relevant project files to verify how it's been done.

1. Install the plugin:
```shell
npm install --save gatsby-source-flotiq
```
2. Provide API credentials in `.env`:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved
3. Register the source plugin in `gatsby-config.js`:
Make sure your `gatsby-config.js` has the following configuration:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved
```javascript:title=gatsby-config.js
module.exports = {
// ...
plugins: [
{
resolve: "gatsby-source-flotiq",
options: {
baseUrl: process.env.GATSBY_FLOTIQ_BASE_URL,
authToken: process.env.FLOTIQ_API_KEY,
},
},
],
// ...
}
```

By default the source plugin will pull all the data you've stored in Flotiq. In some cases, e.g. when building sites that have thousands of pages - you'll likely want to limit the number of pulled data during development. You can do that either by narrowing down the scope to specific content types or by limiting the number of pulled objects (see the `includeTypes` and `objectLimit` options). The source plugin will do its best to resolve any dependencies for you.
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

> You can find more details about how to configure the Flotiq source plugin in the [plugin's README](https://github.com/flotiq/gatsby-source-flotiq).

## Accessing Flotiq data in Gatsby

Once you finish configuring your environment you can start developing. The source plugin will pull all the content from your Flotiq account and create respective Gatsby GraphQL nodes. It will also preserve the relations you've setup in Flotiq, so the GraphQL nodes will be automatically linked, just as you would expect.
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

For example, if you define a relation between `BlogPost` and `Category` content types in Flotiq - they will retain their relationship in Gatsby's GraphQL, so you can retrieve that in a query:

```graphql
query {
allBlogPost {
nodes {
id
slug
category {
name
}
}
}
}
```

The next step is implementing these queries in your `gatsby-node.js`
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

```javascript:title=gatsby-node.js
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved
const blogPost = path.resolve(`./src/templates/blog-post.js`)
const result = await graphql(`
query GetBlogPosts {
allBlogpost(sort: { fields: flotiqInternal___createdAt, order: DESC }) {
edges {
node {
headerImage {
extension
id
}
content
id
slug
title
}
}
}
}
`)

if (result.errors) {
throw result.errors
}

// Create blog posts pages.
const posts = result.data.allBlogpost.edges
```

Now the only thing you have to do is let Gatsby create appropriate pages for your Content Objects:
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

```javascript:title=gatsby-node.js
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node

createPage({
path: post.node.slug,
component: blogPost,
context: {
slug: post.node.slug,
previous,
next,
},
})
})
```

## Deployment

Once you're finished with your website there are several ways to deploy it. You can use any hosting provider you choose - Netlify, Heroku, AWS S3, Cloudflare, etc.

If you're using Gatsby Cloud - we've provided an integration that allows the to streamline your workflow by providing live updates in preview and push-button deployments.
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved

You can read the relevant [Flotiq Gatsby Cloud integration](https://flotiq.com/docs/panel/Plugins/Gatsby-cloud-integration/) documentation page to learn more.

## Summary

We've gone through the key points of setting up a simple Gatsby starter, configuring it to work with Flotiq and kicking off your development process. Remember to check out other [Flotiq Gatsby starters](https://github.com/flotiq/?q=gatsby-starter) that we've prepared to kick start your next project and come join the [Flotiq Discord channel](https://discord.gg/FwXcHnX) if you need any help!
andrzejwp marked this conversation as resolved.
Show resolved Hide resolved