Skip to content

Commit

Permalink
feat(gatsby): serve Admin from develop parent process (#23734)
Browse files Browse the repository at this point in the history
* Server static admin app from parent process

* Start Recipes GraphQL server when running develop and fetch fresh port in admin

* Setup gatsby-inteface and strict-ui

* Build gatsby admin navbar

* Proper headings on the dash

* Style rest of dashboard

* Add return type to createUrqlClient

* Update gatsby admin running instructions

* Add instructions to replace ___loader with window.___loader

* Remove unused components

* Bump strict-ui and test local site dev

* Add GATSBY_EXPERIMENTAL_ENABLE_ADMIN feature flag

* Resolve type issue by pinning version of csstype to 2.6.10

* Fix package.json for gatsby-admin package

* Update packages/gatsby-admin/README.md

Co-authored-by: Lennart <lekoarts@gmail.com>

* Remove built-in plugin

* Add some more technical docs

* Fix prettier

Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
mxstbr and LekoArts committed May 30, 2020
1 parent 9b077c6 commit a6eb21b
Show file tree
Hide file tree
Showing 16 changed files with 1,002 additions and 115 deletions.
6 changes: 3 additions & 3 deletions docs/blog/2020-05-29-gazette/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In May we also announced our [series B round of funding round](/blog/2020-05-27-

Your content editors can now enjoy “instant preview” with Strapi and Gatsby Cloud! If you're new to Strapi, it’s a JavaScript-based, open source CMS. and a great pair for Gatsby projects. The Gatsby starter the Strapi team made for this project is gorgeous 😍 .

![Strapi blog screenshot](/strapi-blog.png)
![Strapi blog screenshot](/strapi-blog.png)

Give Strapi a try with [this step-by-step tutorial](<(/blog/2020-05-12-strapi-instant-content-preview-plugin/)>).

Expand Down Expand Up @@ -65,7 +65,7 @@ This month we made the Gatsby Typescript plugin part of core Gatsby, so you no l

In may we continued to improve our error messaging (see example [here](https://github.com/gatsbyjs/gatsby/pull/24186) and [here](https://github.com/gatsbyjs/gatsby/pull/23741)). But what about slow GraphQL queries? Well, Gatsby now supports performance tracing using the opentracing standard. You can to [enable tracing for GraphQL queries](/docs/performance-tracing/). This is useful because it allows you to debug why querying may be slow in your Gatsby project.

## 👩‍🚀 New in the Gatsby Community
## 👩‍🚀 New in the Gatsby Community

### Gatsby Days is June 2 - 3

Expand Down Expand Up @@ -103,7 +103,7 @@ Thank you **Akuoko Daniel Jnr** for helpful your article, ["Increasing Website P

And special thanks to our long time community member, **Horacio Herrera** for making a site to [teach Gatsby to Web Developers in Spanish](https://aprendegatsby.com/). We appreciate you!

## 🪐 New in the Gatsby Ecosystem
## 🪐 New in the Gatsby Ecosystem

### Gatsby Themes & Plugins

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
"unified": "^9.0.0",
"yargs": "^10.1.2"
},
"resolutions": {
"theme-ui": "0.4.0-alpha.3",
"csstype": "2.6.10"
},
"engines": {
"yarn": "^1.17.3",
"node": ">=10.13.0"
Expand Down
39 changes: 29 additions & 10 deletions packages/gatsby-admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@

A visual interface to configure your Gatsby site.

We have not packaged this nicely yet, so it is not installable.
## Development

## How to develop it
The Admin app itself is a standard Gatsby site. It fetches its data from the Recipes GraphQL server, which exposes all the information we need about the Gatsby site.

However, you can do some manual set up in order to work with it locally. Follow these steps:
It uses [theme-ui](https://theme-ui.com) (with the [strict-ui](https://github.com/system-ui/theme-ui/pull/719) experiment) and [gatsby-interface](https://github.com/gatsby-inc/gatsby-interface) to style the app and [urql](https://github.com/FormidableLabs/urql) to fetch the data from the GraphQL server.

1. Navigate to the monorepo and open the `packages/gatsby-admin` directory.
2. In that directory, run `yarn develop`.
> If you see eslint errors you'll need to temporarily replace all references to `___loader` with `window.___loader` inside of `gatsby-link/index.js`.
3. In a new tab, navigate to a Gatsby site of your choice (or create one) that runs the latest version of Gatsby (recipes are a requirement).
4. From the `packages/gatsby-recipes/src` directory in the monorepo copy the `create-types.js` and `graphql.js` files. Use these files to replace those currently in your site's `node_modules/gatsby-recipes/src` directory.
5. Run `node ./node_modules/gatsby-recipes/src/graphql.js` to start the Recipes GraphQL server for that site.
### Running it locally

You should now be able to visit `localhost:8000` to see Gatsby Admin for that site!
The easiest way to work on Admin locally is to develop Admin itself.

1. Make sure to have the dependencies installed by running `yarn` in the root folder
2. Start the develop process for the Admin site by running `yarn workspace gatsby-admin run develop`

> If you see eslint errors you'll need to temporarily replace all references to `___loader` with `window.___loader` in `packages/gatsby-link/index.js`.
### Running it on a local site

To try Admin with one of your sites locally, use the `gatsby-dev-cli` to copy the local versions of `gatsby`, `gatsby-cli`, `gatsby-recipes`, `gatsby-core-utils` and `gatsby-admin` into your project:

```sh
# Make sure to build the latest local versions of all packages
~/gatsby
yarn run watch

~/my-gatsby-site
$ gatsby-dev --packages gatsby gatsby-cli gatsby-recipes gatsby-core-utils gatsby-admin

# In another tab, start your site with the Admin feature flag set
~/my-gatsby-site
$ GATSBY_EXPERIMENTAL_ENABLE_ADMIN=true gatsby develop
```

Then visit `localhost:8000/\_\_\_admin` and you should see Gatsby Admin for your site!
4 changes: 2 additions & 2 deletions packages/gatsby-admin/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Providers from './src/components/providers'
import Layout from './src/components/layout'

export const wrapPageElement = ({ element,props }) =>(
<Providers {...props}>{element}</Providers>
<Layout {...props}>{element}</Layout>
)
3 changes: 2 additions & 1 deletion packages/gatsby-admin/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
plugins: [`gatsby-plugin-typescript`]
plugins: [],
pathPrefix: `/___admin`
}
1 change: 1 addition & 0 deletions packages/gatsby-admin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// NOOP
22 changes: 19 additions & 3 deletions packages/gatsby-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@
"main": "index.js",
"author": "Max Stoiber",
"license": "MIT",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-admin"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-admin#readme",
"dependencies": {
"@emotion/core": "^10.0.28",
"@emotion/styled": "^10.0.27",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"csstype": "^2.6.10",
"formik": "^2.1.4",
"gatsby": "^2.22.12",
"gatsby-interface": "0.0.163",
"gatsby-plugin-typescript": "^2.4.3",
"gatsby-source-graphql": "^2.5.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-icons": "^3.10.0",
"subscriptions-transport-ws": "^0.9.16",
"strict-ui": "^0.1.2",
"theme-ui": "^0.4.0-alpha.3",
"typescript": "^3.9.2",
"urql": "^1.9.7"
"urql": "^1.9.7",
"yup": "^0.29.0"
},
"scripts": {
"develop": "gatsby develop"
"develop": "gatsby develop",
"build": "gatsby build --prefix-paths",
"watch": "nodemon --watch src --ext js,ts,tsx,json --exec \"yarn run build\""
}
}
15 changes: 15 additions & 0 deletions packages/gatsby-admin/src/components/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @jsx jsx */
import { jsx, Flex } from "strict-ui"
import Providers from "./providers"
import Navbar from "./navbar"

const Layout: React.FC<{}> = ({ children }) => (
<Providers>
<Flex gap={0} flexDirection="column">
<Navbar />
{children}
</Flex>
</Providers>
)

export default Layout
58 changes: 58 additions & 0 deletions packages/gatsby-admin/src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** @jsx jsx */
import { jsx, Flex } from "strict-ui"
import { Text, BaseAnchor } from "gatsby-interface"
import { useQuery } from "urql"

const Navbar: React.FC<{}> = () => {
const [{ data }] = useQuery({
query: `
{
npmPackageJson(id: "name") {
value
}
}
`,
})

return (
<Flex
justifyContent="space-between"
alignItems="center"
sx={{
backgroundColor: `grey.90`,
borderBottom: `default`,
paddingX: 6,
paddingY: 5,
}}
>
<Flex gap={5} alignItems="center">
<Text sx={{ color: `white` }}>Gatsby Admin</Text>
{data && data.npmPackageJson && (
<div
sx={{
width: `1px`,
height: `16px`,
backgroundColor: `grey.40`,
}}
/>
)}
{data && data.npmPackageJson && (
<Text sx={{ color: `teal.50` }}>
{data.npmPackageJson.value.replace(/^"|"$/g, ``)}
</Text>
)}
</Flex>
<Flex alignItems="center">
<BaseAnchor
href={`/`}
target="_blank"
sx={{ color: `whiteFade.60`, textDecoration: `none` }}
>
Visit site
</BaseAnchor>
</Flex>
</Flex>
)
}

export default Navbar
64 changes: 60 additions & 4 deletions packages/gatsby-admin/src/components/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
import React from "react"
import { Provider } from "urql"
import client from "../urql-client"
import { Provider, Client } from "urql"
import { ThemeProvider, getTheme } from "gatsby-interface"
import { ThemeProvider as StrictUIProvider } from "strict-ui"
import { createUrqlClient } from "../urql-client"

export default ({ children }): React.ReactElement => (
<Provider value={client}>{children}</Provider>
const baseTheme = getTheme()

const theme = {
...baseTheme,
colors: {
...baseTheme.colors,
background: baseTheme.colors.grey[90],
},
fontWeights: {
...baseTheme.fontWeights,
"500": 500,
},
borders: {
default: `1px solid ${baseTheme.colors.whiteFade[20]}`,
},
sizes: {
// NOTE(@mxstbr): Hacks around issues with strict-ui that I have to fix upstream eventually
"1px": `1px`,
"100%": `100%`,
"16px": `16px`,
initial: `initial`,
},
}

const GraphQLProvider: React.FC<{}> = ({ children }) => {
const [status, setStatus] = React.useState(`loading`)
const [client, setClient] = React.useState<Client | null>(null)

React.useEffect(() => {
setStatus(`loading`)
fetch(`/___services`)
.then(res => res.json())
.then(json => {
setStatus(`idle`)
if (json.recipesgraphqlserver) {
setClient(createUrqlClient({ port: json.recipesgraphqlserver.port }))
}
})
}, [])

if (status === `loading`) return <p>Loading...</p>

if (client === null)
return <p>It looks like no develop process is running.</p>

return <Provider value={client}>{children}</Provider>
}

const Providers: React.FC<{}> = ({ children }) => (
<StrictUIProvider theme={theme}>
<ThemeProvider theme={theme}>
<GraphQLProvider>{children}</GraphQLProvider>
</ThemeProvider>
</StrictUIProvider>
)

export default Providers

0 comments on commit a6eb21b

Please sign in to comment.