Real World React November 17th, 2017
An IntroductionTo GraphQLReal World React, November 16, 2017
View Slide
Jon WongFrontend Infrastructure,Coursera@jnwng2
Runtime▪ Clients▪ Servers▪ ToolsWhat We’re Covering in GraphQLLanguage▪ Schemas▪ Queries▪ Mutations3
What is GraphQL?Taken straight from GraphQL.org
Describe your Datatype Project {name: Stringtagline: Stringcontributors: [User]}5
Ask for What You Want{project(name: "GraphQL") {tagline}}6
Get Predictable Results{project(name: "GraphQL") {tagline}}7{"project": {"tagline": "A query languagefor APIs"}}
A single endpoint for data from anywhere{book {author {tweetsamazonReviews}}}8
SchemasDescribing possible data
Describe everything that is possible▪ All possible data results aredescribed by the server.10
Validation▪ Every query is validated againstthe GraphQL schema▪ No runtime errors for querydocuments11
Execution▪ Query execution utilizes theschema to figure out what torun, mapping them to“resolvers”12
The GraphQL Schemagives us the basis ofeverything that ispossible whencommunicating to theserver from the client.13
QueriesReading your data
query {someField}{ “data”: { “someField”: “someValue” }}15A simple query
query MyQueryName {fieldName}16A simple query with a name
query ($someArg: String) {fieldWithArg(name: $someArg)}17A simple query with arguments
query ($someArg: String) {fieldWithArg(name: $someArg)}+{ “someArg”: “my-field-name” }18… which requires variables
query {smallPicture: picture(size: 100)mediumPicture: picture(size: 500)largePicture: picture(size: 2000)}19A simple query with aliases
Fragmentsfragment MyFancyFragment on MyResource {someField}20These are required!
Spreading a fragment in a queryquery {someResource {...MyFancyFragment}}21These two typesshould match
22A typical React application
23… broken down into components ...
24A complex query
25A complex query
26A complex queryquery CoursePage {course(slug: “Machine Learning”) {titledescription...University...Instructor}}
27{course.title}{course.description}Breaking queries down with fragmentsquery CoursePage {course(slug: “Machine Learning”) {titledescription...University...Instructor}}
28▪ Components can be more portable▪ Components are moreself-sufficient.Every component can declare its owndata requirements
GraphQL queries allowthe client to declareexactly what it needs, inthe form that it needs itin.29
MutationsWriting your data
mutation {addToCounter {count}}31A simple mutation{“data”: {“addToCounter”: {“count”: 1}}}
mutation {addToCounter {count}}32A simple mutationThis is a query
mutation {addToCounter { count }second: addToCounter {count}}33Mutations execute serially{“data”: {“addToCounter”: { “count”: 1 },“second”: { “count”: 2 }}}
ClientsManaging your data
Clients make it easier to manage dataRelay Apollo35
Caching▪ Clients provide advancedcaching for GraphQL queries.36
Cachingquery ListView {allBooks {idname}}37query DetailView ($id: ID) {bookById(id: $id) {name}}
The GraphQL HoCconst Component = ({ data: { someField }}) =>{someField}export default graphql(gql`query { someField }`)(Component)38
39
40const Instructor = ({ data: { instructor }}) => ({instructor.name});export default graphql(gql`query {instructor(id: 1) {nameprofile {...ProfilePhoto}}}${ProfilePhoto.fragment}`)(Instructor)
Clients make adoptingGraphQL in yourapplication a breeze.41
ServersServing your data
Mapping types to resolvers43const schemaDefinition = `type Query {books: [Book]}type Book {title: Stringauthor: String}`;const resolvers = {Query: {books: () =>fetch('https://api.com/books')},};
Every field can be resolved separately.44const resolvers = {Query: {books: () => fetch('https://api.com/books')},Book: {reviews: () => return [],title: () => fetch(‘https://api.com/titles’)}};
A single endpoint for data from anywhere{book {author {tweetsamazonReviews}}}45
GraphQL servers are flexible46▪ They can act as proxies toexisting data▪ They can also become thebusiness logic layer itself
ToolsSupercharging your data
GraphiQL48
`eslint-plugin-graphql`49Catch invalid API calls at compile-time
`apollo-codegen`50Catch runtime errors at compile-time
GraphQL enables anentire ecosystem of toolsto make developers moreeffective.51
▪ GraphQL makes it really simpleto query lots of data, no matterwhere it is.52In Summary
▪ Just like React, GraphQL canbreak down complexity intocomposable, reusable pieces53In Summary
▪ GraphQL.org▪ GraphQL.com▪ https://launchpad.graphql.com/new54How to Get Started
THANKS!55@jnwngPresentation template by SlidesCarnival