Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"import/no-named-as-default": "off"
},
"overrides": [
{
"files": ["packages/example-graphql-events/*.ts"],
"env": {
"node": true
}
},
{
"files": ["*.spec.ts"],
"env": {
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
on: pull_request
name: Test examples

jobs:
build-example-graphql-events:
name: Build Example GraphQL Events
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v4
with:
node-version: 20.x
cache: 'yarn'
- name: Install Deps
run: yarn install --immutable
- name: Build
run: yarn workspace qminder-graphql-events-example build
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
"scripts": {
"test": "jest",
"test-node": "jest --env=node",
"lint-eslint": "eslint --ext ts .",
"lint-eslint": "yarn workspace qminder-api build && eslint --ext ts .",
"lint-prettier": "prettier --check .",
"lint": "yarn lint-eslint && yarn lint-prettier",
"format": "prettier --write .",
"docs": "sh ./scripts/build-documentation.sh",
"prepack": "yarn run build",
"build": "yarn workspace qminder-api build",
"watch": "yarn workspace qminder-api watch",
"example-new-visitors": "ts-node examples/events/new-visitors.ts"
"example-graphql-events": "yarn workspace qminder-api build && yarn workspace qminder-graphql-events-example start"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^8.0.0",
Expand All @@ -26,7 +26,8 @@
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-import": "^2.27.5",
"prettier": "2.8.8"
"prettier": "2.8.8",
"typescript": "5.5.4"
},
"workspaces": [
"packages/*"
Expand Down
11 changes: 11 additions & 0 deletions packages/example-graphql-events/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Example of GraphQL events

This simple example script demonstrates how to consume ticket creation events using the GraphQL subscription API.

## Usage

```bash
export API_KEY=XXX
yarn
yarn example-graphql-events
```
69 changes: 69 additions & 0 deletions packages/example-graphql-events/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Qminder } from 'qminder-api';

interface Location {
id: string;
name: string;
}

interface Ticket {
id: string;
firstName: string;
lastName: string;
}

interface TicketCreatedEvent {
createdTickets: Ticket;
}

async function findFirstLocationId(): Promise<Location> {
const result: any = await Qminder.GraphQL.query(`{
locations {
id
name
}
}`);

if (result.errors) {
throw new Error(
`Failed to find locations. Errors: ${JSON.stringify(result.errors)}`,
);
}

if (result.data.locations.length < 1) {
throw new Error('Account does not have any locations');
}

console.log(`Found ${result.data.locations.length} locations`);
return result.data.locations[0];
}

async function listenForTickets() {
if (!process.env.API_KEY) {
console.error('Please set API key first by executing "export API_KEY=XXX"');
console.error(
'More information: https://developer.qminder.com/reference/overview',
);
throw new Error('API key is not set');
}

Qminder.setKey(process.env.API_KEY);

console.log('Example of GraphQL events in Qminder');

let location = await findFirstLocationId();
console.log(`Listening for new visitors in: ${location.name}`);

const observable = Qminder.GraphQL
.subscribe(`subscription { createdTickets(locationId: ${location.id}) {
id
firstName
lastName
}
}`);

observable.subscribe((event: TicketCreatedEvent) =>
console.log(`New visitor: ${JSON.stringify(event.createdTickets)}`),
);
}

listenForTickets();
17 changes: 17 additions & 0 deletions packages/example-graphql-events/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "qminder-graphql-events-example",
"version": "1.0.0",
"description": "Example of using GraphQL events",
"main": "app.ts",
"scripts": {
"build": "yarn workspace qminder-api build && tsc -p .",
"start": "yarn build && tsx app.ts",
"example-graphql-events": "yarn start"
},
"devDependencies": {
"tsx": "^4.19.2",
"typescript": "5.5.4"
},
"author": "Qminder <support@qminder.com> (https://www.qminder.com)",
"license": "Apache-2.0"
}
20 changes: 20 additions & 0 deletions packages/example-graphql-events/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "es2020",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es2020",
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"outDir": "build",
"rootDir": "./",
"declaration": true
},
"include": ["app.ts"],
"files": ["app.ts"]
}
Loading
Loading