Slide 1

Slide 1 text

SDL as an Artifact: Code-First Schemas in TS/JS @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

vs

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

…a few months pass

Slide 18

Slide 18 text

“Hey, this approach is actually very nice… scales really well to multiple teams working on the same schema”

Slide 19

Slide 19 text

Stats from Graphene Generated SDL: ~ 600 types ~ 3000 fields ~ 35 deprecations ~ 0 custom directives ~ 18 developers, 4 teams contributing to the same schema

Slide 20

Slide 20 text

GitHub Public API SDL: ~ 500 types ~ 3000 fields ~ 6 deprecations ~ 0 custom directives Source: https://github.com/octokit/graphql-schema

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 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 24

Slide 24 text

No content

Slide 25

Slide 25 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 26

Slide 26 text

…it feels like the JavaScript ecosystem is missing out

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Maybe we’re doing it wrong?

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Why not SDL first?

Slide 32

Slide 32 text

GraphQL Nexus Why not SDL first?

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Why not SDL first?

Slide 36

Slide 36 text

Code Duplication

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

“Double Declaration Problem”

Slide 39

Slide 39 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 40

Slide 40 text

Code Duplication Documentation / Type Ordering

Slide 41

Slide 41 text

Keeps your generated SDL consistent, no need to worry about merge conflicts or making sure fields & types you add are in the right place in a handwritten SDL file

Slide 42

Slide 42 text

Code Duplication Documentation / Type Ordering Building For Larger Teams

Slide 43

Slide 43 text

Budget Team Search Team Expenses Team Integrations Team Schema(types) Web Client API Mobile

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

https://nexus.js.org

Slide 48

Slide 48 text

Code First

Slide 49

Slide 49 text

Code First Declarative

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Code First Declarative Type-Safe

Slide 53

Slide 53 text

Code First Declarative Type-Safe JavaScript Friendly

Slide 54

Slide 54 text

Code First Declarative Type-Safe JavaScript Friendly Extensible

Slide 55

Slide 55 text

What makes it different??

Slide 56

Slide 56 text

Development Feedback Loop

Slide 57

Slide 57 text

makeSchema({ types })

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Generated types are 
 Inferred by
 TypeScript Changes to
 Schema, Restart 
 the Server IDE Auto-Completion & Type Checking Optional for JS development makeSchema({ types })

Slide 60

Slide 60 text

Generated types are 
 Inferred by
 TypeScript Changes to
 Schema, Restart 
 the Server Regenerates SDL & Types files makeSchema({ types })

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

No content

Slide 65

Slide 65 text

Generated in Development

Slide 66

Slide 66 text

Generates a normal schema

Slide 67

Slide 67 text

How? Conditional Types + Global Declaration Merging

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

What’s the resolver return type?

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

What’s the resolver return type (continued)?

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

End Result: Type-safety with less typing

Slide 76

Slide 76 text

Extensibility & Plugins: Building custom, type-safe abstractions

Slide 77

Slide 77 text

fieldAuthorizePlugin

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Example of a Plugin

Slide 81

Slide 81 text

Plugins: “onMissingType” Allows defining fallback behavior when a requested type does not exist in the schema Dynamic type generation

Slide 82

Slide 82 text

Created “Just in Time” as Nexus is creating the GraphQL schema Plugins: “onMissingType”

Slide 83

Slide 83 text

Dynamic Builder Methods/Properties

Slide 84

Slide 84 text

Connections:

Slide 85

Slide 85 text

Customize object construction for your use cases

Slide 86

Slide 86 text

Customize object construction for your use cases

Slide 87

Slide 87 text

Auto-generated

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Makes something previously complicated simple

Slide 90

Slide 90 text

nexus-prisma +

Slide 91

Slide 91 text

Abstracts CRUD boilerplate based on what they know about your database and how it interacts with the Prisma platform Allows users to override the pieces they care about as they need to evolve, rather than locking you into a certain schema up-front nexus-prisma

Slide 92

Slide 92 text

Augmenting library behavior, both runtime and types based on user defined constructs

Slide 93

Slide 93 text

The SDL Converter

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

The Rough Edges

Slide 96

Slide 96 text

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 The Rough Edges: makeSchema({ types })

Slide 97

Slide 97 text

Overall, though it has seemed to work pretty well

Slide 98

Slide 98 text

Other Code-First Tooling

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

SDL is Still Incredibly Useful

Slide 103

Slide 103 text

Common Language

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

Pick whichever tools help you solve problems

Slide 106

Slide 106 text

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