CREATE DATABASE dellstore2;
\connect dellstore2;
CREATE SCHEMA postgraphile;
pg_restore --no-owner --dbname postgraphile dellstore2.sql;
OR specify hostname, database and username
psql -h hostname -d databasename -U username -f file.sql
e.g. ```psql -h localhost -d dellstore2 -U postgres -f dellstore2.sql```
postgraphile --watch --schema postgraphile -c "postgres://username:password@localhost:5432/dellstore2"
http://localhost:5000/graphiql
query getProductById{
productByProdId(prodId:10){
title
}
}
OUTPUT:
{
"data": {
"productByProdId": {
"title": "ACADEMY ALADDIN"
}
}
}
query getProductAndCategory{
ProductOne: productByProdId(prodId:10000){
title,
actor,
price,
categoryByCategory{
categoryname,
}
}
}
OUTPUT:
{
"data": {
"ProductOne": {
"title": "ALADDIN ZORRO",
"actor": "MAE CRUISE",
"price": "10.99",
"categoryByCategory": {
"categoryname": "New"
}
}
}
}
Simple example of a pre-defined funciton:
SELECT (price - (price * 0.21)) FROM postgraphile.products WHERE prod_id = 10000;
mutation sds{
getPrice(input:{clientMutationId:""}){
bigFloat
}
}
OUTPUT:
{
"data": {
"getPrice": {
"bigFloat": "8.6821"
}
}
}
4. A GraphQL query which returns the attributes from 3 joined database relations having 2 levels of nesting in the resultant output Select all customers (first name, last name, country and income) for particular order from the orderline, specified by product.
query question3{
productByProdId(prodId:1000){
orderlinesByProdId(orderBy: PROD_ID_ASC){
totalCount
nodes{
orderByOrderid{
customerByCustomerid{
firstname,
lastname,
country,
income
}
}
}
}
}
}
OUTPUT:
{
"data": {
"productByProdId": {
"orderlinesByProdId": {
"totalCount": 5,
"nodes": [
{
"orderByOrderid": {
"customerByCustomerid": {
"firstname": "UXLWZS",
"lastname": "KAHFIKAFKZ",
"country": "China",
"income": 60000
}
}
},
.
.
.
5.A mutation to add a new order to the database. The mutation updates the orders, orderlines and cust_hist relations
mutation addOrderr($pOrder: OrderInput!, $pOrderLine: OrderlineInput!, $pCust_Hist: CustHistInput!){
createOrder(input: {clientMutationId:"",order: $pOrder}){
order{
orderid,
customerid,
orderdate
}
}
createOrderline(input:{clientMutationId:"",orderline: $pOrderLine}){
orderline{
orderid,
orderdate,
prodId,
orderdate
}
}
createCustHist(input:{clientMutationId:"",custHist:$pCust_Hist}){
custHist{
customerid,
orderid,
prodId,
}
}
}
OUTPUT:
{
"data": {
"createOrder": {
"order": {
"orderid": 12015,
"customerid": 10000,
"orderdate": "2002-12-12"
}
},
"createOrderline": {
"orderline": {
"orderid": 12015,
"orderdate": "2002-12-12",
"prodId": 1000
}
},
"createCustHist": {
"custHist": {
"customerid": 10000,
"orderid": 12015,
"prodId": 1000
}
}
}
}
Manual implementation of the query from task 2 (above) directly using GraphQL and Express i.e. not using postgraphql. Sequelize is used to query Postgres as part of the resolver function.
To start the program, navigate to the root directory and execute the following:
node server.js
Then navigate to http://localhost:3000/graphAPI
and execute the following:
{
products{
prod_id,
price,
actor,
special
category{
category,
categoryname
}
}
}
The output should be as follow:
{
"data": {
"products": [
{
"prod_id": 1,
"price": 25.99,
"actor": "PENELOPE GUINESS",
"special": 0,
"category": {
"category": 14,
"categoryname": "Sci-Fi"
}
},
{
"prod_id": 2,
"price": 20.99,
"actor": "EWAN RICKMAN",
"special": 0,
"category": {
"category": 6,
"categoryname": "Documentary"
}
},
.
.
.