项目作者: jackchi

项目描述 :
Example Apollo Server
高级语言: JavaScript
项目地址: git://github.com/jackchi/graphql-twitter-example.git
创建时间: 2020-05-03T16:53:38Z
项目社区:https://github.com/jackchi/graphql-twitter-example

开源协议:

下载


Sample Twitter Example

An Apollo GraphQL backend server for a minimal twitter-like GraphQL API.

Installing

To get started with graphql-twitter-example:

  1. brew install node

Install dependencies:

  1. cd graphql-twitter-example
  2. npm install

Start apollo server:

  1. cd graphql-twitter-example
  2. nodemon src/index.js
  3. [nodemon] 2.0.3
  4. [nodemon] to restart at any time, enter `rs`
  5. [nodemon] watching path(s): *.*
  6. [nodemon] watching extensions: js,mjs,json
  7. [nodemon] starting `node src/index.js`
  8. 🚀 Server ready at http://localhost:4000/

Navicate to localhost:4000

You should see:
Apollo Playground Demo 1

Optional Seeding / Mutation

sequelize-cli is installed for extensibility of future schema changes.

Let’s say future you want to add a profile picture first,last names.

  1. npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string,password:string

To seed db, populate the db with some accounts for testing:

  1. npx sequelize-cli db:seed:all

APIs

Mutations

register - registers a email/password if it doesn’t exist

  1. mutation Register($email:String, $password: String) {
  2. register(email: $email, password: $password) {
  3. success
  4. message
  5. email
  6. }
  7. }

If successful:

  1. {
  2. "data": {
  3. "register": {
  4. "success": true,
  5. "message": "User Created",
  6. "email": "jack.chi_3@gmail.com"
  7. }
  8. }
  9. }

If email is already registered:

  1. {
  2. "data": {
  3. "register": {
  4. "success": false,
  5. "message": "Email already taken.",
  6. "email": "jack.chi_1@gmail.com"
  7. }
  8. }
  9. }

login - logs in the user via email/password

  1. mutation LoginUser($email:String, $password: String) {
  2. login(email: $email, password: $password) {
  3. success
  4. message
  5. token
  6. }
  7. }

If successful login, an Authorization Token will be returned.

  1. {
  2. "data": {
  3. "login": {
  4. "success": true,
  5. "message": "Logged in!",
  6. "token": "$2b$10$vBm4xcoUn5gReSDKLrWQIeBiy.8sin2ROiLKL6TPvtF0N8Jo3brkG"
  7. }
  8. }
  9. }

Then use the Authorization token on HTTP request headers:

  1. {
  2. "authorization": "$2b$10$vBm4xcoUn5gReSDKLrWQIeBiy.8sin2ROiLKL6TPvtF0N8Jo3brkG"
  3. }

createPost - creates a post from a String message

  1. mutation CreatePost ($message: String) {
  2. createPost(message: $message) {
  3. success
  4. message
  5. userId
  6. }
  7. }

Notice how we didn’t need to provide the email/password again.

  1. {
  2. "data": {
  3. "createPost": {
  4. "success": true,
  5. "message": "Cash Point Card is awesome!",
  6. "userId": "5"
  7. }
  8. }
  9. }

Query

me - gets the logged in user

  1. query Me {
  2. me {
  3. id
  4. email
  5. posts {
  6. message
  7. }
  8. }
  9. }

response:

  1. {
  2. "data": {
  3. "me": {
  4. "id": "5",
  5. "email": "jack.chi_3@gmail.com",
  6. "posts": null
  7. }
  8. }
  9. }

posts

  1. query GetPosts {
  2. posts {
  3. count
  4. posts {
  5. id
  6. userId
  7. message
  8. createdAt
  9. }
  10. }
  11. }

response contains total count and the message with created at datestring

  1. {
  2. "data": {
  3. "posts": {
  4. "count": 1,
  5. "posts": [
  6. {
  7. "id": "13",
  8. "userId": "5",
  9. "message": "Cash Point Card is awesome!",
  10. "createdAt": "5/3/2020, 9:35:57 AM"
  11. }
  12. ]
  13. }
  14. }
  15. }

Further Explanations

PROS

  • Currently using a sequelize for database orm. The datasource can be easily swapped out for mariadb/sql/etc.
  • Choice of using Apollo GraphQL server can be federated across services.
  • Simplicity!

CONS

  • Account authentication security. Initially I experimented with accountjs-accounts/accounts but had issues with fragment inline types. I tried to extend the schema type User but it was too rigid to define @relations. This definitely needs to be fixed. Perhaps with Firebase REST datasource.

Acknowledgments

  • Initially inspired by Point Card coding challenge