Slide 1

Slide 1 text

SDL as an Artifact: Code-first Schemas and GraphQL Nexus @tgriesser

Slide 2

Slide 2 text

Tim Griesser @tgriesser “Fast, reliable testing for anything that runs in a browser”

Slide 3

Slide 3 text

Slide 4

Slide 4 text

❤ A Brief Timeline

Slide 5

Slide 5 text

2015: graphql-js

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Later 2015 graph.ql 2015: graphql-js

Slide 9

Slide 9 text

Original “SDL First” Library: Released November 2015

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

vs

Slide 14

Slide 14 text

Later 2015 - 2016: graph.ql graphql-tools 2015: graphql-js

Slide 15

Slide 15 text

2015: graphql-js Later 2015 - 2016: graph.ql graphql-tools 2017: Graphene (Python)

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

– Me, when beginning to use Graphene “Oh no, this library doesn’t develop the SDL first”

Slide 18

Slide 18 text

…a few months pass

Slide 19

Slide 19 text

– Me, after spending a few more months in Graphene “Hey, this is actually pretty nice… scales really well to multiple teams”

Slide 20

Slide 20 text

Stats from Generated SDL: 307 object types 129 input types 89 enum types 10 interfaces 5 custom scalars 1 union type 0 custom directives 2542 output fields 581 input fields 717 enum members 35 deprecations ~ 18 developers, 4 teams contributing to the same schema

Slide 21

Slide 21 text

GitHub Public API SDL: 326 object types 90 input types 70 enum types 25 interfaces 8 custom scalars 20 union types 0 custom directives 2085 output fields 307 input fields 299 enum members 6 deprecations https://github.com/octokit/graphql-schema

Slide 22

Slide 22 text

2015: graphql-js Later 2015 - 2016: graph.ql graphql-tools 2017: Graphene (Python) 2018: GraphQL Ruby

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

2015: graphql-js Later 2015 - 2016: graph.ql graphql-tools 2017: Graphene (Python) 2018: GraphQL Ruby Also 2018: graphql-tools + graphql-code-generator

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

– Marc-André Giroux during Q&A after at GraphQL NYC “…I’m not sure - SDL first seems to be unique to the JavaScript community”

Slide 27

Slide 27 text

…it feels like the JavaScript ecosystem is missing out

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Maybe we’re doing it wrong?

Slide 30

Slide 30 text

What might a better “code first” approach look like?

Slide 31

Slide 31 text

GraphQL Nexus: An improved experience building code-first schemas in JavaScript & TypeScript

Slide 32

Slide 32 text

Why not SDL first?

Slide 33

Slide 33 text

GraphQL Nexus Why not SDL first?

Slide 34

Slide 34 text

Related Tooling in the Ecosystem GraphQL Nexus Why not SDL first?

Slide 35

Slide 35 text

Why not SDL first? Related Tooling in the Ecosystem GraphQL Nexus How SDL is Still Incredibly Useful

Slide 36

Slide 36 text

Why not SDL first?

Slide 37

Slide 37 text

Code Duplication

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

“Double Declaration Problem”

Slide 40

Slide 40 text

Implemented 16 times in GitHub’s Public API… Adding a new field with SDL first would mean finding all 16 locations and adding the new field

Slide 41

Slide 41 text

Code Duplication Documentation / Type Ordering

Slide 42

Slide 42 text

Code Duplication Documentation / Type Ordering Building For Larger Teams

Slide 43

Slide 43 text

Code Duplication Documentation / Type Ordering Building For Larger Teams Tooling Complexity

Slide 44

Slide 44 text

Code Duplication Documentation / Type Ordering Building For Larger Teams Tooling Complexity Microservices??

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

https://nexus.js.org

Slide 47

Slide 47 text

Code First

Slide 48

Slide 48 text

Code First Declarative

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Code First Declarative Type-Safe

Slide 52

Slide 52 text

Code First Declarative Type-Safe JavaScript Friendly

Slide 53

Slide 53 text

Code First Declarative Type-Safe JavaScript Friendly Opinionated

Slide 54

Slide 54 text

Code First Declarative Type-Safe JavaScript Friendly Opinionated** **but only slightly, configurable, and hopefully not too controversial

Slide 55

Slide 55 text

What makes it different??

Slide 56

Slide 56 text

Development Feedback Loop

Slide 57

Slide 57 text

“makeSchema”
 Generates Types

Slide 58

Slide 58 text

“makeSchema”
 Generates Types Generated types are 
 Inferred by
 TypeScript Outputs SDL Schema Artifact Creates GraphQL schema Outputs TypeScript Types Imported type “registry” wired up

Slide 59

Slide 59 text

“makeSchema”
 Generates Types Generated types are 
 Inferred by
 TypeScript Changes to
 Schema, Restart 
 the Server IDE Auto-Completion & Type Checking Optional for JS development

Slide 60

Slide 60 text

“makeSchema”
 Generates Types Generated types are 
 Inferred by
 TypeScript Changes to
 Schema, Restart 
 the Server Regenerates SDL & Types files

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Type-Safety

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Generated in Development

Slide 65

Slide 65 text

Generates a normal schema

Slide 66

Slide 66 text

How? Conditional Types + Global Declaration Merging

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

What’s the resolver return type?

Slide 71

Slide 71 text

What’s the resolver return type? "Droid", "primaryFunction"

Slide 72

Slide 72 text

What’s the resolver return type (continued)?

Slide 73

Slide 73 text

What’s the resolver return type (continued)? You don’t need to worry about this…

Slide 74

Slide 74 text

Does the field need a resolver?

Slide 75

Slide 75 text

End Result: Type-safety with less typing

Slide 76

Slide 76 text

Dynamic Methods: Building custom, type-safe abstractions

Slide 77

Slide 77 text

Connections:

Slide 78

Slide 78 text

Customize object construction for your use cases

Slide 79

Slide 79 text

Customize object construction for your use cases

Slide 80

Slide 80 text

Auto-generated

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Makes something previously complicated simple

Slide 83

Slide 83 text

Nexus Plugins: User-defined, type-safe additions

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Augmenting library behavior (runtime and types) based on user defined constructs

Slide 87

Slide 87 text

Example: nexus-prisma +

Slide 88

Slide 88 text

Opinions: Nullability

Slide 89

Slide 89 text

– GraphQL Documentation “... in a GraphQL type system, every field is nullable by default. This is because there are many things which can go awry in a networked service backed by databases and other services. A database could go down, an asynchronous action could fail, an exception could be thrown. Beyond simply system failures, authorization can often be granular, where individual fields within a request can have different authorization rules.”

Slide 90

Slide 90 text

Exceptional States Should be Exceptional: “null” to indicate network failure should be declared & documented

Slide 91

Slide 91 text

– GraphQL Documentation “... in a GraphQL type system, every field is nullable by default. This is because there are many things which can go awry in a networked service backed by databases and other services. A database could go down, an asynchronous action could fail, an exception could be thrown. Beyond simply system failures, authorization can often be granular, where individual fields within a request can have different authorization rules.”

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Configurable, schema level or type level

Slide 94

Slide 94 text

The SDL Converter

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Paste your SDL endpoint thanks @swcarlosrj

Slide 97

Slide 97 text

The Bad Parts Rough Edges

Slide 98

Slide 98 text

“makeSchema”
 Generates Types Generated types are 
 Inferred by
 TypeScript Changes to
 Code, Restarts
 the Server Requires Some Configuration ?? Chicken / Egg Problem TypeScript errors are sometimes cryptic Dynamic Types & Jump To Code

Slide 99

Slide 99 text

Other Code-First Tooling

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

What’s Next?

Slide 104

Slide 104 text

“Dynamic Methods” Type-Safe “Plugin” System Progammatic Directives Better interop with SDL snippets

Slide 105

Slide 105 text

SDL is Still Incredibly Useful

Slide 106

Slide 106 text

Prototyping New Features

Slide 107

Slide 107 text

Common Language

Slide 108

Slide 108 text

Users don’t see our code, they just want a good experience on the web

Slide 109

Slide 109 text

Pick whichever tools help you solve problems

Slide 110

Slide 110 text

Thanks! https://nexus.js.org @tgriesser