Solution: Creating a Basic GraphQL Endpoint
Go over the solution for the Creating a Basic GraphQL Endpoint challenge.
We'll cover the following...
Solution
The solution to this challenge is given in the code below:
// Diffrent imports
import { Application, Router } from 'https://deno.land/x/oak@v6.0.1/mod.ts';
import { applyGraphQL, gql } from "https://deno.land/x/oak_graphql@0.6.2/mod.ts";
// Configure Deno application
const app = new Application();
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});
// Types definition
const types = gql`
type Car {
id: String
brand: String
registration: String
price: Float
description: String
}
input CarInput {
brand: String
registration: String
price: Float
description: String
}
type CarRental {
id: String
car_id: String
user_id: String
total_price: Float
start_date: String
end_date: String
}
input CarRentalInput {
car_id: String
user_id: String
total_price: Float
start_date: String
end_date: String
}
type User {
id: String
firstName: String
lastName: String
}
input UserInput {
firstName: String
lastName: String
}
type ResolveType {
done: Boolean
}
type Query {
getAllCars: [Car]
getAllCarRentals: [CarRental]
getCar(id: String): Car
getCarRental(id: String): CarRental
}
type Mutation {
addCar(input: CarInput!): ResolveType!
addCarRental(input: CarRentalInput!): ResolveType!
addUser(input: UserInput!): ResolveType!
}
`;
// Some dummy data
const cars = [
{
id: "0",
brand: "Toyota",
registration: "XT-564-FS",
price: 15.3,
description: "A Car for renting"
}
];
const carRentals = [
{
id: "0",
car_id: "0",
user_id: "0",
total_price: 153,
start_date: "01/01/2022",
end_date:"11/01/2022"
}
];
const users = [
{
id: "0",
firstName: "Deno",
lastName: "Tutorial"
}
];
// Resolvers definition
const resolvers = {
Query: {
getAllCars: () => {
return cars;
},
getAllCarRentals: () => {
return carRentals
},
getCar: (_parent: any, { id }: { id: string }) => {
console.log(`getCar() id=(${id})`);
return cars.find((car) => car.id === id);
},
getCarRental: (_parent: any, { id }: { id: string }) => {
console.log(`getCarRental() id=(${id})`);
return carRentals.find((carRental) => carRental.id === id);
}
},
Mutation: {
addCar: (_parent: any, { input: { brand, registration, price, description } }: any, _context: any, _info: any) => {
console.log(`addCar() brand=(${brand}), registration=(${registration}), price=(${price}) description=(${description})`);
cars.push(
{
id: ((cars.length)++).toString(),
brand,
registration,
price,
description }
);
return {
done: true,
};
},
addCarRental: (_parent: any, { input: { car_id, user_id, total_price, start_date, end_date } }: any, _context: any, _info: any) => {
console.log(`addCarRental() car_id=(${car_id}), user_id=(${user_id}), total_price=(${total_price}), start_date=(${start_date}), end_date=(${end_date})`);
carRentals.push(
{
id: ((carRentals.length)++).toString(),
car_id,
user_id,
total_price,
start_date,
end_date
}
);
return {
done: true,
};
},
addUser: (_parent: any, { input: { firstName, lastName } }: any, _context: any, _info: any) => {
console.log(`addUser() firstName=(${firstName}) and lastName=(${lastName})`);
users.push(
{
id: ((users.length)++).toString(),
firstName,
lastName
}
);
return {
done: true,
};
}
}
};
// Add the GraphQL service
const GraphQLService = await applyGraphQL<Router>({
Router,
typeDefs: types,
resolvers: resolvers
})
// Start application
app.use(GraphQLService.routes(), GraphQLService.allowedMethods());
await app.listen({ port: 8000, secure: true, certFile: "/usercode/server.cert", keyFile: "private.key" });
Challenge solution
Explanation
In the code above, we ...
Ask