Following and building upon How To GraphQL Tutorial
Currently building hackernews-node backend of project
npx prisma init
Note that in this repo this command has already been run.
When it was run, it created a directory called prisma
that contained a file called schema.prisma
. In this repo this file has since been modified.
The pervious command also created the file .env
. Please note that.env
is NOT included in this repo as it can contain secret tokens. Also note that .env
has been added to .gitignore
to prevent the accidental pushing of .env
to a public repo.
Renameexample.env
to .env
There are currently 2 variables in this file that are used by this application. Alter them as required.
Create/update db
npx prisma migrate dev --name "init" --preview-feature
--name
is an optional param
Creates node_modules/@prisma/client
. This is imported into the code
npx prisma generate
npx prisma studio
node src/index.js
query {
info
}
mutation {
signup(email: "hi@example.com", password: "Pa$$w0rd", name: "Jane Doe") {
token
user {
id
email
name
}
}
}
mutation {
login(email: "hi@example.com",
password: "Pa$$w0rd") {
token
user {
id
email
name
}
}
}
Make not of token
In some of the following requests you will need to mimic being logged in, to do so:
In HTTP HEADERS add the following, replacing __TOKEN__
with the previously noted token
{
"Authorization": "Bearer __TOKEN__"
}
mutation {
post(url: "www.example.com",
description: "My first post") {
id
url
description
}
}
query{
users {
count
users {
id
name
email
}
}
}
query{
feed {
count
links {
id
description
url
postedBy {
name
}
}
}
}
Once you have a few links in your feed or a few users you can fiter the results like this:
query{
feed(filter: "post", skip: 1, take: 3, orderBy: {createdAt: desc }) {
count
links {
id
description
url
postedBy {
name
}
}
}
}
Filtering: This example looks for all links with either a description or url that contains "post".
Pagination: It skips the first result and limits what is returned to no more than 3 links.
Ordering: Finally, it orders the results by createdAt date in descending order.
mutation {
updateLink(id: 1,
description: "Jane's first post updated") {
id
description
url
}
}
mutation {
deleteLink(id: 2) {
id
}
}
mutation {
vote (linkId: 10) {
id
link {
description
}
user {
id
}
}
}
In the first window/tab run the following.
subscription {
newLink {
id
url
description
postedBy {
id
name
email
}
}
}
In the second window/tab post a new link (4.)
In the first window/tab run the following.
subscription {
newVote {
id
link {
id
description
url
postedBy {
name
}
}
user {
id
name
}
}
}
In the second window/tab vote for a link (10.)