- create project dir
- run:
gql create prisma-tweets --boilerplate node-basic
to bootstrap a prisma based graphql server - configure datamodel.graphql
type Tweet {
id: ID! @unique
createdAt: DateTime!
text: String!
owner: User!
location: Location!
}
type User {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
handle: String! @unique
name: String
tweets: [Tweet!]!
}
type Location {
latitude: Float!
longitude: Float!
}
-
in this example use local
-
run:
prisma deploy
, choose local. On running the install from first step, this will automatically runprisma deploy
-
returns endpoints
HTTP: http://localhost:4466/prisma-tweets/dev
WS: ws://localhost:4466/prisma-tweets/dev
- every time we rerun
prisma deploy
prisma.graphql
gets updated.
gql get-schema -p database -o schema.graphql
-
src/generated/prisma.graphql (Prisma schema): Defines the Prisma API with CRUD functionality for your database. This file is auto-generated based on your data model and should never be manually altered.
-
Whenever you make changes to your data model, this file is (automatically) updated as well.
- create a dir for resolvers
mkdir src/resolvers/Query.js
- write your first query
- import the query into src/index.js
- run: prisma deploy and yarn dev to update graphql schema and open playground
- make sure to import your types from generated prisma graphql file and use them in schema.grapqhl
const TEMP_APP_SECRET = "mysecret123";
module.exports = {
TEMP_APP_SECRET
};
- output:
- mutation:
mutation {
signup(
email: "[email protected]"
password: "graphql"
username: "idkjs"
fullName: "Alain Armand"
) {
token
user {
id
}
}
}
{
"data": {
"signup": {
"token":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjamQ1d3RucTcwMW82MDE4NXlmYmUzd3dzIiwiaWF0IjoxNTE3NTc0NzQ4fQ.i5rDF7omlnqtWYq6HQLPKwkLqWF6R_kG2OgHY-VNm_Q",
"user": {
"id": "cjd5wtnq701o60185yfbe3wws"
}
}
}
}
- get header from response and use in variables section of playground
- mutation:
mutation {
login(email: "[email protected]", password: "graphql") {
token
}
}
- output:
{
"data": {
"login": {
"token":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjamQ1d3RucTcwMW82MDE4NXlmYmUzd3dzIiwiaWF0IjoxNTE3NTc1MTE3fQ.a7CHdRbLOBOwbB1DvTpk-liUn8xKBjFRTPWS4hBcMT0"
}
}
}
- header
{
"Authorization":
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjamQ1d3RucTcwMW82MDE4NXlmYmUzd3dzIiwiaWF0IjoxNTE3NTc1MTE3fQ.a7CHdRbLOBOwbB1DvTpk-liUn8xKBjFRTPWS4hBcMT0"
}
-
the feed query returns tweets posted on this server, not actually interacting with twitter
-
query
{
feed {
tweets {
id
postedBy {
username
}
body
}
}
}
- output:
{
"data": {
"feed": {
"tweets": [
{
"id": "cjd5x6taj01sy0185djv9t6hu",
"postedBy": {
"username": "idkjs"
},
"body": "https://www.graphqlweekly.com"
}
]
}
}
}
- run:
now --public
- see it here: prisma-tweets.now.sh