Coder Social home page Coder Social logo

gatsby-plugin-cockpit's Introduction

gatsby-plugin-cockpit

This source plugin pulls Cockpit Headless CMS data to aGatsbyJS, a static site generator for React.

Resources

Install

npm install --save gatsby-plugin-cockpit

##How to use

Add the following to gatsby-config.js

  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `src`,
        path: `${__dirname}/src/`,
      },
    },
    {
      resolve: 'gatsby-plugin-cockpit',
      options: {
        cockpitConfig: {
          baseURL: 'http://localhost:8888',
          folder: '/cockpit',
          accessToken: '4d659efb084077fd24aeb4871d4386',
          collections: ['posts'],
          regions: ['footer'],
          availableLngs: ['en', 'fr'],
          customComponents: [],
        },
      },
    },
  ],

Options

Option Default Description
baseURL required none The url to you Cockpit installation
folder optional '' The folder of your Cockpit installation
accessToken required none A valid API access token to you cockpit installation. See the Cockpit Documentation
collections optional [] The specific Cockpit collections you want to fetch. If empty all collections will be fetched.
regions optional [] The specific Cockpit regions you want to fetch. If empty all regions will be fetched.
availableLngs optional [] If you have set locale in the cockpit setting, set the keys here and a new field lang will be added for you to filter items in different language.
customComponents optional [] If you have defined some custom components for you cockpit layout fields and want them to be parsed for image fields.

How to Query

You can query collections, regions and assets.

Query Collections

Collections are converted into nodes. For example a posts collections is transformed into post nodes. So you can you allPost and post in your GraphQL queries.

allPost {
    edges {
        nodes {
            id
            ...
        }
    }
}

Query Assets

Assets are converted into Gatsby filesystem files and can be fetched with allFile or file.

allFile {
    edges {
        nodes {
            id
            publicURL
            ...
        }
    }
}

Query Regions

Regions are available in GraphQL as region nodes. So if you have a footer region in Cockpit with a col1 and a col2 field you can use following GraphQL query:

footer: region(name: { eq: "footer" }) {
	values {
		col1
		col2
	}
}

Query Child Nodes From Cockpit Fields

The Gatsby CollectionLink, Asset and Image fields will be parsed and transform into corresponding GraphQL nodes.

CollectionLink Field

The CollectionLink field links a collection to another collection. Here an example with players and teams collections.

allPlayer {
	edges {
        nodes {
            name
            role
            team {
                name
            }            
        }
	}
}

###Asset Field

The asset field gets extended with a localFile attribute linking to the corresponding file node.

For example, if you have a candidates collection with a name and a pdf as an asset field. So You can get the PDF URL with following GraphQL query:

allCandidate {
	edges {
        nodes {
            name
            pdf {
            	localFile {
	                publicURL                    
            	}
            }            
        }
	}
}

Image Field

Like the asset field, the image field gets extended with a localFile attribute linking to the corresponding file node.

Given a post collection with title, content and preview. You can use gatsby-image plugin in your GraphQL query and take advantage of Gatsby's image processing features.

allPost {
	edges {
        nodes {
            title
            content
            preview {
            	localFile {
                    childImageSharp {
                        sizes(maxWidth: 2000, quality: 90) {
                            ...GatsbyImageSharpSizes_withWebp
                        }
                    }                    
            	}
            }            
        }
	}
}

Layout Field

The layout field enables us to visually arrange components. It is even possible to define custom components corresponding to React Components we will us in Gatsby. This may be done with the LayoutComponents addon.

Given a page collection with title and content. You can do the following query:

allPage {
	edges {
        nodes {
            title
            content {
            	component
		        settings {
                    # component fields
                    ...
		        }
            }    
            content_parsed
            content_files {
                id
                publicURL
                # normal file node
            }
        }
	}
}
  • fieldname gives you the components with their settings
  • fieldname_parsed gives you the the same than field name but in an object. It enables you to ignore the type of components in the query and just getting all the component settings.
  • fieldname_files gives you all the file nodes present in the layout.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.