Skip to content

chore: Add/nuxt gforms example #137

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
23 changes: 23 additions & 0 deletions examples/nuxt/nuxt-headlesswp-gravity-forms/wp-env/.wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"phpVersion": "7.4",
"plugins": [
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip",
"https://github.com/AxeWP/wp-graphql-gravity-forms/releases/download/v0.13.0.1/wp-graphql-gravity-forms.zip"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fran-A-Dev

Can we change this to https://github.com/AxeWP/wp-graphql-gravity-forms/releases/latest/download/wp-graphql-gravity-forms.zip as this will always get the latest version or would prefer to keep this to always version 13?

],
"config": {
"WP_DEBUG": true,
"SCRIPT_DEBUG": false,
"GRAPHQL_DEBUG": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": false,
"SAVEQUERIES": false
},
"mappings": {
"db": "./wp-env/db",
"wp-content/uploads": "./wp-env/uploads",
".htaccess": "./wp-env/setup/.htaccess"
},
"lifecycleScripts": {
"afterStart": "wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
}
}
106 changes: 106 additions & 0 deletions examples/nuxt/nuxt-headlesswp-gravity-forms/wp-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Example: Gravity Forms in Headless WordPress with Nuxt/Vue

This example shows you how to wire up a full headless WordPress backend—complete with Gravity Forms, WPGraphQL, and a pre-built "Questionnaire" form—alongside a Nuxt 3 front end that dynamically renders your forms using the GraphQL interfaces feature of WPGraphQL for Gravity Forms.

## Features

- **Headless WordPress with Gravity Forms**

- Stand up WP + WPGraphQL + Gravity Forms (and your Questionnaire JSON) in one command.

- **Dynamic form rendering**

- Use GraphQL interfaces (`GfFieldWithLabelSetting`, `GfFieldWithChoicesSetting`, etc.) plus a single `useFormFields` composable to automatically map any new field type to the correct Vue component.

- **Composable Nuxt 3 front end**

- Fetch your form schema with `useGravityForm` and render all fields via a dynamic `<component :is="resolveFieldComponent(field)" … />` pattern.

- **Out-of-the-box data**
- Includes a SQL dump of WP + Gravity Forms data and a zipped uploads folder so you can dive right in.

## Project Structure

```
├── components/ # Vue form-field components & barrel file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fran-A-Dev Nice 😊

├── composables/ # useGravityForm.js & useFormFields.js
├── pages/ # Nuxt page (index.vue) that renders the form
├── wp-env/
│ ├── db/
│ │ └── database.sql # WordPress + Gravity Forms schema & data
│ ├── setup/
│ │ └── .htaccess # CORS + pretty-permalinks for wp-env
│ └── uploads.zip # wp-content/uploads media files
├── .wp-env.json # @wordpress/env configuration
├── package.json # wp-env + Nuxt dev scripts
└── nuxt.config.ts # Nuxt application config
```

## Running the Example with wp-env

### Prerequisites

- Node.js (v18+ recommended)
- pnpm or npm/yarn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just assume npm. Maybe here it's fine but any commands should be shown with NPM.

- Docker (so that @wordpress/env can spin up the WP container)

### Setup Repository and Install

```bash
git clone https://github.com/your-org/nuxt-gravityforms-example.git
cd nuxt-gravityforms-example
pnpm install

echo "NUXT_PUBLIC_WORDPRESS_URL=http://localhost:8888" > .env
```

### Quick Start

1. Unzip uploads, start WP, import DB, then launch Nuxt:

```bash
pnpm example:build
```

2. Or run steps separately:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fran-A-Dev I really like this. @moonmeister We should also consider this for other examples when we rewrite them for npm


```bash
# Start WP
pnpm wp:start

# Import DB
pnpm wp:db:import

# Unzip uploads
pnpm wp:images:unzip

# Start Nuxt dev server
pnpm dev
```

By the end, you will have:

- WordPress Admin: http://localhost:8888/wp-admin/
- user: admin / pass: password
- Nuxt Front End: http://localhost:3000/

## Scripts

| Command | Description |
| ---------------------- | ---------------------------------------------------------------- |
| `pnpm example:build` | Unzip media → start WP env → import DB → launch Nuxt dev server |
| `pnpm example:start` | Start WP env, then Nuxt dev server |
| `pnpm example:stop` | Stop the WordPress environment (wp-env stop) |
| `pnpm example:prune` | Destroy & rebuild the WP environment, then restart—all in one go |
| `pnpm wp:start` | @wordpress/env start (launches PHP/MySQL container with WP) |
| `pnpm wp:stop` | @wordpress/env stop |
| `pnpm wp:db:import` | Import the SQL dump into the running WP container |
| `pnpm wp:db:export` | Export the current WP database back to wp-env/db/database.sql |
| `pnpm wp:images:unzip` | Clear & unzip wp-env/uploads.zip → populates wp-content/uploads |
| `pnpm dev` | Start the Nuxt 3 development server on port 3000 |

> **Tip:** You can also run any arbitrary WP-CLI command inside the container via `pnpm wp:cli -- <wp-cli-command>`

## Database Access

If you need direct database access (phpMyAdmin), add a `"phpmyadminPort": 11111` entry to your `.wp-env.json` and then navigate to http://localhost:11111.
18 changes: 18 additions & 0 deletions examples/nuxt/nuxt-headlesswp-gravity-forms/wp-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "headlesswp-gravity-forms-nuxt-env",
"version": "1.0.0",
"scripts": {
"wp:start": "wp-env start",
"wp:stop": "wp-env stop",
"wp:destroy": "wp-env destroy --config .wp-env.json",
"wp:db:import": "wp-env run cli -- wp db import /var/www/html/db/database.sql",
"wp:db:export": "wp-env run cli -- wp db export /var/www/html/db/database.sql",
"wp:images:unzip": "rm -rf ./wp-content/uploads && unzip uploads.zip -d wp-content/uploads",
"frontend:dev": "pnpm --prefix .. dev",
"start": "npm run wp:start && npm run frontend:dev"
},
"devDependencies": {
"@wordpress/env": "^10.20.0"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header set Access-Control-Allow-Headers "Authorization, Content-Type"
</IfModule>

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
Loading