This example shows how to implement a GraphQL server with JavaScript (Node.js) based on Prisma Client, apollo-server, PostgreSQL, and GraphQL Nexus.
Install npm dependencies:
npm install
npm run launchDocker
npm run createDB
npm run generate
npm run seed
npm run dev
npm run start
Open the following in your browser
Resource | URL |
---|---|
Prisma Studio | localhost:5555 |
GraphQL Playground | localhost:4000 |
One of the scripts specified in the package.json file is:
"launchDocker": "docker run --name pg-docker"
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 } }
],
},
})
},
})
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, },
})
},
})
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"
query allVehicles {
Vehicles{
id
make
model
year
price
}
}
See more API operations
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
}
}
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
}
}
mutation deleteVehicle {
deleteOneVehicle(where: {
id: "ck98yqta90000m0uaz66vmccr"
})
{
id
make
}
}
query filterVehicle {
Vehicle(id: "ck98yqta90000m0uaz66vmccr") {
make
model
year
price
vtype
}
}
query filterVehicles {
Vehicles(searchString: "truck") {
make
model
year
price
}
}