Giter VIP home page Giter VIP logo

graphql-with-prisma's Introduction

Graphql with Prisma

This project is made to show how to setup a Graphql server using graphql-yoga and connect it to the Prisma open-source database toolkit for database queries and transactions

Prerequisites

  • Node v13.7
  • NPM
  • Yarn (preferred dependency manager, but you can use NPM, whichever you prefer)
  • Mysql
    • A database

Installation

Clone this repository

git clone https://github.com/mrmunar/graphql-with-prisma.git

Create a .env file under the prisma folder with the following variables and set your database credentials:

# Example database credentials
DATABASE_URL="mysql://root:123456@localhost:3308/testdb?schema=public"

NODE_ENV="development"

Install the dependencies

yarn install

Generate Prisma files and functions. This lets you use Prisma commands for interacting with the database.

yarn run prisma generate

Migrate database schema

yarn run prisma-migrate-up

Start the Graphql server. This will spin up a server using port 4000.

yarn start

Usage

Access the Graphql server (Playground)

http://localhost:4000

There are 4 APIs available:

  • createUser
  • getUsers
  • createPost
  • getPosts

createUser

Request:

mutation {
  createUser(data: { firstname: "Migs", lastname: "Munar" }) {
    id
    firstname
    lastname
  }
}

Response:

{
  "data": {
    "createUser": {
      "id": "18",
      "firstname": "Migs",
      "lastname": "Munar"
    }
  }
}

getUsers

Request:

query {
  getUsers {
    id
    firstname
    lastname
  }
}

Response:

{
  "data": {
    "getUsers": [
      {
        "id": "1",
        "firstname": "firstname1",
        "lastname": "lastname1"
      },
      {
        "id": "2",
        "firstname": "firstname2",
        "lastname": "lastname2"
      },
      {
        "id": "3",
        "firstname": "firstname3",
        "lastname": "lastname3"
      },
      ...
    ]
  }
}

createPost

Request:

mutation {
  createPost(data: {title: "test", content: "post"}) {
    id
    title
    content
  }
}

Response:

{
  "data": {
    "createPost": {
      "id": "14",
      "title": "test",
      "content": "post"
    }
  }
}

getPosts

Request:

query {
  getPosts {
    id
    title
    content
  }
}

Response:

{
  "data": {
    "getPosts": [
      {
        "id": "1",
        "title": "title1",
        "content": "content1"
      },
      {
        "id": "2",
        "title": "title2",
        "content": "content2"
      },
      {
        "id": "3",
        "title": "title3",
        "content": "content3"
      },
      ...
    ]
  }
}

License

MIT

graphql-with-prisma's People

Contributors

mrmunar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.