Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple boilerplate integrated typescript, graphql, postgres and apollo server

NotificationsYou must be signed in to change notification settings

davidnguyen11/typescript-graphql-postgres-boilerplate

Repository files navigation

Getting started

  • Make sure you haveDocker installed on your machine.
  • Make sure you haveNodeJS 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=dockerPOSTGRES_PASSWORD=dockerPOSTGRES_HOST=localhostPOSTGRES_DB=todoPOSTGRES_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

exportinterfaceTodoListItem{id:number;content:string;created_at:Date;updated_at:Date;}exportinterfaceInputTodoListItem{content:string;}

4. Implement the resolvers

graphql/resolvers/queries/todoListItems.ts

import{DB}from'../../../types';exportasyncfunctiontodoListItems(db:DB,args:any){constres=awaitdb.query('SELECT * FROM todo_list');  ...}

graphql/resolvers/mutations/createTodoItem.ts

import{DB}from'../../../types';exportasyncfunctioncreateTodoItem(db:DB,args:any){constquery='INSERT INTO todo_list(content) VALUES($1) RETURNING *';constvalues=['Call Your Dad'];constres=awaitdb.query(query,values);  ...}

Playground

This sandbox can only run in development mode by commandyarn dev ornpm 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

Withexpress-graphql, you can just send an HTTPPOST 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

constkeyword='Call your Mom';constquery=`{ 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

constquery=`  todoListItems($keyword:String){    todoListItems(keyword:$keyword){      status      data{        content      }    }  }`;constvariables=`{"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

About

Simple boilerplate integrated typescript, graphql, postgres and apollo server

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp