|
| 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