4. Fundamental Properties · An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query Language Tutorial at ISWC 2019, October 27, 2019 Olaf Hartig ... Github’s

Post on 23-May-2020

6 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

An Introduction to GraphQLTutorial at ISWC 2019, October 27, 2019

4. Fundamental PropertiesOlaf Hartiga, Ruben Taelmanb

(a) Dept. of Computer and Information Science, Linköping University, Sweden

(b) Ghent University – imec – IDLab, Belgium

Joint work with Jorge Pérezfrom the Universidad de Chile

Based on: O Hartig and J Pérez: Semantics and Complexity of GraphQL. In Proceedings of The Web Conference 2018 (WWW 2018).

2An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

3An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Our Contributions in a Nutshell

Formal definition of the language

Study of computational complexity(the language admits really efficient evaluation methods)

Solution to the problem of large results

4An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

How is the language defined in the spec?

5An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

How is the language defined in the spec?

6An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

How is the language defined in the spec?

7An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

How is the language defined in the spec?

8An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

How is the language defined in the spec?

Formalization of GraphQL

10An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

11An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

Typed nodes

12An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

Typed nodes

One special node

13An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

Typed nodes

One special node

Node properties

14An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

Typed nodes

One special node

Node properties

Edge properties

15An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

GraphQL Graphs (Our Formalization)

Typed nodes

One special node

Node properties

Edge properties

We also formalize the notions of GraphQL schema and schema satisfaction based on this data model

16An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

17An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

⟦friends { name }⟧u =

18An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

= friends: [ ]⟦friends { name }⟧u

19An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

friends: [ { ⟦name⟧v} { ⟦name⟧w} ]=⟦friends { name }⟧u

20An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

= friends: [ {name:R2-D2} {name:Han} ]⟦friends { name }⟧u

21An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

⟦ ⟧hero[episode:EMPIRE] { friends {name} }

22An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

⟦ ⟧rhero[episode:EMPIRE] { friends {name} }

23An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

⟦ ⟧rhero[episode:EMPIRE] { friends {name} } = hero: { ⟦ friends {name} ⟧u }

24An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Formalization of Query Evaluation Function

⟦ ⟧rhero[episode:EMPIRE] { friends {name} } = hero: { ⟦ friends {name} ⟧u }

= hero: { friends: [ {name:R2-D2} {name:Han} ] }

Complexity Analysis

Evaluation Problem

26An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Evaluation Problem of GraphQL

GraphQL query qGraphQL graph G

data value d

yes no

Does d occur in theresult of q over G?

Does d occur in theresult of q over G?

27An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Complexity ClassesP

SP

AC

E

NP

P

28An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Complexity of Evaluation ProblemsP

SP

AC

E

NP

P

Relational Algebra

SPARQL Conjunctive Queries

BGPs (SPARQL)

29An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Complexity of Evaluation ProblemsP

SP

AC

E

NP

P NL

GraphQL

Relational Algebra

SPARQL Conjunctive Queries

BGPs (SPARQL)

Complexity Analysis

Enumeration Problem

31An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Non-Redundancy

hero {name: Lukefriends: [

{ name: R2-D2}{ name: Han}

]id: 1000name: Lukefriends: [

{ id: 2001}{ id: 1002}

]}

hero(episode: EMPIRE) {namefriends {

name}idnamefriends {

id}

}

Valid query Invalid result

32An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Non-Redundancy

hero(episode: EMPIRE) {namefriends {

name}idnamefriends {

id}

}

Valid query

hero {name: Lukefriends: [

{ name: R2-D2 Id: 2001 }{ name: Han Id: 1002 }

]id: 1000

}

Fields are collected before answering

Correct result

33An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Non-Redundancy

hero {name: Lukefriends: [

{ name: R2-D2 Id: 2001 }{ name: Han Id: 1002 }

]id: 1000

}

hero(episode: EMPIRE) {namefriends {

nameid

}id

}

Fields are collected before answering

Non-redundant query Correct result

34An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Another Complication: Type Restrictions

hero(episode: EMPIRE) {namefriends {

on Droid { name }on Human { id }name

}}

Valid query Invalid result

hero {name: Lukefriends: [

{ name: R2-D2 name: R2-D2 }{ id: 1002 name: Han }

]}

35An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Another Complication: Type Restrictions

hero(episode: EMPIRE) {namefriends {

on Droid { name }on Human { id }name

}}

Valid query Correct result

hero {name: Lukefriends: [

{ name: R2-D2 }{ id: 1002 name: Han }

]}

36An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Another Complication: Type Restrictions

hero(episode: EMPIRE) {namefriends {

on Droid { name }on Human { id name }

}}

Valid query Correct result

hero {name: Lukefriends: [

{ name: R2-D2 }{ id: 1002 name: Han }

]}

37An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Ground-Typed Normal Form

hero(episode: EMPIRE) {namefriends {

on Droid { name }on Human { id name }

}}

Ground-typed query Correct result

hero {name: Lukefriends: [

{ name: R2-D2 }{ id: 1002 name: Han }

]}

38An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Eliminating Redundancies

Rewriting rules for queries

Every GraphQL query q can be rewritteninto a query q’ that is i) non-redundant andii) in ground-typed normal form, such that q ≡ q’

Advantage: field collection is not needed for non-redundant queries in ground-typed NF

39An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Now to the Enumeration Problem

Let q be i) non-redundant andii) in ground-typed normal form

Result of q can be produced symbol by symbol with only constant time between symbols

Time to produce the complete query resultdepends linearly on the size of this result

hero { friends: [ { name:R2-D2} ] }

Complexity Analysis

Result Size

41An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Results of GraphQL queries can be huge

start { knows { knows { … { knows { name } }… } } }

Alice appears 2N times in the result

2N times

42An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Huge results in practice: Github’s GraphQL API

Owners of first five repos that user “danbri” contributes to, and the owners of first five repos that they contribute to, and so on...

43An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result sizes can be computed efficiently!!!

Let q be i) non-redundant andii) in ground-typed normal form

Time to compute the size of the resultof q over a graph G depends linearly onthe product (size of q) × (size of G)

We provide an algorithm thatachieves this complexity bound

44An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Proposal for GraphQL Servers

First, compute the size of the result.

If too big, reject query.

Else, inform the size to the client, and

Send the result byte by byte.

(or use the size as basis of a billing model)

Summary

46An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Our Results in a Nutshell

Formal definition of the language● Property Graph-like data model● Formal query semantics

Study of computational complexity(the language admits really efficient evaluation methods)

● Evaluation problem is NL-complete● Enumeration of results is linear

Solution to the problem of large results● Efficient algorithm to compute result size

Olaf Hartig and Jorge Pérez: Semantics and Complexity of GraphQL. In The Web Conference 2018.

www.liu.se

Backup Slides

49An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q2 q3

start: { … … }

Result:

50An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

size(q2 ,u) = size(q3 ,u) =

q3q2

51An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) size(q3 ,u) =

q4

q2

52An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) size(q3 ,u) =

q5

size(q4 ,v) = 4 + size(q5 ,w)

q4

q2

53An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) size(q3 ,u) =

q5

size(q4 ,v) = 4 + size(q5 ,w)

q4

size(q5 ,w) = 3

size(q5) = 3

q2

54An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) size(q3 ,u) =

q5

size(q4 ,v) = 4 + size(q5 ,w) = 4 + 3 = 7

q4

size(q5 ,w) = 3

size(q4) = 7 size(q5) = 3

q2

55An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) = 11 size(q3 ,u) =

q5

size(q4 ,v) = 4 + size(q5 ,w) = 4 + 3 = 7

q4

size(q5 ,w) = 3

size(q4) = 7 size(q5) = 3

size(q2) = 11

q2

56An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) = 11 size(q3 ,u)= 4 + size(q4 ,v)

q5

size(q4 ,v) = 4 + size(q5 ,w) = 4 + 3 = 7

q4

size(q5 ,w) = 3

size(q4) = 7 size(q5) = 3

size(q2) = 11q4

q2

57An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u)

q3

size(q2 ,u) = 4 + size(q4 ,v) = 11 size(q3 ,u)= 4 + size(q4 ,v) = 4 + 7 = 11

q5

size(q4 ,v) = 4 + size(q5 ,w) = 4 + 3 = 7

q4

size(q5 ,w) = 3

size(q4) = 7 size(q5) = 3

q4

size(q2) = 11size(q3) = 11

q2

58An Introduction to GraphQL 4. Fundamental Properties of the GraphQL Query LanguageTutorial at ISWC 2019, October 27, 2019 Olaf Hartig

Result-Size Computation

start { advisor { univ { name } } friend { univ { name } } }

size(q, r) = 4 + size(q2 ,u) + size(q3 ,u) = 26

q3

size(q2 ,u) = 4 + size(q4 ,v) = 11 size(q3 ,u)= 4 + size(q4 ,v) = 4 + 7 = 11

q5

size(q4 ,v) = 4 + size(q5 ,w) = 4 + 3 = 7

q4

size(q5 ,w) = 3

size(q4) = 7 size(q5) = 3

size(q2) = 11size(q3) = 11 q4

q2

11 11

top related