Skip to content

CobyYates/Dealership-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dealership GraphQL Backend

GraphQL

GraphQL

GraphQL

Project By Coby Yates

This example shows how to implement a GraphQL server with JavaScript (Node.js) based on Prisma Client, apollo-server, PostgreSQL, and GraphQL Nexus.

How to use

1. Download example & install dependencies

Install npm dependencies:

npm install

2. Run each npm script in package.json in this order

npm run launchDocker
npm run createDB
npm run generate
npm run seed

Dev Server

npm run dev

In another terminal tab run

npm run start

To view your Prisma Studio and GraphQL

Open the following in your browser

Resource URL
Prisma Studio localhost:5555
GraphQL Playground localhost:4000

Prisma as your data modeling tool

Docker-based PostgreSQL, MySQL, or MongoDB as your data store

One of the scripts specified in the package.json file is:

"launchDocker": "docker run --name pg-docker"

At least 3 Query resolvers to get data from your server

See Mutations's
t.field('Vehicle', {
  type: 'Vehicle',
  nullable: true,
  args: { id: idArg() },
  resolve: (parent, { id }, ctx) => {
    return ctx.prisma.vehicle.findOne({
      where: {
        id,
      },
    })
  },
})
t.list.field('Vehicles', {
  type: 'Vehicle',
  args: {
    searchString: stringArg({ nullable: true }),
  },
  resolve: (parent, { searchString }, ctx) => {
    return ctx.prisma.vehicle.findMany({
      where: {
        OR: [
              { make: { contains: searchString } },
              { year: { contains: searchString } },
              { vtype: { contains: searchString } }
            ],
      },
    })
  },
})

At least 2 Mutation resolvers allowing users to create, update, delete or upsert an item.

Simple form (more work for user)

export const Mutation = mutationType({
  name: 'Mutation',
  definition(t) {
    t.crud.updateOneVehicle()
    t.crud.deleteOneVehicle()
  },
})

More specific Mutation

See Mutations's
export const Mutation = mutationType({
    name: 'Mutation',
    definition(t) {
        t.crud.deleteOneVehicle()
        t.field('createVehicle', {
          type: 'Vehicle',
          args: {
            make: stringArg({ nullable: false }),
            model: stringArg({ nullable: false }),
            year: stringArg({ nullable: false }),
            topSpeed: stringArg(),
            power: stringArg(),
            weight: stringArg(),
            engine: stringArg(),
            torque: stringArg(),
            sixty: stringArg(),
            price: stringArg(),
            vtype: stringArg(),
          },
          resolve: (
            parent,
            { make, model, year, topSpeed, power, weight, engine, torque, sixty, price, vtype, },
            ctx,
          ) => {
            return ctx.prisma.vehicle.create({
              data: { make, model, year, topSpeed, power, weight, engine, torque, sixty, price, vtype, },
            })
          },
        })

Your datastore will contain at least 25 items

The original data is pulled from a cars.json file to seed the database with data

Your app will be deployable locally using Docker and will have seed data entered into the datastore.

One of the scripts specified in the package.json file is:

"seed": "node -r esm prisma/seed"

GraphQL

query allVehicles {
   Vehicles{
    id
    make
    model
    year
    price
  }  
}
See more API operations

Create a single vehicle

mutation createVehicle {
  createVehicle(
    	vtype: "truck",
      make: "Ford",
      model: "Raptor",
      year: "2020",
      topSpeed: "107",
      power: "400",
      weight: "5508",
      engine: "V6",
      torque: "510",
      sixty: "5.1",
      price: "53455",
      image: "test"
  ) {
    id
    createdAt
    make
    model
  }
}

Update a vehicle by id

mutation updateOneVehicle {
  updateOneVehicle(
    where: {id: "ck98yqta90000m0uaz66vmccr"}
    data: {
      vtype: "truck",
      make: "Ford",
      model: "Raptor",
      year: "2020",
      topSpeed: "107",
      power: "450",
      weight: "5508",
      engine: "V6",
      torque: "510",
      sixty: "5.1",
      price: "53455"
    }
  ){
    make
    model
    power
    createdAt
    updatedAt
  }
}

Delete a single vehicle by id

mutation deleteVehicle {
  deleteOneVehicle(where: {
    id: "ck98yqta90000m0uaz66vmccr"
  })
  {
    id
    make
  }
}

Find one vehicle by id

query filterVehicle {
  Vehicle(id: "ck98yqta90000m0uaz66vmccr") {
    make
    model
    year
    price
    vtype
  }
}

Search by any type of make

query filterVehicles {
  Vehicles(searchString: "truck") {
    make
    model
    year
    price
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published