All Projects → davidnguyen179 → typescript-graphql-postgres-boilerplate

davidnguyen179 / typescript-graphql-postgres-boilerplate

Licence: other
Simple boilerplate integrated typescript, graphql, postgres and apollo server

Programming Languages

typescript
32286 projects
Dockerfile
14818 projects
shell
77523 projects

Projects that are alternatives of or similar to typescript-graphql-postgres-boilerplate

apollo-log
A logging extension for the Apollo GraphQL Server
Stars: ✭ 64 (+255.56%)
Mutual labels:  apollo, apollo-server, apollo-graphql
kontent-boilerplate-express-apollo
Kontent Boilerplate for development of Express application using Apollo server and GraphQL.
Stars: ✭ 21 (+16.67%)
Mutual labels:  apollo, apollo-server, apollo-server-express
apollo-error-converter
Global Apollo Server Error handling made easy. Remove verbose and repetitive resolver / data source Error handling. Automatic Error catching, logging, and conversion to ApolloErrors.
Stars: ✭ 16 (-11.11%)
Mutual labels:  apollo, apollo-server, apollo-server-express
apollo-graphql-tutorial
Learning apollo graphql version 2.x with Node.js
Stars: ✭ 18 (+0%)
Mutual labels:  apollo-server, apollo-server-express, apollo-graphql
apollo-express-ts-server-boilerplate
No description or website provided.
Stars: ✭ 29 (+61.11%)
Mutual labels:  apollo, apollo-server, apollo-server-express
future-of-gql-servers
The future of GraphQL servers (GraphQL Europe 2018)
Stars: ✭ 27 (+50%)
Mutual labels:  apollo, apollo-server
bookshelf
My GraphQL playground
Stars: ✭ 64 (+255.56%)
Mutual labels:  apollo, apollo-server
Todo-apollo-redux-react-native
Todo App with apollo + redux + react-native
Stars: ✭ 15 (-16.67%)
Mutual labels:  apollo, apollo-server
graphql-blog-cms-api
A Blog CMS API powered by GraphQL and Apollo server
Stars: ✭ 23 (+27.78%)
Mutual labels:  apollo-server, apollo-server-express
Heighliner
A GraphQL Server for NewSpring Web
Stars: ✭ 13 (-27.78%)
Mutual labels:  apollo, apollo-server
graphql-compose-elasticsearch
Graphql App using Node with typescript, KOA framework and Elasticsearch
Stars: ✭ 40 (+122.22%)
Mutual labels:  apollo-server, apollo-graphql
hasura-node-monolith-example
Example of a monolithic web application using Hasura GraphQL Engine + Node.js + Next.js
Stars: ✭ 25 (+38.89%)
Mutual labels:  apollo, apollo-graphql
Fullstack Boilerplate
Fullstack boilerplate using Typescript, React, GraphQL
Stars: ✭ 181 (+905.56%)
Mutual labels:  postgres, apollo
fullstack-apollo-subscription-example
A minimal Apollo Server 2 with Apollo Client 2 with Subscriptions application.
Stars: ✭ 72 (+300%)
Mutual labels:  apollo-server, apollo-server-express
Fullstack Apollo Express Postgresql Boilerplate
💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
Stars: ✭ 1,079 (+5894.44%)
Mutual labels:  postgres, apollo-server
Yiigo
🔥 Go 轻量级开发通用库 🚀🚀🚀
Stars: ✭ 304 (+1588.89%)
Mutual labels:  postgres, apollo
graphX
A simple blog based on Nuxt and graphQL
Stars: ✭ 19 (+5.56%)
Mutual labels:  apollo, apollo-server-express
create-full-stack
Set up a TypeScript full stack with one command.
Stars: ✭ 94 (+422.22%)
Mutual labels:  apollo, apollo-server-express
les-chat
Real-time messenger with private, public & group chat. Made using PERN + GraphQL stack.
Stars: ✭ 48 (+166.67%)
Mutual labels:  apollo, apollo-server
Sqldatasource
SQL DataSource for Apollo GraphQL projects
Stars: ✭ 176 (+877.78%)
Mutual labels:  apollo, apollo-server

Graphql Todo API

Getting started

  • Make sure you have Docker installed on your machine.
  • Make sure you have NodeJS installed on your machine.

Then run

npm

npm i

yarn

yarn install

.env file

.env file

  1. Create the .env file
  2. Copy and parse the database connection information below:
POSTGRES_USER=docker
POSTGRES_PASSWORD=docker
POSTGRES_HOST=localhost
POSTGRES_DB=todo
POSTGRES_PORT=54320

Database

To create database, run:

docker-compose up -d

Seeding data

dump data

To initialize the dump data for a database, run:

npm run seed

Development

To run on development environment

npm run dev

Production

To run on production environment

npm start

How to write GraphQL

1. Define the schema & type

For more information: https://graphql.org/learn/schema/

graphql/types/todo-list.graphql

type ResponseTodoList {
  status: String!
  message: String!
  data: [TodoListItem]
}

type TodoListItem {
  id: ID!
  content: String!
}

input InputTodoListItem {
  content: String!
}

type Query {
  todoListItems(keyword: String): ResponseTodoList!
}

type Mutation {
  createTodoItem(item: InputTodoListItem): ResponseTodoList!
}

2. Register *.graphql in schema.graphql

graphql/types/schema.graphql

# import Query.*, Mutation.* from "todo-list.graphql"

3. Define models for data

The model actually the type of data mapped to fields of table in database.

models/todo-list.ts

export interface TodoListItem {
  id: number;
  content: string;
  created_at: Date;
  updated_at: Date;
}

export interface InputTodoListItem {
  content: string;
}

4. Implement the resolvers

graphql/resolvers/queries/todoListItems.ts

import { DB } from '../../../types';

export async function todoListItems(db: DB, args: any) {
  const res = await db.query('SELECT * FROM todo_list');
  ...
}

graphql/resolvers/mutations/createTodoItem.ts

import { DB } from '../../../types';

export async function createTodoItem(db: DB, args: any) {
  const query = 'INSERT INTO todo_list(content) VALUES($1) RETURNING *';
  const values = ['Call Your Dad'];
  const res = await db.query(query, values);
  ...
}

Playground

This sandbox can only run in development mode by command yarn dev or npm run dev. After starting the development environment, open:

query - without param

query{
  todoListItems{
    status
    data{
      content
    }
  }
}

query - with param

query{
  todoListItems(keyword: "Call your Mom"){
    status
    data{
      content
    }
  }
}

mutation - createTodoItem

mutation{
  createTodoItem(item:{
    content: "Just relax, dude!"
  }){
    status
    data{
      content
    }
  }
}

Usage

With express-graphql, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the query field in a JSON payload.

POST cURL

curl -X POST \
  http://localhost:4000/graphql \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: c011dc94-6f67-483a-84cb-2bd4ed442a95' \
  -H 'cache-control: no-cache' \
  -d '{
	"query": "{ todoListItems{ data { content } } }"
}'

GET cURL

curl -X GET \
  'http://localhost:4000/graphql?query=query+todoListItems%28$keyword:String%29{todoListItems%28keyword:$keyword%29{status}}&variables={%22keyword%22:%22Call%20your%20Mom%22}' \
  -H 'Postman-Token: f92396a4-4f51-47f0-ac20-3c900289358f' \
  -H 'cache-control: no-cache'

POST fetch

const keyword = 'Call your Mom';
const query = `{ todoListItems(keyword: "${keyword}"){ data { content } } }`;

fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Accept: 'application/json',
  },
  body: JSON.stringify({ query }),
})
  .then(res => res.json())
  .then(data => console.log('data returned:', data));

GET fetch

const query = `
  todoListItems($keyword:String){
    todoListItems(keyword:$keyword){
      status
      data{
        content
      }
    }
  }
`;

const variables = `{"keyword":"Call your Mom"}`;

fetch(`/graphql?query=query+${query}&variables=${variables}`)
  .then(res => res.json())
  .then(data => console.log('data returned:', data));

For more information check:

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].