Skip to content

dantxal/social-graphql-api

Repository files navigation

social-graphql-api

This is a GraphQL API built with Koa for consuming with a Relay front-end. You can check out the Web App I built to consume this API at dantxal/socialapp-challenge.

Features:

  • You can create posts and comments;
  • GraphiQL Playground at PORT/graphiql;
  • Integration with a Mongo database (mongoose);
  • Relay compatible pagination (first, last, after, before);
  • Seeding script of posts and comments, with logs;
  • ESLint and Prettier configured.

Installation & Usage

Before running the project you need to have an instance of MongoDB to connect to. Then set environment variables at '.env' file.

⚠️ If you're using docker's "mongo" image, make sure to run it using --auth option and setting environment variables for user and password, for more information about doing this refer to mongo's image page and these docs.

docker run --name some-mongo 
-e MONGO_INITDB_ROOT_USERNAME=admin 
-e MONGO_INITDB_ROOT_PASSWORD=example 
-d -p 27017:27017 
mongo --auth
#HTTP
git clone https://github.com/dantxal/social-graphql-api.git 

#SSH
git clone [email protected]:dantxal/social-graphql-api.git 

yarn copy-env

# Add vars to .env

yarn 

dev mode

yarn dev

production mode

yarn start

seed database

yarn seed

Some useful queries

Create Post
mutation CreatePost {
  CreatePost(input: {
    title: "Post's title"
    text: "Some awesome information."
  }) {
    postEdge {
      cursor
      node {
        id
        title
        text
      }
    }
  }
}
Create Comment
mutation CreateComment {
  CreateComment (input: {
    postId: "UG9zdDo1Zjk2NDZjYTY1OTQ2NmQ0ODFkN2Q2NTc=" # change it for a valid Post ID
    text: "What a great post 3"
  }) {
    commentEdge {
      cursor
      node {
        id
        text
        createdAt
        updatedAt
      }
    }
  }
}
Query Posts (GET)
query GetPostsAndCommentsText{
  posts(first: 3, after: "") {
    edges {
      cursor
      node {
        id
        text
        comments {
          edges {
            node {
              text
            }
          }

        }
      }
    }
  }
}
Update Post
mutation UpdatePost {
  UpdatePost (input: {
    postId: "UG9zdDo1ZjhmOTljMDFhM2U2MjM0OGMzOWEzYWM=", # change it for a valid Post ID
    title: "First Post"
    text: "They apply it quite broadly to include database schemas, test plans, the build system, even documentation"
  }) {
    post {
      id
      text
      title
    }
  }
}
Delete post
mutation DeletePost {
  DeletePost(input: {
    postId: "UG9zdDo1ZjhmOThkYjFhM2U2MjM0OGMzOWEzYWE=", # change it for a valid Post ID
  }) {
    payload {
      id
      text
      title
    }
  }
}

Possible errors and solutions

Error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

This usually happens when running the application, if you are not running a database instance, or did not set environment variables correctly. SOLUTION: Make sure you're running your database instance and have set env vars correctly. Docker users may run docker ps

MongoError: command find/aggregate requires authentication at ...

This usually happens if you miss something from the setup database step.

SOLUTION: Provide 'MONGO_USER', 'MONGO_PASS' and 'MONGO_AUTH_SRC' environment variables. And if you're running 'mongo' image on docker. Run it using '--auth' option.

docker run --name some-mongo 
-e MONGO_INITDB_ROOT_USERNAME=admin 
-e MONGO_INITDB_ROOT_PASSWORD=example 
-d -p 27017:27017 
mongo --auth

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages