graphql indyjs april 2016

37
GRAPHQL INDYJS MEETUP APRIL 2016

Upload: brad-pillow

Post on 06-Apr-2017

254 views

Category:

Software


0 download

TRANSCRIPT

Page 1: GraphQL IndyJS April 2016

GRAPHQLINDYJS MEETUP APRIL 2016

Page 2: GraphQL IndyJS April 2016

MEMES…

Page 3: GraphQL IndyJS April 2016

REST

REST IS AN ACRONYM FOR ?

Page 4: GraphQL IndyJS April 2016

REST

REST HISTORY

▸ The term representational state transfer was introduced and defined in 2000 by Roy Fielding

▸ The defacto standard for communicating with a server

▸ Defines simple operations: GET, PUT, POST, DELETE

▸ A query operation (like GET) promises no side-effects (e.g. changes) in data being queried. Commands (like PUT/DELETE) answer no questions about the data, but compute changes applied to the data (e.g. UPDATE or INSERT to use database terms).

Page 5: GraphQL IndyJS April 2016

REST

REST ISSUES

▸ Many endpoints - proliferation of ad hoc endpoints

▸ Many round trips between the client and server

▸ First fetch pilots, then fetch homeworld info for each pilot, then…

▸ Very expensive for mobile apps

▸ Response structure may change over time

▸ Data overfetching

▸ We don’t always need all the data for a REST call

▸ Usually no metadata

▸ Big blob of data…we can’t run inquires on it’s types or structure

Page 6: GraphQL IndyJS April 2016

REST

FIXES FOR REST??

▸ Define your own ad-hoc query language

▸ Use REST PATCH to minimize data transfer ?

▸ REST API introspection via Swagger (http://swagger.io/)

Page 7: GraphQL IndyJS April 2016

GRAPHQL

JAVASCRIPT FATIGUE

BECAUSE A NEW JAVASCRIPT FRAMEWORK IS RELEASED

Page 8: GraphQL IndyJS April 2016

GRAPHQL

WHAT IS GRAPHQL?

▸ Queries describe the shape of data that the client needs. The response has the same structure as the request query.

▸ Server interprets GraphQL calls and queries the database (or any other source of data)

▸ GraphQL is language-, database- and protocol-agnostic

Page 9: GraphQL IndyJS April 2016

GRAPHQL WAS INVENTED AT FACEBOOK IN 2012, BUT IT WAS FIRST SHOWN TO PUBLIC IN 2015 DURING REACT.JS CONF AS A PART OF FACEBOOK OPEN-SOURCE ECOSYSTEM.

Page 10: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL MOTIVATION

▸ Single endpoint

▸ Hierarchical nature

▸ Strongly typed - enables validation, introspection and more

▸ Response mirrors the shape of the query

▸ Introspection - make inquiries from your client about the schema

▸ Application-Layer Protocol

▸ GraphQL is an application-layer protocol and does not require a particular transport. It is a string that is parsed and interpreted by a server.

Page 11: GraphQL IndyJS April 2016

GRAPHQL

GRAPHS - DAG/AST

*

-

397

+

6

DIRECTED ACYCLIC GRAPH ABSTRACT SYNTAX TREES

(6 + 7) * (9 - 3)

Page 12: GraphQL IndyJS April 2016

GRAPHQL

GRAPHS PERSON [NAME, ID, …]

STARSHIP-A [ID, MANUF,..]

STARSHIP-B [ID, MANUF,..]

STARSHIP-C [ID, MANUF,..]

PILOT-A [ID, NAME,..]

STARSHIP CONNECTION

STARSH

IP CONNECT

ION

STARSHIP CONNECTION

PILOT CONNECTION

HOMEWORLD-A [ID, NAME,..]

HOMEWORLD CONNECTION

HOMEWORLD CONNECTION

NODE

NODE

EDGE

Page 13: GraphQL IndyJS April 2016

GRAPHQL

FOLLOW ALONG!

▸ Go here and follow along with me

▸ http://graphql-swapi.parseapp.com

Page 14: GraphQL IndyJS April 2016

GRAPHQL

BASIC QUERY

Page 15: GraphQL IndyJS April 2016

GRAPHQL

NESTED FIELDS

Page 16: GraphQL IndyJS April 2016

GRAPHQL

CONNECTIONS

Page 17: GraphQL IndyJS April 2016

GRAPHQL

MANY CONNECTIONS

Page 18: GraphQL IndyJS April 2016

GRAPHQL

ARGUMENTS

Page 19: GraphQL IndyJS April 2016

GRAPHQL

FRAGMENTS

Page 20: GraphQL IndyJS April 2016

GRAPHQL

MORE FRAGMENTS

Page 21: GraphQL IndyJS April 2016

GRAPHQL

INTROSPECTION

Page 22: GraphQL IndyJS April 2016

GRAPHQL

SCARED YET?

▸ Syntax (covered)

▸ Type System

▸ Resolving

▸ Edges/Nodes & Pagination

Page 23: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL TYPE SYSTEM

▸ Scalars - basic types

▸ Schema - a representation of the capabilities of the GraphQL server

▸ Definitions - defining types

▸ Predicates - meta info on types

▸ Un-modifiers - type “un” modifiers

Page 24: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL SCALARS

▸ GraphQLInt - integer number

▸ GraphQLFloat - float number

▸ GraphQLString - string

▸ GraphQLBoolean - boolean

▸ GraphQLID - represents an ID (essentially a string)

Page 25: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL SCHEMAS

▸ It all starts with a “root” query

▸ Fields on type can be plain data or “resolved”

▸ You can define your own types

▸ You can define/override the serialization on a type

▸ You can add enhanced validation to types

▸ From a schema, you can generate a more human readable schema and JSON based schema for clients to use

Page 26: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL DEFINITIONS

▸ GraphQLScalarType - the class of scalars

▸ GraphQLObjectType - an object

▸ GraphQLInterfaceType - a interface

▸ GraphQLUnionType - a union

▸ GraphQLEnumType - enum

▸ GraphQLInputObjectType

▸ GraphQLList - what you use for lists arrays of objects

▸ GraphQLNonNull - an object for which null is an invalid value

Page 27: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL DATA RESOLUTION▸ Resolve to data - synchronous

result. Return the data as teh result of a resolve.

▸ Resolve to promise - asynchronous result. Return a promise that will return the data in the future. My preference is to use ES6 and async functions (which actually return promises).

Page 28: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL EDGES/NODES

▸ Not built into the language, but provided by types

▸ The type Facebook uses and I recommend you use too, are in : https://github.com/graphql/graphql-relay-js

▸ Using edges and nodes allow for pagination. If you don’t need pagination, then use a simple GraphQLList.

Page 29: GraphQL IndyJS April 2016

GRAPHQL

RUNNING ON EXPRESS https://github.com/graphql/graphql-js

Page 30: GraphQL IndyJS April 2016

GRAPHQL

EXAMPLE SCHEMA

Page 31: GraphQL IndyJS April 2016

GRAPHQL

CUSTOM SCALAR

Credits: Snippet from https://github.com/mattkrick/meatier

Page 32: GraphQL IndyJS April 2016

GRAPHQL

BUILD IT!

▸ Build your server…I use Babel and ES6

▸ You’ll also need to run (see sample code) a script to “update” your schema. This creates a client and a human readable version of your schema.

▸ If you are using Relay, you will also need to use the Babel-Relay plugin to consume, compile and validate your GraphQL queries in your components.

▸ You also need to tell the Babel-Relay plugin where to find the schema you built above (the JSON form).

Page 33: GraphQL IndyJS April 2016

GRAPHQL

MAKE SENSE?

▸ Hopefully more sense than Chewy!

Page 34: GraphQL IndyJS April 2016

GRAPHQL

GRAPHQL MISUNDERSTANDINGS

▸ All of the queries you see are defined by your schema. I.e. things like viewer, user, edge, node are defined by the schema and not an inherent part of the language.

▸ GraphQL is Javascript only. Nope!

▸ There are 3rd party ports for Python, Scala, Go, Ruby, etc.

▸ GraphQL requires special communications protocol. Nope!

▸ The default implementation uses a basic REST post call, but you can use web sockets, etc. and even mix and match.

Page 35: GraphQL IndyJS April 2016

GRAPHQL & RELAY

RESOURCES

▸ Cartoon Guides To GraphQL: https://code-cartoons.com

▸ Awesome GraphQL - curated list on Github: https://github.com/chentsulin/awesome-graphql

Page 36: GraphQL IndyJS April 2016

GRAPHQL & RELAY

MORE RESOURCES

▸ GraphQL Home: http://graphql.org/

▸ GraphQL Spec: https://facebook.github.io/graphql/

▸ Many REST APIs you can experiment with using GraphQL: https://www.graphqlhub.com/

Page 37: GraphQL IndyJS April 2016

QUESTIONS??? ▸ Twitter: @BradPillow, GitHub: pillowsoft

THANKS!