Get Practical GraphQL: Learning Full-Stack GraphQL Development With Projects 1st Edition Nabendu Biswas Free All Chapters
Get Practical GraphQL: Learning Full-Stack GraphQL Development With Projects 1st Edition Nabendu Biswas Free All Chapters
Get Practical GraphQL: Learning Full-Stack GraphQL Development With Projects 1st Edition Nabendu Biswas Free All Chapters
com
https://ebookmass.com/product/practical-graphql-learning-
full-stack-graphql-development-with-projects-1st-edition-
nabendu-biswas/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookmass.com/product/typescript-basics-learn-typescript-
from-scratch-and-solidify-your-skills-with-projects-1st-edition-
nabendu-biswas/
https://ebookmass.com/product/practical-matlab-deep-learning-a-
projects-based-approach-2nd-edition-michael-paluszek/
https://ebookmass.com/product/serverless-web-applications-with-
aws-amplify-build-full-stack-serverless-applications-using-
amazon-web-services-akshat-paul/
https://ebookmass.com/product/practical-rust-projects-second-
edition-shing-lyu/
With Full Malice Brenda Hill
https://ebookmass.com/product/with-full-malice-brenda-hill/
https://ebookmass.com/product/engineering-for-sustainable-
development-theory-and-practice-wahidul-k-biswas/
https://ebookmass.com/product/heartbeat-sensor-projects-with-
pulsesensor-prototyping-devices-with-biofeedback-1st-edition-
yury-gitman/
https://ebookmass.com/product/practical-rust-projects-build-
serverless-ai-machine-learning-embedded-game-and-web-
applications-2nd-ed-2nd-edition-shing-lyu/
https://ebookmass.com/product/practical-machine-learning-on-
databricks-1st-edition-debu-sinha/
Practical
GraphQL
Learning Full-Stack GraphQL
Development with Projects
—
Nabendu Biswas
Practical GraphQL
Learning Full-Stack GraphQL
Development with Projects
Nabendu Biswas
Practical GraphQL: Learning Full-Stack GraphQL Development with
Projects
Nabendu Biswas
Bhopal, India
Introduction���������������������������������������������������������������������������������������xiii
v
Table of Contents
Adding Reviews����������������������������������������������������������������������������������������39
Adding Filtering����������������������������������������������������������������������������������������42
Filter by Average Rating���������������������������������������������������������������������������46
Summary��������������������������������������������������������������������������������������������������48
Chapter 3: Mutations��������������������������������������������������������������������������49
Creating Data�������������������������������������������������������������������������������������������49
Deleting Data��������������������������������������������������������������������������������������������57
Updating Data�������������������������������������������������������������������������������������������68
Summary��������������������������������������������������������������������������������������������������75
Chapter 4: Full-Stack GraphQL�����������������������������������������������������������77
The Setup�������������������������������������������������������������������������������������������������77
Create an Express Server�������������������������������������������������������������������������78
Set Up GraphQL����������������������������������������������������������������������������������������80
Client Queries�������������������������������������������������������������������������������������������82
Project Queries�����������������������������������������������������������������������������������������85
Cloud MongoDB����������������������������������������������������������������������������������������89
Mongoose Models and Fetching Data����������������������������������������������������100
Client Mutations�������������������������������������������������������������������������������������104
Project Mutations�����������������������������������������������������������������������������������107
Client with React������������������������������������������������������������������������������������114
Apollo Setup for the Client���������������������������������������������������������������������120
Adding the deleteClient and addClient Mutations����������������������������������125
Displaying Projects and the React Router����������������������������������������������133
vi
Table of Contents
vii
Table of Contents
Index�������������������������������������������������������������������������������������������������257
viii
About the Author
Nabendu Biswas is a full-stack JavaScript
developer and has been working in the IT
industry for the past 16 years for some of
world’s top development firms and investment
banks. He is a passionate tech blogger and
YouTuber and currently works as an Architect
in an IT firm. He is also the author of six Apress
books focusing on topics such as Gatsby,
MERN, TypeScript and React Firebase, all of
which can be found on Amazon.
ix
About the Technical Reviewer
Preethi Vasudev is an Oracle Certified Java
programmer with more than 20 years of
industry experience in investment banking,
healthcare, and other domains. She has a
master’s degree from Auburn University,
Alabama, and enjoys participating in coding
competitions.
xi
Introduction
GraphQL is revolutionizing how we develop and build websites. Seen
as an alternative to REST APIs, this query language has become popular
among developers and engineers who are looking for a query language
to help them quickly develop and deploy applications and features with
minimal fuss.
Starting with the basics, this book will teach you how to set up
GraphQL and key details regarding queries and mutations, before moving
on to more advanced, practical topics and projects.
You will gain a full understanding of the full web development
ecosystem from front end to back end, by learning to build React
applications using Prisma Apollo Client and MongoDB. Using a project-
based approach, this book will equip you with all the practical knowledge
needed to tackle full enterprise projects and turbocharge your skills and
projects.
xiii
CHAPTER 1
Getting Started
In this book, you will learn about GraphQL. GraphQL is a new way to
create APIs. Traditional APIs were created with REST. The problem with
REST is that even when you need a subset of data, the endpoint gives all
the data from the databases.
This is a waste of precious network resources. So, in this book, you will
learn all about GraphQL. Here, you will learn to create APIs in a new way
in NodeJS. You will also learn to connect them to the front end.
Initial Setup
In this first chapter, you will learn how to install GraphQL on your system.
Also, you will learn to add extensions for GraphQL on VS Code to help you
in the development process.
To start using GraphQL, you need to have NodeJS installed on
your system. You will be using VS Code in your project. After that, you
should also install an extension called GraphQL, which will be helpful in
highlighting GraphQL in VS Code (Figure 1-1).
2
Chapter 1 Getting Started
There are different ways to use graphql, and apollo is the most
popular one (Figure 1-2).
S
ummary
In this chapter, you installed the packages apollo-server and graphql
in your system, through the terminal. You installed the official GraphQL
extension in VS Code, which will help you in the development process.
In the next chapter, you will learn about queries in GraphQL, which are
similar to GET APIs in RESTful endpoints and are used to get the data from
the database.
3
CHAPTER 2
Queries
In this chapter, we will start creating an e-commerce app and learn how to
use GraphQL with it.
“It was meant that they all should wear dresses alike,
But bad luck has prompted the tailors to strike.
When they go to their work, his men will appear
À la Française, Conducteur à Mons. Shillibeer.
But, unfortunately for Shillibeer, the plan for the Greenwich railway
did not disappear. It was carried out, and when, in 1835, the railway
was opened, the earnings of Shillibeer’s omnibuses began to
decrease ominously. For a time Shillibeer struggled on manfully, but
the fight with the railway was an expensive one, and getting into
arrears with his payments to the Stamp and Taxes Office, his
omnibuses were seized and not permitted to be worked until the
money was paid. This unreasonable action on the part of the Stamp
Office was repeated three or four times, and the heavy expenses
and hindrance to business caused thereby brought about Shillibeer’s
failure.
Acting on the advice of his many sympathisers, Shillibeer appealed,
in 1838, to the Lords of the Treasury for compensation for the
injustice done to him, with the result that, shortly after, he was
offered the position of Assistant Registrar of Licences, created by the
Bill just passed for the better regulation of omnibuses in and near
the Metropolis. This Act, the second one dealing with omnibuses,
made it compulsory that the words “Metropolitan Stage Carriage,”
the Stamp Office number, and the number of passengers that each
vehicle was licensed to carry should be painted, in a conspicuous
manner, both on the inside and outside of every omnibus. Drivers
and conductors were compelled to wear numbered badges, so as to
afford means of identification in case of misconduct. Licences were
not to be transferred or lent under a penalty of £5, and the omnibus
proprietors were forbidden, under a penalty of £10, to allow any
unlicensed person to act as driver or conductor, except in the case of
sudden illness of the licensed man.
Shillibeer had been led to believe that he would receive the
appointment of Registrar of Licences, and was, therefore, greatly
disappointed when the Assistant Registrarship was offered him. He
declined it, and renewed his applications to the Lords of the Treasury
for compensation for the loss he had suffered through the almost
criminal stupidity of the Stamp and Taxes Office.
At length their Lordships appointed their Financial Secretary, Mr.
Gordon, to inquire into his case, and that gentleman’s investigation
of the facts proved to their complete satisfaction that Shillibeer had
been cruelly wronged by the Stamp and Taxes Office. Thereupon,
they promised Shillibeer that he should receive a Government
appointment, or a sum of money, that would compensate him for
the loss he had suffered. Mr. Gordon was then instructed to apply to
the Marquis of Normanby and the Right Hon. Henry Labouchere, the
heads of two Government departments, to appoint Shillibeer
Inspector-General of Public Carriages, or to give him an appointment
on the Railway Department at the Board of Trade. Unfortunately
both of these applications were unsuccessful. Mr. Gordon then
applied for and obtained for Shillibeer the promise of one of the
twenty-five appointments of Receiver-General of County Courts,
which were just then being established. But once again Shillibeer
was doomed to disappointment. Mr. Gordon resigned his position of
Secretary to the Lords of the Treasury, but, before ceasing his
duties, he told Shillibeer that, if the Miscellaneous Estimates for the
year had not been made up, his name would have been placed in
them for a grant of £5000. Moreover, he promised to impress upon
his successor the necessity of seeing that Shillibeer received his
appointment and grant. He received neither. His claims were not
disputed, but unjustly ignored.
At last Shillibeer came to the conclusion that it was useless to place
reliance in Government promises. He, therefore, started business as
an undertaker, in premises adjoining Bunhill Fields Burial-ground,
and the following advertisement appeared continually in the daily
papers and elsewhere:—
“Aux Étrangers. Pompes.
“Funèbres sur le systeme de la Compagnie Générale des Inhumations
et Pompes Funèbres à Paris. Shillibeer’s, City Road, near Finsbury
Square, où l’on parle Français. Every description of funerals, from the
most costly to the most humble, performed much lower than any
other funeral establishment. Catholic fittings from Paris. Gentlemen’s
funerals from 10 guineas. Tradesmen’s and artisans’, £8, £6, and £4.”
The first real steam omnibuses, the “Era” and “Autopsy,” were
invented by Walter Hancock, of Stratford, and placed on the London
roads in 1833. Hancock had invented steam carriages before
Shillibeer’s omnibuses were introduced, but the “Autopsy” and the
“Era” were the first which he constructed with the idea of entering
into competition with the popular horse-drawn vehicles. The “Era”
was the better omnibus of the two, and the most flattering things
were said and predicted of it. Enthusiasts declared that omnibuses of
the “Era” type would enable passengers to be carried at a cheaper
rate and greater speed than by Shillibeer’s vehicles.
THE “AUTOPSY” STEAM OMNIBUS.
The “Era” ran from Paddington to the Bank, the same route as the
horse-drawn omnibuses, and carried fourteen passengers, the fare
being sixpence all the way. It travelled at the rate of ten miles an
hour, and consumed from 8 to 12 lbs. of coke, and 100 lbs. of water
per mile. But, in spite of what the enthusiasts of the day wrote, the
“Era” was by no means a success, for it broke down continually, and
frequently a considerable time elapsed before it could resume the
journey. Our grandfathers, who took life more leisurely than we, did
not appear to be greatly annoyed at these collapses. An hour’s delay
in reaching their destination was of little consequence to those who
could afford to live in the suburbs, and as the steam omnibuses—
when they did run—were guided easily and escaped collisions, they
were perfectly satisfied, assuring themselves that in a few years, at
the most, some means would be found for making the vehicles stop
only when required. Moreover, they were a novelty, and as such
were patronised for a time. Unfortunately for Hancock, the
eccentricities of the “Autopsy” and “Era” increased as the months
went on, although the two vehicles continued to run after all the
steam omnibuses by other makers had been taken off the roads.
Nevertheless, Hancock was not dispirited, and in July, 1835, started
his last, and best, steam omnibus—the “Automaton.” This was a
larger vehicle than his previous ones, being built to carry twenty-two
passengers, and to travel at an average speed of thirteen miles an
hour. On its trial trip to Romford and back, it did not, however,
succeed in attaining a better average than eleven miles an hour.
Certainly faster travelling was not desirable in London streets, but on
one occasion the “Automaton” was driven at full speed along the
Bow Road, and covered a mile at the rate of twenty-one miles an
hour. And that record run was the more remarkable as, when it was
made, the omnibus carried twenty passengers.
But it was not only of the drivers and conductors that the public
complained. The officials at the inquiry offices stationed at the
starting-point of each line, were denounced as being utterly unfitted
for the positions they occupied. All were rude, and most of them
possessed but little intelligence. One afternoon, about twenty
minutes past four, a gentleman entered the omnibus office at the
George and Blue Boar, Holborn, and inquired of the clerk whether
omnibuses started from there to a certain railway-station.
“Yes,” was the reply.
“At what hours?”
“One hour before each train.”
“Then I’m just in time to catch the 5.30 one.”
“It’s all down in writing on that there board.”
The traveller turned to the board, and, finding the 5.30 train entered
upon it, went out into the street to await the arrival of the omnibus.
But after pacing up and down for a quarter of an hour, and seeing
no sign of a conveyance, he returned to the office and enquired
when it would arrive.
“It’s gone,” the official said.
“Then it didn’t start from here,” the traveller declared. “I’ve been
waiting outside since twenty past four.”
“What train do you want to catch?”
“The half-past five, to be sure. I told you so.”
“Oh, we ain’t got no omnibus to catch that train.”
“But, man, you said that you had one to each train.”
“I told you it was all down in writing on that there board, and you
ought to have seen for yourself there ain’t no omnibus for the half-
past five.”
The traveller again turned to the board, and after glancing at it,
declared angrily, “There’s nothing of the kind stated here!”
The official pointed to a small cross against the 5.30 train, and said
triumphantly, “This here mark means there ain’t no omnibus.”
“Well, how was I to know that?”
“Most gentlemen, when they sees it, asks me what the deuce it
means, and I tells them.”
“But what do the others do?”
The clerk did not condescend to answer, but took out his pocket-
knife and busied himself in peeling an apple.
While the public was busily denouncing the behaviour of ’busmen, a
quaint vehicle, named the “equirotal omnibus,” was placed on the
streets. The inventor, Mr. W. B. Adams, maintained that all vehicles
should have four large wheels, instead of two large and two small,
and his omnibus was constructed on that principle. It was built in
two parts, which were joined together in the middle by a flexible
leather passage, to enable it to turn easily. “It will turn with facility in
the narrowest streets, without impeding the passage along the
interior,” Mr. Adams declared, “as the flexible sides move in a circle.
With this omnibus two horses will do the work of three; there will be
great facility of access and egress; perfect command over the
horses; increased ease to the passengers; greater head-room and
more perfect ventilation; greater general durability and absence of
the usual rattling noise, accompanied by entire safety against
overturning.”
In spite of Mr. Adams’s recommendation, the “equirotal omnibus” did
not become popular, and had but a short career.
A KNIFE-BOARD OMNIBUS.
These new outside seats were very popular with the public, but the
police objected to them, on the ground that the climbing up to them
was dangerous. The police were undoubtedly in the right, as many
accidents testified later, and when they summoned Mrs. Sophia
Gaywood for having such seats on the roof of one of her Bayswater
omnibuses, they obtained a conviction. But Mrs. Gaywood, like most
ladies who have been omnibus proprietors, before and since her
time, was rather fond of litigation, and appealed against the
conviction. Mr. Wilson of Islington, and other leading omnibus
proprietors, gave evidence in her favour, and finally the appeal was
allowed and the conviction quashed.
On March 13, 1851, a new patent omnibus was placed on the
Bayswater and Charing Cross road. Each passenger had a seat
entirely to himself, and every seat was shut off and as secluded as a
private box at the theatre. But its career was short. So was that of
the London Conveyance Company, which ran omnibuses to the Bank,
viâ Holborn. This Company’s vehicles had the initials L.C.C. painted
on them, but not in such large letters as the London County Council
have on their omnibuses.
In October of the same year a meeting of London omnibus
proprietors was held at the Duke of Wellington, Bathurst Street,
Argyle Square, to consider a suggestion made by Mr. Crawford, the
originator of the Hungerford and Camden Town Association—now
known as the Camden Town Association—for choosing and working
new routes at cheap fares. The Hungerford and Camden Town
Association, and one or two similar bodies, had come into existence
a few years previously through the omnibus proprietors arriving at
the conclusion that it would be more remunerative to cease their
fierce struggles one with another, and to work harmoniously
together. They ran their omnibuses at regular intervals, and the
coachmen and conductors were strictly ordered to keep their time. It
was an excellent idea, although it afforded little satisfaction to
lawyers, many of whom had grown prosperous on the quarrels of
omnibus proprietors.
But a reduction in legal expenses was by no means the only saving
effected by the amalgamation. Office and management expenses
were reduced considerably. The conductors, instead of being
engaged by the various proprietors, were now employed and
controlled by the secretary of the Association.
At the meeting at the Duke of Wellington new lines were decided
upon, the most important one being from Bayswater to the Bank—
fourpence all the way, with intermediate twopenny fares. Twenty
omnibuses, the majority built by Messrs. Rock and Gowar, were
placed on that road, and were successful from the day of starting.
The Associations now in existence are—
Atlas and Waterloo Omnibus Association.
Camden Town ” ”
John Bull ” ”
King’s Cross and Barnsbury ” ”
King’s Cross and Victoria ” ”
Victoria Station ” ”
Westminster ” ”
Nearly all of the above were in existence before the London General
Omnibus Company was started. The Atlas and Waterloo is the
largest of the Associations, and its omnibuses run as far south as
Gipsy Hill and north as Finchley. Moreover, it claims, and its claim
cannot be disputed, to have the prettiest omnibus route in London.
That route is from Oxford Circus to Hendon, viâ Finchley Road,
Child’s Hill and Golder’s Green.
OMNIBUS BUILT BY ROCK AND GOWAR.
When the first “Times” had proved a success, Mr. Tilling started
omnibuses on other roads, and before many years had elapsed there
was no name better known to South Londoners than his. At that
period it was the morning custom of South London omnibuses to go
round the streets, in the district from which they started, to pick up
their regular riders at their houses; but Mr. Tilling would not conform
to this practice. He made it known that his omnibuses would not
collect passengers, but would start from a certain place at a stated
time, and people understood that if they wanted to travel by them
they would have to go to the starting-place.
Mr. Tilling was by no means an omnibus proprietor only. Before he
had been established many years he was the owner of coaches,
cabs, wedding carriages, and, in short, carried on the ordinary
business of a job master. On Derby Day he had, usually, as many as
two hundred horses on the course, and although he was present at
Epsom thirty consecutive years, he had always so much to attend to
that he never once saw the great race run. In fact, on one occasion,
when he got back to Peckham, he surprised his chief clerk, who had
been in the office all day, by asking what horse had won. After that
it need scarcely be said that Mr. Tilling did not indulge in betting.
Indeed, betting and swearing were practices which he would not
tolerate among his men, although he was one of the most
considerate employers that ever lived. Unspoiled by success,
unostentatiously charitable and simple in his tastes, he was held in
the highest esteem by every man in his employ, and when he died,
in 1893, the loss was felt by each of them to be a personal one.
There exists, at Messrs. Tilling’s chief offices, a good-sized room
containing a pleasing testimony to the interest which the founder of
the firm took in his employees. Mr. Tilling, many years ago, ordered
that a photograph should be taken, and hung in that room, of every
man who had been in his employ for twenty years. As other men
completed their twenty years’ service their photographs were taken
and added to the collection, and now—for the practice is still
maintained—the walls are covered with them.
Many of the men whose photographs adorn the room have been in
the Tillings’ employ for nearly half a century. One of their “Times”
coachmen, whose face is very familiar to frequenters of Regent
Street, has driven an omnibus through that thoroughfare for over
forty years. His brother has been in the same employ for a still
longer period. The office also has its representatives of long service,
one gentleman having been engaged there nearly forty years.
Mr. Tilling, as already stated, began business with one horse, but the
limited liability company which bears his name has now a stud of
over four thousand, and possesses one hundred and sixty
omnibuses. The horse with which Mr. Tilling started business was a
grey, and for many years, in fact until he was compelled by his
customers’ requirements to break the rule, he would purchase no
horses that were not of that colour.