A simple example to run a ApolloServer with subscriptions and authorization. The logic in this example contains:
- login functionality using
graphql-passport
- message publication using subscriptions
- filtering subscriptions
- checking subscriptions for valid credentials
For running in any NodeJS environment you have two options:
npm run start
- runsts-node
and starts the applicationnpm run dev
- runsts-node-dev
and allows you to do changes in the application with automated respawns
The simplest aproach is to just run:
docker-compose up
Package also contains a Dockerfile that can be used to isolate the app. The code is inspired by: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
Build using (use sudo if you haven't added your user to the Docker group - not that adding may be a security issue):
docker build -t <your username>/subscription-app .
To run just write (the --rm
makes it easier to clean up the container):
docker run --name subapp --rm -p 49160:4000 -d <your username>/subscription-app
Additional useful commands:
# Get container ID
$ docker ps
# Print app output
$ docker logs <container id or name ('subapp' if used above run)>
# Enter the container
$ docker exec -it <container id or name ('subapp' if used above run)> /bin/bash
Then you can connect to it on localhost:4000/graphql and run two simultaneous graphql queries in multiple tabs. Note Once you enter a subscription the context will never change, thus we need to first login:
mutation {
login(email: "john@doe.com", password: "pwd123") {
name
}
}
Then in a new tab, start a subscription:
subscription {
newMessage
}
The third tab should do a mutation for publishing to the subscription-tab:
mutation {
addMessage(message: "Testing to send a subscription message")
}
After running the mutation you should see "Testing to send a subscription message" in the subscription tab:
{
"data": {
"newMessage": "Testing to send a subscription message"
}
}
To test the user login functionality with multiple users you can start an incognito/private window or an alternative browser, load up the playground and see how messages are sent between users.