25
Why UI developers love GraphQL Coursera, 9.20.16

Why UI developers love GraphQL

Embed Size (px)

Citation preview

Page 1: Why UI developers love GraphQL

Why UI developers love GraphQLCoursera, 9.20.16

Page 2: Why UI developers love GraphQL

A special place in the stackRight between frontend and backend devs

Page 3: Why UI developers love GraphQL

Backend devs implement the APIGraphQL is an API technology, so it must make sense to backend people:

- Describe data with strong types- Potential for more efficient responses because of explicit fields- Easy to refactor backend to microservices

What do you get as a frontend dev?

Page 4: Why UI developers love GraphQL

Speed up your UI development workflowGraphQL enables next-generation tools that let you think less about data fetching.

Think about what data you need, and it’s there.

Page 5: Why UI developers love GraphQL

How do you query a GraphQL API?// REST

fetch(`api.server.com/posts/1`).then(...)

// GraphQL - no special tools needed!

fetch(`api.server.com/graphql?

query={ post(id: 1) { title, content } }`).then(...)

Page 6: Why UI developers love GraphQL

Benefits over REST, without special client tools- Know exactly which fields we are going to get- Documentation and data exploration built in (something you usually only

get with BaaS!)- Nested data fetching is trivial

Page 7: Why UI developers love GraphQL

Basic toolingRealizing the query is more than just a string.

Page 8: Why UI developers love GraphQL

Getting at the query structure

- Tag the query to designate that it’s GraphQL- Use a simple regex or AST traversal to find

all of the queries- Alternative: put in .graphql files

Now the GraphQL query is far more than a “magic string” -- it’s a semantic unit of data fetching

const query = gql` query HumanQuery { human(id: "1000") { name height(unit: FOOT) } }`;

Page 9: Why UI developers love GraphQL

Static query validation and analysis

Without running the UI:

- Typos in fields- Wrong arguments- Deprecated fields- Identify field usage

apollostack/eslint-plugin-graphql

Page 10: Why UI developers love GraphQL

In-editor autocomplete

- Today: in IntelliJ- Tomorrow: everywhere

with the upcoming GraphQL language service

jimkyndemeyer/js-graphql-intellij-plugin

Page 11: Why UI developers love GraphQL

It’s super easy to build new toolsGraphQL.js: Facebook’s GraphQL tools platform, includes parsing, validation, traversal, etc: graphql/graphql-js

GraphQL AST: Super nice to work with, matches up with the spec 1:1, you can use AST explorer: astexplorer.net

Introspection: Every GraphQL server is required to provide info in a standard format that works with all tools: graphql.org/learn/introspection/

Page 12: Why UI developers love GraphQL

Caching clientsUsing the query structure on the client at runtime.

Relay

Page 13: Why UI developers love GraphQL

How might we cache REST?1. Use URL as cache key

fetchOrLoadFromCache(`/posts/1`)

2. Write custom normalizer for Redux: decompose responses into objects, store them in separate fields. Might need to handle different endpoints manually, depending on your API design.

Page 14: Why UI developers love GraphQL

Caching GraphQL: Easier or harder?Using URLs or queries isn’t great:

fetchOrLoadFromCache(`/graphql?query={...}`)

But we gain much more potential: A fancy query structure that tells us exactly what fields we’re looking for.

Page 15: Why UI developers love GraphQL

Example: Overlapping queries

query bigQuery { post(id: 1) { title, content author { name, age } }}

query smallQuery { post(id: 1) { title, content }}

Easy to prefetch data and get subsets of previous queries.

Page 16: Why UI developers love GraphQL

Formalize as pathsWhat’s the atom of GraphQL data? A leaf field.

post(id: 1) -> title // bigQuery, smallQuery

post(id: 1) -> content // bigQuery, smallQuery

post(id: 1) -> author -> name // bigQuery

post(id: 1) -> author -> age // bigQuery

Lets us easily reason about the data we have fetched, replaces the URLs from REST. This is how Apollo Client works out of the box.

Page 17: Why UI developers love GraphQL

Cache consistency

query Sidebar { postFeed { title }}

query Post { post(id: 1) { title content }}

How do we make the title field for the post refer to one location in the cache?

Page 18: Why UI developers love GraphQL

Cache consistency

postFeed[0] -> title -> 'Original title'post(id: 1) -> title -> 'New title'

How do we make the title field for the post refer to one location in the cache?

Page 19: Why UI developers love GraphQL

Defining cache IDs for the client

Could be server-side like Relay or client-side like Apollo

(obj) => obj.__typename + ':' + obj.id

Page 20: Why UI developers love GraphQL

Paths with IDs

Now we can easily keep our cache consistent!

postFeed[0] -> (id: 1)post(id: 1) -> (id: 1)

(id: 1) -> title -> 'New title'

Page 21: Why UI developers love GraphQL

Data flowNow that we have a cache, let’s use it!

Page 22: Why UI developers love GraphQL

Updating the store updates the UI

Smart clients execute the queries when the store changes, keeping your UI consistent

query Sidebar { postFeed { title }}

Page 23: Why UI developers love GraphQL

Full data loading path

Smart clients execute the queries when the store changes, keeping your UI consistent

query Sidebar { postFeed { title }}

Fetcher

query Sidebar { postFeed { title }}

Normalizer

Page 24: Why UI developers love GraphQL

Updates can come from anywhere

It’s like automated Flux, which takes advantage of the GraphQL query structure

query Sidebar { postFeed { title }}

Other queries

Mutation results

Subscription results

Reduxactions

Page 25: Why UI developers love GraphQL

More toolsToday:

- Static code generation and native clients: apollo-ios- Pub/sub GraphQL subscriptions: graphql-subscriptions

Tomorrow:

- Client-side data- Cache expiration- Building your own bespoke clients

Let’s build the future together!