Skip to content

danielmocan/graphql-api-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL API Starter

GraphQL API Starter using Node, Apollo GraphQL Server and MongoDB.

The starter simulates a backend server for a blog. You have authors, posts and comments all tied together, this allows us to go in depth with our GraphQL Queries.

Prerequisites:

You will need MongoDB working on port 27017 (default port, if you have MongoDB installed globably just run mongod)

And install the project dependecies

npm install

Start the project

npm start

You can check the GraphQL Playground at http://localhost:4000

Your database will be empty so lets create some records using the GraphQL Interface:

Adding a user

mutation {
  addUser(user: {firstName: "Daniel", lastName:"Mocan", email:"[email protected]", password:"SomePassword"}) {
    id
    firstName
    lastName
  }
}

Login

Only user that are logged in are allowed to add post or comments

mutation {
  login(loginData:{email:"[email protected]", password: "SomePassword"}) {
    user{
      id
      firstName
      lastName
    }
    token
  }
}

You should receive the user information and the token. To send the token to the graphql api you need to send it on the headers. At the bottom of the left corner you two tabs Query Variables and HTTP Headers. In the Http Headers tab add the authorization header like this:

{
  "authorization": "Add here the token that you received at logged in (previous step)"
}

Now that we have the token and we send it back to the graphql api, we can create a post. As a side note you can check server.js we check if the token exists, if its valid and get the logged in user's details.

Adding a post

mutation {
  addPost(post:{ title: "Our first Post", content: "Hello World! This is the content of the post"}) {
    id
    title
    content
  }
}

Query of Posts

We can now query for the posts.

query {
  getPosts {
    id
    title
  }
}

And now to see the realy power and beauty of GraphQL in action, we can do a nested query.

query {
  getPosts {
    id
    title
    author {
      id
      firstName
      lastName
      posts {
        title,
        content,
        author {
          firstName,
          lastName,
          id
        }
        comments {
          content
        }
      }
    }
  }
}

To Do List

  • Decouple controller from any database/moongoose dependency
  • Add validation for fields
  • Add Error handling
  • Document how things are connected
  • Maybe move some of the filed resolvers to a general resolver
  • Add more comments in the code

About

GraphQL API Starter using Node, Apollo GraphQL Server and MongoDB (Mongoose)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published