From 3e0fc96c0ee5ab37adbd93758e2f6b8b860f7fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Mila=C5=A1inovi=C4=87?= Date: Tue, 12 May 2026 20:41:06 +0200 Subject: [PATCH] GraphQL bugfix + query samples --- .../SetupGraphQL/Mutations.Event.cs | 2 +- .../SetupGraphQL/Queries.cs | 2 +- .../Events.GraphQLServer/appsettings.json | 2 +- Events.GraphQL/graphql-cheat-sheet.md | 96 +++++ Events.GraphQL/graphql-query-examples.md | 392 ++++++++++++++++++ 5 files changed, 491 insertions(+), 3 deletions(-) create mode 100644 Events.GraphQL/graphql-cheat-sheet.md create mode 100644 Events.GraphQL/graphql-query-examples.md diff --git a/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Mutations.Event.cs b/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Mutations.Event.cs index b15d608..3816b95 100644 --- a/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Mutations.Event.cs +++ b/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Mutations.Event.cs @@ -1,4 +1,4 @@ -using Events.EF.Data.MSSQL; +using Events.EF.Data.Postgres; using Events.EF.Models; namespace GraphQLServer.SetupGraphQL; diff --git a/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs b/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs index 4f7d5f0..87388e8 100644 --- a/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs +++ b/Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs @@ -1,4 +1,4 @@ -using Events.EF.Data.MSSQL; +using Events.EF.Data.Postgres; using Events.EF.Models; using Microsoft.EntityFrameworkCore; diff --git a/Events.GraphQL/Events.GraphQLServer/appsettings.json b/Events.GraphQL/Events.GraphQLServer/appsettings.json index d3d136c..7436834 100644 --- a/Events.GraphQL/Events.GraphQLServer/appsettings.json +++ b/Events.GraphQL/Events.GraphQLServer/appsettings.json @@ -6,7 +6,7 @@ } }, "ConnectionStrings": { - "EventsMssql": "Data Source=.,1433;Initial Catalog=Events;User Id=sport;Password=go and look in the secrets file;TrustServerCertificate=True" + "EventsPostgres": "Data Source=.,1433;Initial Catalog=Events;User Id=sport;Password=go and look in the secrets file;TrustServerCertificate=True" }, "AllowedHosts": "*" } diff --git a/Events.GraphQL/graphql-cheat-sheet.md b/Events.GraphQL/graphql-cheat-sheet.md new file mode 100644 index 0000000..d9d048c --- /dev/null +++ b/Events.GraphQL/graphql-cheat-sheet.md @@ -0,0 +1,96 @@ +# GraphQL Cheat Sheet + +Use endpoint: `/graphql` + +## Available queries + +- `countries` +- `sports` +- `people` +- `peoplePage(pageNumber, pageSize)` +- `events` +- `eventsForDate(date)` +- `registrations` + +## Common arguments + +- paging: `first`, `after`, `last`, `before` +- filtering: `where` +- sorting: `order` + +## Quick examples + +### Get events + +```graphql +query { + events(first: 5) { + nodes { + id + name + eventDate + } + } +} +``` + +### Filter people by country + +```graphql +query { + people(where: { countryCode: { eq: "HR" } }, first: 10) { + nodes { + id + firstName + lastName + } + } +} +``` + +### Sort sports by name + +```graphql +query { + sports(order: [{ name: ASC }], first: 10) { + nodes { + id + name + } + } +} +``` + +### Events for a specific date + +```graphql +query { + eventsForDate(date: "2026-06-15", first: 10) { + nodes { + id + name + } + } +} +``` + +### Classic paging with `peoplePage` + +```graphql +query { + peoplePage(pageNumber: 1, pageSize: 5) { + totalCount + items { + id + firstName + lastName + } + } +} +``` + +## Mutations + +- `addEvent(input)` +- `updateEvent(id, input)` +- `deleteEvent(id)` diff --git a/Events.GraphQL/graphql-query-examples.md b/Events.GraphQL/graphql-query-examples.md new file mode 100644 index 0000000..6c36c7c --- /dev/null +++ b/Events.GraphQL/graphql-query-examples.md @@ -0,0 +1,392 @@ +# GraphQL Query Examples + +The project exposes the GraphQL endpoint at `/graphql`, and in development GraphiQL is available at `/`. + +## Defined queries + +The following queries are defined in `Queries.cs`: + +- `countries` +- `sports` +- `people` +- `peoplePage(pageNumber, pageSize)` +- `events` +- `eventsForDate(date)` +- `registrations` + +The `countries`, `sports`, `people`, `events`, `eventsForDate`, and `registrations` queries support: + +- paging via `first`, `after`, `last`, `before` +- filtering via the `where` argument +- sorting via the `order` argument +- `totalCount` because it is enabled in paging options + +## Basic examples + +### 1. Get countries + +```graphql +query { + countries(first: 10) { + totalCount + nodes { + code + alpha3 + name + } + } +} +``` + +### 2. Get sports + +```graphql +query { + sports(first: 10, order: [{ name: ASC }]) { + nodes { + id + name + } + } +} +``` + +### 3. Get people + +```graphql +query { + people(first: 10) { + totalCount + nodes { + id + firstName + lastName + email + city + birthDate + countryCode + } + } +} +``` + +### 4. Get events + +```graphql +query { + events(first: 10, order: [{ eventDate: DESC }]) { + nodes { + id + name + eventDate + } + } +} +``` + +### 5. Get registrations + +```graphql +query { + registrations(first: 10) { + nodes { + id + registeredAt + personId + sportId + eventId + } + } +} +``` + +## Filtering and sorting examples + +### 6. Countries whose name contains "Bos" + +```graphql +query { + countries( + first: 10 + where: { name: { contains: "Bos" } } + order: [{ name: ASC }] + ) { + nodes { + code + name + } + } +} +``` + +### 7. People from a specific country + +```graphql +query { + people( + first: 10 + where: { countryCode: { eq: "HR" } } + order: [{ lastName: ASC }, { firstName: ASC }] + ) { + totalCount + nodes { + id + firstName + lastName + city + countryCode + } + } +} +``` + +### 8. People born after a given date + +```graphql +query { + people( + first: 10 + where: { birthDate: { gt: "2000-01-01" } } + order: [{ birthDate: DESC }] + ) { + nodes { + id + firstName + lastName + birthDate + } + } +} +``` + +### 9. Events for an exact date + +```graphql +query { + eventsForDate(date: "2026-06-15", first: 20) { + totalCount + nodes { + id + name + eventDate + } + } +} +``` + +### 10. Events whose name contains a word + +```graphql +query { + events( + first: 10 + where: { name: { contains: "Open" } } + order: [{ eventDate: ASC }] + ) { + nodes { + id + name + eventDate + } + } +} +``` + +### 11. Registrations with nested data + +```graphql +query { + registrations(first: 10, order: [{ registeredAt: DESC }]) { + nodes { + id + registeredAt + person { + id + firstName + lastName + } + sport { + id + name + } + event { + id + name + eventDate + } + } + } +} +``` + +## Paging examples + +### 12. `pageInfo` for cursor paging + +```graphql +query { + people(first: 5, order: [{ id: ASC }]) { + totalCount + pageInfo { + hasNextPage + endCursor + } + nodes { + id + firstName + lastName + } + } +} +``` + +### 13. Next page using a cursor + +```graphql +query { + people(first: 5, after: "PUT_END_CURSOR_HERE") { + pageInfo { + hasNextPage + endCursor + } + nodes { + id + firstName + lastName + } + } +} +``` + +### 14. Custom page query `peoplePage` + +This query does not use cursor paging. It uses classic `pageNumber` and `pageSize`. + +```graphql +query { + peoplePage(pageNumber: 2, pageSize: 5) { + totalCount + pageNumber + pageSize + items { + id + firstName + lastName + email + } + } +} +``` + +## Examples with variables + +### 15. `eventsForDate` with variables + +```graphql +query EventsForDate($date: Date!) { + eventsForDate(date: $date, first: 20) { + nodes { + id + name + eventDate + } + } +} +``` + +Variables: + +```json +{ + "date": "2026-06-15" +} +``` + +### 16. `peoplePage` with variables + +```graphql +query PeoplePage($pageNumber: Int!, $pageSize: Int!) { + peoplePage(pageNumber: $pageNumber, pageSize: $pageSize) { + totalCount + pageNumber + pageSize + items { + id + firstName + lastName + } + } +} +``` + +Variables: + +```json +{ + "pageNumber": 1, + "pageSize": 10 +} +``` + +## Existing mutations + +Even though this document focuses on queries, the project also defines these mutations for the `Event` entity: + +- `addEvent(input)` +- `updateEvent(id, input)` +- `deleteEvent(id)` + +### 17. Add an event + +```graphql +mutation { + addEvent(input: { name: "Street Basketball 2026", eventDate: "2026-07-01" }) { + id + name + eventDate + } +} +``` + +### 18. Update an event + +```graphql +mutation { + updateEvent( + id: 1 + input: { name: "Street Basketball 2026", eventDate: "2026-07-02" } + ) { + id + name + eventDate + } +} +``` + +### 19. Delete an event + +```graphql +mutation { + deleteEvent(id: 1) +} +``` + +## Fields by type + +Useful fields visible from the models: + +- `Country`: `code`, `alpha3`, `name`, `translations` +- `Sport`: `id`, `name` +- `Person`: `id`, `firstName`, `lastName`, `firstNameTranscription`, `lastNameTranscription`, `addressLine`, `postalCode`, `city`, `addressCountry`, `email`, `contactPhone`, `birthDate`, `documentNumber`, `countryCode` +- `Event`: `id`, `name`, `eventDate` +- `Registration`: `id`, `personId`, `sportId`, `eventId`, `registeredAt` + +## Source + +Examples are based on the implementation in: + +- `Events.GraphQLServer/SetupGraphQL/Queries.cs` +- `Events.GraphQLServer/SetupGraphQL/Mutations.Event.cs` +- `Events.GraphQLServer/SetupGraphQL/PeoplePage.cs` +- `Events.GraphQLServer/SetupGraphQL/EventInput.cs` +- `Events.EF/Models/*.cs`