Apollo server 2.0
This project is a good starting point for all the people that want to have apollo-server 2.0 ready to use. It integrates:
- queries
- mutations
- subscriptions (real-time data)
- authentication and authorisation (using google sign-on as provider server)
- schema stitching
- mongo db
- logger (it will create a file
error.log
and write on it in case of error - to change the level editcommon/util/logger.ts
lines 12, 13, 14 )
Installation:
-
install mongodb - refer to mongodb guide
-
npm install
Environment
In the root folder is available a file called .env
. It contains all the configuration environment for:
- mongodb
- google-sign-in as identity server for authentication and authorisation. More info available at http://www.dzurico.com/apollo-server-2-0-auth-with-google-signin/
available commands
- test environment:
npm run dev
(it will automatically rerun your code all the time you save and will check for tslint errors and formatting style errors.) - production environment:
npm run prod
How to configure prettier on your editor:
Webstorm
VS Code
prettier-vscode can be installed using the extension sidebar. Search for Prettier - Code formatter. It can also be installed using ext install prettier-vscode in the command palette.
Other editors
https://prettier.io/docs/en/editors.html
Playground
- Open your browser to this address: http://localhost:4000 and run the available commands:
- hello world
query {
hello
}
- fetch all the posts:
query {
posts {
author
comment
}
}
- add a new post
mutation {
addPost(author: "Daniele Zurico", comment: "www.dzurico.com") {
author
comment
}
}
- realtime data (Subscription) for post
subscription {
postAdded {
author
comment
}
}
- fetch all the users (you need to be authenticated with google identity server): For more information how to setup: http://www.dzurico.com/apollo-server-2-0-auth-with-google-signin/
query{
users {
id
name
surname
}
}
For a better explanation on how I setup this repo please refer to: