Skip to content

Commit 37ebe3f

Browse files
committed
refactor: graphql fetching with template hierarchy and implement in astro example
1 parent eea37b4 commit 37ebe3f

23 files changed

Lines changed: 1089 additions & 294 deletions

File tree

examples/astro/template-hierarchy/astro.config.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,23 @@ export default defineConfig({
99
'@': '/src',
1010
},
1111
},
12+
server: {
13+
watch: {
14+
// Explicitly watch the packages directories
15+
ignored: [
16+
'**/node_modules/**',
17+
'!**/node_modules/@faustjs/**', // Watch @faustjs packages
18+
],
19+
followSymlinks: true,
20+
},
21+
},
22+
optimizeDeps: {
23+
// Don't pre-bundle workspace packages so changes are picked up immediately
24+
exclude: [
25+
'@faustjs/astro',
26+
'@faustjs/template-hierarchy',
27+
'@faustjs/graphql',
28+
],
29+
},
1230
},
1331
});

examples/astro/template-hierarchy/src/pages/[...uri].astro

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
---
2-
import { uriToTemplate } from '@faustjs/astro';
2+
import {
3+
uriToTemplate,
4+
createDefaultClient,
5+
setGraphQLClient,
6+
} from '@faustjs/astro';
7+
8+
// Set up GraphQL client using environment variable
9+
const client = createDefaultClient(import.meta.env.WORDPRESS_URL);
10+
setGraphQLClient(client);
311
412
// URI comes as a string from Astro params, ensure it starts with /
513
const { uri = '/' } = Astro.params;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@faustjs/nextjs-template-hierarchy-example",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "Example Next.js app using @faustjs/nextjs template hierarchy",
6+
"scripts": {
7+
"dev": "next dev",
8+
"build": "next build",
9+
"start": "next start",
10+
"lint": "next lint"
11+
},
12+
"dependencies": {
13+
"@faustjs/nextjs": "workspace:*",
14+
"@faustjs/template-hierarchy": "workspace:*",
15+
"next": "^14.0.0",
16+
"react": "^18.0.0",
17+
"react-dom": "^18.0.0"
18+
},
19+
"devDependencies": {
20+
"eslint": "^8.0.0",
21+
"eslint-config-next": "^14.0.0"
22+
}
23+
}

packages/astro/config.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

packages/astro/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,9 @@ export { uriToTemplate } from './templateHierarchy.js';
88
// Export template collection
99
export { createTemplateCollection, templateSchema } from './templateLoader.js';
1010

11-
// Export configuration utilities
11+
// Export GraphQL configuration utilities
1212
export {
1313
setGraphQLClient,
14-
getConfiguredClient,
15-
createDefaultClient,
16-
} from './config.js';
17-
18-
// TODO: Implement Astro integration
19-
export function faustjs(options = {}) {
20-
return {
21-
name: '@faustjs/astro',
22-
hooks: {
23-
// TODO: Add Astro integration hooks
24-
},
25-
};
26-
}
14+
getGraphQLClient,
15+
createDefaultGraphQLClient as createDefaultClient,
16+
} from '@faustjs/graphql';

packages/astro/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"node": ">=24"
2323
},
2424
"dependencies": {
25-
"@faustjs/template-hierarchy": "workspace:*"
25+
"@faustjs/template-hierarchy": "workspace:*",
26+
"@faustjs/graphql": "workspace:*",
27+
"graphql": "^16.8.1"
2628
},
2729
"peerDependencies": {
2830
"astro": "^5.0.0",

packages/astro/seedQuery.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/astro/templateHierarchy.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
*/
44

55
import { getCollection, getEntry } from 'astro:content';
6-
import { getTemplate, getPossibleTemplates } from '@faustjs/template-hierarchy';
7-
import { getSeedQuery } from './seedQuery.js';
6+
import {
7+
getTemplate,
8+
getPossibleTemplates,
9+
getSeedQuery,
10+
} from '@faustjs/template-hierarchy';
11+
import { getGraphQLClient } from '@faustjs/graphql';
12+
13+
console.log(
14+
'🚀 Astro templateHierarchy.js loaded at:',
15+
new Date().toISOString(),
16+
);
817

918
/**
1019
* Resolve a URI to template data using WordPress template hierarchy
@@ -21,7 +30,9 @@ export async function uriToTemplate({ uri, graphqlClient }) {
2130
template: undefined,
2231
};
2332

24-
const { data, error } = await getSeedQuery({ uri, graphqlClient });
33+
// Get the GraphQL client - use provided one or get configured one
34+
const client = getGraphQLClient(graphqlClient);
35+
const { data, error } = await getSeedQuery({ uri, graphqlClient: client });
2536

2637
returnData.seedQuery = { data, error };
2738

@@ -60,9 +71,12 @@ export async function uriToTemplate({ uri, graphqlClient }) {
6071
availableTemplates.map((template) => template.data.id),
6172
possibleTemplates,
6273
);
63-
returnData.template = await getEntry('templates', templateId)?.data;
6474

65-
if (!returnData.template) {
75+
const template = await getEntry('templates', templateId);
76+
77+
returnData.template = template.data;
78+
79+
if (!template) {
6680
console.error('No template found for route');
6781
}
6882

packages/astro/types.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@
88
* @property {(query: string, variables?: Record<string, any>) => Promise<{data?: any, error?: any}>} request - Execute a GraphQL query
99
*/
1010

11-
/**
12-
* Options for getSeedQuery function
13-
* @typedef {Object} SeedQueryOptions
14-
* @property {string} uri - The URI to query for
15-
* @property {GraphQLClient} [graphqlClient] - Optional GraphQL client to use
16-
*/
17-
1811
export {};

packages/graphql/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# @faustjs/graphql
2+
3+
Shared GraphQL utilities for FaustJS packages.
4+
5+
## Features
6+
7+
- Default GraphQL client using fetch
8+
- Global client configuration with fallback priority
9+
- WordPress URL utilities
10+
- Framework-agnostic GraphQL endpoint handling
11+
12+
## Installation
13+
14+
```bash
15+
npm install @faustjs/graphql
16+
# or
17+
pnpm add @faustjs/graphql
18+
# or
19+
yarn add @faustjs/graphql
20+
```
21+
22+
## Usage
23+
24+
### Basic Usage
25+
26+
```js
27+
import {
28+
setGraphQLClient,
29+
getGraphQLClient,
30+
createDefaultClient,
31+
} from '@faustjs/graphql';
32+
33+
// Create and set up client explicitly
34+
const client = createDefaultClient('https://my-wordpress-site.com');
35+
setGraphQLClient(client);
36+
37+
// Later, get the configured client
38+
const configuredClient = getGraphQLClient();
39+
40+
// Use client for your own queries
41+
const result = await configuredClient.request(yourQuery, variables);
42+
```
43+
44+
### Client Priority System
45+
46+
The GraphQL client uses a simple priority system:
47+
48+
1. **Provided Client**: Client passed directly to functions
49+
2. **Configured Client**: Client set globally with `setGraphQLClient()`
50+
3. **Error**: No automatic fallback - explicit setup required
51+
52+
This explicit approach ensures you know exactly which client is being used.
53+
54+
### Environment Variables
55+
56+
The package supports these environment variables:
57+
58+
- `WORDPRESS_URL` - Your WordPress site URL
59+
- Framework-specific variants (e.g., `NEXT_PUBLIC_WORDPRESS_URL`)
60+
61+
## API Reference
62+
63+
### `setGraphQLClient(client)`
64+
65+
Set a global GraphQL client.
66+
67+
### `createDefaultGraphQLClient(wordpressUrl)`
68+
69+
Create a default fetch-based GraphQL client. Requires explicit WordPress URL.
70+
71+
### `getGraphQLClient(providedClient)`
72+
73+
Get a GraphQL client using the priority system. Throws error if no client is available.
74+
75+
## License
76+
77+
MIT

0 commit comments

Comments
 (0)