SlideShare a Scribd company logo
EdgeQL: a primer
Victor Petrovykh
February 10, 2022
Let’s model the schema for something familiar, like a Netflix clone.
Specifically, we concentrate on the part of the schema that represents the
movie library. We ignore all the complexities of accounts handling for this
primer.
2
Let’s Build a Schema
At the bare minimum we need movies with a title and maybe their release
year.
3
type Movie {
required property title -> str;
property release_year -> int32;
}
Start Simple
Movies should have a director.
4
type Movie {
required property title -> str;
property release_year -> int32;
}
type Movie {
required property title -> str;
property release_year -> int32;
}
Add Relationships
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
}
Movies should also have some actors listed.
5
More Relationships
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
}
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person;
}
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person;
}
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
It may be useful to add the names of the characters the actors played.
6
Enrich Relationships
In addition to the movies we also have shows with multiple episodes.
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
type Show {
required property title -> str;
property num_episodes -> int32;
multi link actors -> Person {
property character_name -> str;
}
}
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
More Type Variants
7
To keep duplication minimal we can refactor our Movie and Series.
Refactor
8
type Show {
required property title -> str;
property num_episodes -> int32;
multi link actors -> Person {
property character_name -> str;
}
}
type Person {
property name -> str;
}
type Movie {
required property title -> str;
property release_year -> int32;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
type Movie extending Content {
property release_year -> int32;
link director -> Person;
}
type Show extending Content {
property num_episodes -> int32;
}
type Person {
property name -> str;
}
abstract type Content {
required property title -> str;
multi link actors -> Person {
property character_name -> str;
}
}
Let’s add some constraints to nip unreasonable values in the bud.
type Movie extending Content {
property release_year -> int32;
}
type Show extending Content {
property num_episodes -> int32;
}
type Person {
property name -> str;
}
abstract type Content {
required property title -> str;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
Add Constraints
9
type Movie extending Content {
property release_year -> int32 {
constraint min_value(1900);
}
}
type Show extending Content {
property num_episodes -> int32 {
constraint min_value(2);
}
}
type Person {
property name -> str;
}
abstract type Content {
required property title -> str;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
Querying Nested Data
We can start with a query that gets us all the movie information: title,
release year, director and actors.
select Movie; [
{"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560"},
{"id": "a8e5ff2a-8a06-11ec-99a1-8b718b72ffc4"},
{"id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d"},
{"id": "a8651108-8a06-11ec-99a1-db7a7e621bfc"},
{"id": "a868f052-8a06-11ec-99a1-0779a9198d5d"},
...
10
select Movie {
id,
title,
release_year
};
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow"
},
{
"id": "a8e5ff2a-8a06-11ec-99a1-8b718b72ffc4",
"release_year": 2019,
"title": "Spider-Man: Far From Home"
},
...
select Movie {
id,
title,
release_year,
director: {
name
},
actors: {
name
}
};
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow",
"director": {"name": "Cate Shortland"},
"actors": [
{"name": "Scarlett Johansson"},
{"name": "Florence Pugh"},
{"name": "Ray Winstone"},
{"name": "Olga Kurylenko"}
]
},
...
select Movie {
id,
title,
release_year,
director: {
name
},
actors: {
@character_name,
name
}
} filter Movie.title = 'Black Widow';
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow",
"director": {"name": "Cate Shortland"},
"actors": [
{
"@character_name": "Black Widow",
"name": "Scarlett Johansson"
},
{
"@character_name": "Yelena Belova",
"name": "Florence Pugh"
},
...
select Movie {
id,
title,
release_year,
director: {
name
},
actors: {
@character_name,
name
}
} filter .title = 'Black Widow';
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow",
"director": {"name": "Cate Shortland"},
"actors": [
{
"@character_name": "Black Widow",
"name": "Scarlett Johansson"
},
{
"@character_name": "Yelena Belova",
"name": "Florence Pugh"
},
...
Polymorphic Queries
Let’s tweak our query to provide us the kinds of results we may want to
see in suggestions.
11
select Movie {
id,
title,
release_year,
director: { name }
}
filter .actors.name =
'Scarlett Johansson';
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow",
"director": {"name": "Cate Shortland"}
},
{
"id": "a8921e64-8a06-11ec-99a1-d3e990178505",
"release_year": 2016,
"title": "Captain America: Civil War",
...
select Movie {
id,
title,
release_year,
director: { name }
}
filter .actors.name =
'Scarlett Johansson'
order by .release_year desc
limit 5;
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"release_year": 2021,
"title": "Black Widow",
"director": {"name": "Cate Shortland"}
},
{
"id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d",
"release_year": 2019,
"title": "Avengers: Endgame",
...
select Content {
id,
title,
[is Show].num_episodes,
[is Movie].release_year,
[is Movie].director: { name }
}
filter .actors.name =
'Scarlett Johansson'
order by .release_year desc
limit 5;
[
{
"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560",
"num_seasons": null,
"release_year": 2021,
"title": "Black Widow"
"director": {"name": "Cate Shortland"},
},
{
"id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d",
"num_seasons": null,
"release_year": 2019,
"title": "Avengers: Endgame"
...
Querying Custom Data
We probably want to be able to display some information about the actors,
too. But don’t they only have one property?
12
select Person {
id,
name,
name_parts :=
str_split(.name, ' ')
}
filter .name = 'Scarlett Johansson';
[
{
"id": "a8328530-8a06-11ec-99a1-6b35f115a4eb",
"name": "Scarlett Johansson",
"name_parts": ["Scarlett", "Johansson"]
}
]
select Person {
id,
name,
name_parts :=
str_split(.name, ' '),
movies_count := count(
(select Movie
filter .actors = Person)
)
}
filter .name = 'Scarlett Johansson';
[
{
"id": "a8328530-8a06-11ec-99a1-6b35f115a4eb",
"name": "Scarlett Johansson",
"name_parts": ["Scarlett", "Johansson"],
“movies_count”: 8
}
]
select Person {
id,
name,
name_parts :=
str_split(.name, ' '),
movies_count :=
count(.<actors[is Movie])
}
filter .name = 'Scarlett Johansson';
[
{
"id": "a8328530-8a06-11ec-99a1-6b35f115a4eb",
"name": "Scarlett Johansson",
"name_parts": ["Scarlett", "Johansson"],
“movies_count”: 8
}
]
That backlink seems useful, so let’s just add it to the schema.
type Movie extending Content {
property release_year -> int32 {
constraint min_value(1900);
}
}
type Show extending Content {
property num_episodes -> int32 {
constraint min_value(2);
}
}
type Person {
property name -> str;
}
abstract type Content {
required property title -> str;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
Add Computed Link
13
type Movie extending Content {
property release_year -> int32 {
constraint min_value(1900);
}
}
type Show extending Content {
property num_episodes -> int32 {
constraint min_value(2);
}
}
type Person {
property name -> str;
link in_movies :=
.<actors[is Movie];
}
abstract type Content {
required property title -> str;
link director -> Person;
multi link actors -> Person {
property character_name -> str;
}
}
Fine-Tuned Queries
Let’s add the latest 3 movies the actor appeared in to the data we fetch.
14
select Person {
id,
name,
movies_count := count(.in_movies)
}
filter .name = 'Scarlett Johansson';
[
{
"id": "a8328530-8a06-11ec-99a1-6b35f115a4eb",
"movies_count": 8,
"name": "Scarlett Johansson"
}
]
select Person {
id,
name,
movies_count := count(.in_movies),
in_movies: {
title,
release_year
}
order by .release_year desc
limit 3
}
filter .name = 'Scarlett Johansson';
[
{
"in_movies": [
{
"release_year": 2021,
"title": "Black Widow"
},
{
"release_year": 2019,
"title": "Avengers: Endgame"
},
{
"release_year": 2018,
"title": "Avengers: Infinity War"
}
],
"id": "a8328530-8a06-11ec-99a1-6b35f115a4eb",
"movies_count": 8,
"name": "Scarlett Johansson"
}
]
Insert Data
Of course you can also insert, update and delete data.
So let’s look at a nice simple nested insert here.
15
insert Movie {
title := 'Dune',
release_year := 2021,
director := (
insert Person {
name := 'Denis Villeneuve'
}
)
};
[{"id": "4bfef230-8a56-11ec-a746-e310c2aa2b6e"}]
Many More Features
16
this primer
scalar constraints
alias
function
index
annotation
EdgeQL

More Related Content

Similar to EdgeQL — A primer

SQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer CentreSQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer Centre
jatin batra
 
Introduction to Cypher
Introduction to Cypher Introduction to Cypher
Introduction to Cypher
Neo4j
 
Please complete the following the function query which requires.pdf
Please complete the following the function query which requires.pdfPlease complete the following the function query which requires.pdf
Please complete the following the function query which requires.pdf
amarnathmahajansport
 
Formai sec
Formai secFormai sec
Formai sec
ttasi86
 
Sql success ch04
Sql success ch04Sql success ch04
Sql success ch04
roughsea
 
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
Doris Chen
 
Data Science - The Most Profitable Movie Characteristic
Data Science -  The Most Profitable Movie CharacteristicData Science -  The Most Profitable Movie Characteristic
Data Science - The Most Profitable Movie Characteristic
Cheah Eng Soon
 
Beyond Breakpoints: Advanced Debugging with XCode
Beyond Breakpoints: Advanced Debugging with XCodeBeyond Breakpoints: Advanced Debugging with XCode
Beyond Breakpoints: Advanced Debugging with XCode
Aijaz Ansari
 
20101217 mtg
20101217 mtg20101217 mtg
20101217 mtg
riamaehb
 

Similar to EdgeQL — A primer (9)

SQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer CentreSQL Training in Ambala ! Batra Computer Centre
SQL Training in Ambala ! Batra Computer Centre
 
Introduction to Cypher
Introduction to Cypher Introduction to Cypher
Introduction to Cypher
 
Please complete the following the function query which requires.pdf
Please complete the following the function query which requires.pdfPlease complete the following the function query which requires.pdf
Please complete the following the function query which requires.pdf
 
Formai sec
Formai secFormai sec
Formai sec
 
Sql success ch04
Sql success ch04Sql success ch04
Sql success ch04
 
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Techn...
 
Data Science - The Most Profitable Movie Characteristic
Data Science -  The Most Profitable Movie CharacteristicData Science -  The Most Profitable Movie Characteristic
Data Science - The Most Profitable Movie Characteristic
 
Beyond Breakpoints: Advanced Debugging with XCode
Beyond Breakpoints: Advanced Debugging with XCodeBeyond Breakpoints: Advanced Debugging with XCode
Beyond Breakpoints: Advanced Debugging with XCode
 
20101217 mtg
20101217 mtg20101217 mtg
20101217 mtg
 

Recently uploaded

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 

Recently uploaded (20)

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 

EdgeQL — A primer

  • 1. EdgeQL: a primer Victor Petrovykh February 10, 2022
  • 2. Let’s model the schema for something familiar, like a Netflix clone. Specifically, we concentrate on the part of the schema that represents the movie library. We ignore all the complexities of accounts handling for this primer. 2 Let’s Build a Schema
  • 3. At the bare minimum we need movies with a title and maybe their release year. 3 type Movie { required property title -> str; property release_year -> int32; } Start Simple
  • 4. Movies should have a director. 4 type Movie { required property title -> str; property release_year -> int32; } type Movie { required property title -> str; property release_year -> int32; } Add Relationships type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; }
  • 5. Movies should also have some actors listed. 5 More Relationships type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; } type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person; }
  • 6. type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person; } type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person { property character_name -> str; } } It may be useful to add the names of the characters the actors played. 6 Enrich Relationships
  • 7. In addition to the movies we also have shows with multiple episodes. type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person { property character_name -> str; } } type Show { required property title -> str; property num_episodes -> int32; multi link actors -> Person { property character_name -> str; } } type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person { property character_name -> str; } } More Type Variants 7
  • 8. To keep duplication minimal we can refactor our Movie and Series. Refactor 8 type Show { required property title -> str; property num_episodes -> int32; multi link actors -> Person { property character_name -> str; } } type Person { property name -> str; } type Movie { required property title -> str; property release_year -> int32; link director -> Person; multi link actors -> Person { property character_name -> str; } } type Movie extending Content { property release_year -> int32; link director -> Person; } type Show extending Content { property num_episodes -> int32; } type Person { property name -> str; } abstract type Content { required property title -> str; multi link actors -> Person { property character_name -> str; } }
  • 9. Let’s add some constraints to nip unreasonable values in the bud. type Movie extending Content { property release_year -> int32; } type Show extending Content { property num_episodes -> int32; } type Person { property name -> str; } abstract type Content { required property title -> str; link director -> Person; multi link actors -> Person { property character_name -> str; } } Add Constraints 9 type Movie extending Content { property release_year -> int32 { constraint min_value(1900); } } type Show extending Content { property num_episodes -> int32 { constraint min_value(2); } } type Person { property name -> str; } abstract type Content { required property title -> str; link director -> Person; multi link actors -> Person { property character_name -> str; } }
  • 10. Querying Nested Data We can start with a query that gets us all the movie information: title, release year, director and actors. select Movie; [ {"id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560"}, {"id": "a8e5ff2a-8a06-11ec-99a1-8b718b72ffc4"}, {"id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d"}, {"id": "a8651108-8a06-11ec-99a1-db7a7e621bfc"}, {"id": "a868f052-8a06-11ec-99a1-0779a9198d5d"}, ... 10 select Movie { id, title, release_year }; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow" }, { "id": "a8e5ff2a-8a06-11ec-99a1-8b718b72ffc4", "release_year": 2019, "title": "Spider-Man: Far From Home" }, ... select Movie { id, title, release_year, director: { name }, actors: { name } }; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow", "director": {"name": "Cate Shortland"}, "actors": [ {"name": "Scarlett Johansson"}, {"name": "Florence Pugh"}, {"name": "Ray Winstone"}, {"name": "Olga Kurylenko"} ] }, ... select Movie { id, title, release_year, director: { name }, actors: { @character_name, name } } filter Movie.title = 'Black Widow'; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow", "director": {"name": "Cate Shortland"}, "actors": [ { "@character_name": "Black Widow", "name": "Scarlett Johansson" }, { "@character_name": "Yelena Belova", "name": "Florence Pugh" }, ... select Movie { id, title, release_year, director: { name }, actors: { @character_name, name } } filter .title = 'Black Widow'; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow", "director": {"name": "Cate Shortland"}, "actors": [ { "@character_name": "Black Widow", "name": "Scarlett Johansson" }, { "@character_name": "Yelena Belova", "name": "Florence Pugh" }, ...
  • 11. Polymorphic Queries Let’s tweak our query to provide us the kinds of results we may want to see in suggestions. 11 select Movie { id, title, release_year, director: { name } } filter .actors.name = 'Scarlett Johansson'; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow", "director": {"name": "Cate Shortland"} }, { "id": "a8921e64-8a06-11ec-99a1-d3e990178505", "release_year": 2016, "title": "Captain America: Civil War", ... select Movie { id, title, release_year, director: { name } } filter .actors.name = 'Scarlett Johansson' order by .release_year desc limit 5; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "release_year": 2021, "title": "Black Widow", "director": {"name": "Cate Shortland"} }, { "id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d", "release_year": 2019, "title": "Avengers: Endgame", ... select Content { id, title, [is Show].num_episodes, [is Movie].release_year, [is Movie].director: { name } } filter .actors.name = 'Scarlett Johansson' order by .release_year desc limit 5; [ { "id": "a8e8f31a-8a06-11ec-99a1-f7de41ca9560", "num_seasons": null, "release_year": 2021, "title": "Black Widow" "director": {"name": "Cate Shortland"}, }, { "id": "a8cdecfa-8a06-11ec-99a1-d3ed2d4fe65d", "num_seasons": null, "release_year": 2019, "title": "Avengers: Endgame" ...
  • 12. Querying Custom Data We probably want to be able to display some information about the actors, too. But don’t they only have one property? 12 select Person { id, name, name_parts := str_split(.name, ' ') } filter .name = 'Scarlett Johansson'; [ { "id": "a8328530-8a06-11ec-99a1-6b35f115a4eb", "name": "Scarlett Johansson", "name_parts": ["Scarlett", "Johansson"] } ] select Person { id, name, name_parts := str_split(.name, ' '), movies_count := count( (select Movie filter .actors = Person) ) } filter .name = 'Scarlett Johansson'; [ { "id": "a8328530-8a06-11ec-99a1-6b35f115a4eb", "name": "Scarlett Johansson", "name_parts": ["Scarlett", "Johansson"], “movies_count”: 8 } ] select Person { id, name, name_parts := str_split(.name, ' '), movies_count := count(.<actors[is Movie]) } filter .name = 'Scarlett Johansson'; [ { "id": "a8328530-8a06-11ec-99a1-6b35f115a4eb", "name": "Scarlett Johansson", "name_parts": ["Scarlett", "Johansson"], “movies_count”: 8 } ]
  • 13. That backlink seems useful, so let’s just add it to the schema. type Movie extending Content { property release_year -> int32 { constraint min_value(1900); } } type Show extending Content { property num_episodes -> int32 { constraint min_value(2); } } type Person { property name -> str; } abstract type Content { required property title -> str; link director -> Person; multi link actors -> Person { property character_name -> str; } } Add Computed Link 13 type Movie extending Content { property release_year -> int32 { constraint min_value(1900); } } type Show extending Content { property num_episodes -> int32 { constraint min_value(2); } } type Person { property name -> str; link in_movies := .<actors[is Movie]; } abstract type Content { required property title -> str; link director -> Person; multi link actors -> Person { property character_name -> str; } }
  • 14. Fine-Tuned Queries Let’s add the latest 3 movies the actor appeared in to the data we fetch. 14 select Person { id, name, movies_count := count(.in_movies) } filter .name = 'Scarlett Johansson'; [ { "id": "a8328530-8a06-11ec-99a1-6b35f115a4eb", "movies_count": 8, "name": "Scarlett Johansson" } ] select Person { id, name, movies_count := count(.in_movies), in_movies: { title, release_year } order by .release_year desc limit 3 } filter .name = 'Scarlett Johansson'; [ { "in_movies": [ { "release_year": 2021, "title": "Black Widow" }, { "release_year": 2019, "title": "Avengers: Endgame" }, { "release_year": 2018, "title": "Avengers: Infinity War" } ], "id": "a8328530-8a06-11ec-99a1-6b35f115a4eb", "movies_count": 8, "name": "Scarlett Johansson" } ]
  • 15. Insert Data Of course you can also insert, update and delete data. So let’s look at a nice simple nested insert here. 15 insert Movie { title := 'Dune', release_year := 2021, director := ( insert Person { name := 'Denis Villeneuve' } ) }; [{"id": "4bfef230-8a56-11ec-a746-e310c2aa2b6e"}]
  • 16. Many More Features 16 this primer scalar constraints alias function index annotation EdgeQL