|
| 1 | +# GraphQL Client Integration Guide |
| 2 | + |
| 3 | +This directory contains ready-to-use GraphQL client programs for consuming the DotNet API Boilerplate GraphQL endpoint. Both frontend (TypeScript) and backend (.NET Core) clients are provided, supporting queries, mutations, and subscriptions. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. TypeScript Client (Web/Mobile/Framework Agnostic) |
| 8 | + |
| 9 | +**File:** `graphql-client.ts` |
| 10 | + |
| 11 | +### Features |
| 12 | +- Connects to GraphQL endpoint via HTTP and WebSocket (`/graphql`, `/graphql-ws`) |
| 13 | +- Supports queries, mutations, and subscriptions |
| 14 | +- JWT authentication placeholder (add your token if needed) |
| 15 | +- Framework-agnostic, works with any TypeScript/JavaScript app |
| 16 | + |
| 17 | +### Setup |
| 18 | +1. Install dependencies: |
| 19 | + ```sh |
| 20 | + npm install graphql-ws |
| 21 | + # or |
| 22 | + yarn add graphql-ws |
| 23 | + ``` |
| 24 | +2. Copy `graphql-client.ts` into your project. |
| 25 | +3. Update endpoint URLs and authentication token as needed. |
| 26 | + |
| 27 | +### Usage Example |
| 28 | +```typescript |
| 29 | +import { subscribeNewBlog, getBlogs, createBlog } from './graphql-client'; |
| 30 | + |
| 31 | +// Subscribe to new blog creation |
| 32 | +subscribeNewBlog(); |
| 33 | + |
| 34 | +// Query all blogs |
| 35 | +getBlogs().then(console.log); |
| 36 | + |
| 37 | +// Create a new blog |
| 38 | +createBlog({ name: 'Tech Blog', description: 'A blog about tech', authorName: 'John Doe', tags: ['tech'] }).then(console.log); |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 2. .NET Console Client (Backend/API Integration) |
| 44 | + |
| 45 | +**File:** `GraphQLConsoleClient.cs` |
| 46 | + |
| 47 | +### Features |
| 48 | +- Connects to GraphQL endpoint via HTTP and WebSocket (`/graphql`, `/graphql-ws`) |
| 49 | +- Supports queries, mutations, and subscriptions |
| 50 | +- JWT authentication placeholder (add your token if needed) |
| 51 | +- Console app, can be used as a service or starter for integration |
| 52 | + |
| 53 | +### Setup |
| 54 | +1. Create a new .NET console project: |
| 55 | + ```sh |
| 56 | + dotnet new console -n GraphQLConsoleClient |
| 57 | + cd GraphQLConsoleClient |
| 58 | + ``` |
| 59 | +2. Add NuGet packages: |
| 60 | + ```sh |
| 61 | + dotnet add package GraphQL.Client |
| 62 | + dotnet add package GraphQL.Client.Serializer.Newtonsoft |
| 63 | + ``` |
| 64 | +3. Copy `GraphQLConsoleClient.cs` into your project and replace `Program.cs`. |
| 65 | +4. Update endpoint URLs and authentication token as needed. |
| 66 | + |
| 67 | +### Usage Example |
| 68 | +Run the console app: |
| 69 | +```sh |
| 70 | + dotnet run |
| 71 | +``` |
| 72 | +- Prints queried posts |
| 73 | +- Prints mutation result for creating a post |
| 74 | +- Prints new post data as received via subscription |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Endpoints |
| 79 | +- **HTTP:** `http://localhost:8000/graphql` |
| 80 | +- **WebSocket:** `ws://localhost:8000/graphql-ws` |
| 81 | + |
| 82 | +## Authentication |
| 83 | +- No authentication required by default |
| 84 | +- Add JWT token to `AUTH_TOKEN` variable if needed |
| 85 | + |
| 86 | +## Supported Operations |
| 87 | +- **Queries:** Get blogs, get posts, etc. |
| 88 | +- **Mutations:** Create blog, create post, etc. |
| 89 | +- **Subscriptions:** On blog created, on post created, etc. |
| 90 | + |
| 91 | +## Customization |
| 92 | +- Update queries, mutations, and subscriptions as per your schema |
| 93 | +- Add error handling, logging, and UI integration as needed |
| 94 | + |
| 95 | +## Troubleshooting |
| 96 | +- Ensure the API server is running and accessible |
| 97 | +- Check endpoint URLs and ports |
| 98 | +- For subscriptions, ensure WebSocket support is enabled on the server |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +For further help, see the main project README or contact the API maintainer. |
0 commit comments