6. GraphQL: What is it?
• a schema for graph-oriented data (Facebook/2012)
• well-suited for micro-service style backends
• created for specifying finer-grained queries
• provides interface between client and server
• client requests data from GraphQL server
• data requests are based on GraphQL queries
• => write queries against the schema
7. GraphQL: What it isn’t
• GQL does not dictate a server language
• GQL does not dictate a storage/back-end
• GQL is a query language without a database
8. GraphQL: What Does it Do?
• It exposes a single endpoint
• the endpoint parses and executes a query
• The query executes over a type system
• the type system is defined in the application server
• the type system is available via introspection (a GQL API)
9. GraphQL Server Structure
GraphQL servers have three components:
• 1) GraphQL “core” (JavaScript and other languages)
• 2) Type Definitions (maps app code to internal system)
• 3) Application code (business logic)
10. GraphQL Core: Five Components
• 1) Frontend lexer/parser: an AST [Relay uses parser]
• 2) Type System: GraphQLObjectType (a class)
• 3) Introspection: for querying types
• 4) Validation: is a query valid in the app’s schema?
• 5) Execution: manage query execution (via the AST)
11. The GraphiQL IDE
• https://github.com/skevy/graphiql-
app/blob/master/README.md
• https://github.com/skevy/graphiql-app/releases
• OSX: brew cask install graphiql
14. GraphQL Queries
• query EmpNameQuery {
• emp {
• fname
• lname
• }
• }
• The result of the preceding query is here:
• {
• "data": [{
• "emp": {
• "fname": "John",
• "lname": "Smith"
• }
• }]
• }
15. From REST to GraphQL (1)
• First REST query:
• GET /item/10000
• Result of first REST query:
• {
• item: {
• message: “Learning about graphql”,
• timestamp: 1456781234,
• ownerid: 23000
• }
16. From REST to GraphQL (2)
• Second REST query:
• GET /owner/23000
• Result of second REST query:
• {
• owner: {
• id: 8000,
• name: “John Smith”,
• homepage: {
• url: http://acme.com/jsmith
• }
• }
17. From REST to GraphQL (3)
• Execute ONE GraphQL query:
• {
• item (id: 10000) {
• message, timestamp,
• owner { // NB: owner is an object
• id, name,
• homepage {
• url
• }
• }
• }
25. ReactJS Client Code (2)
• render() { // this method is part of the UserInfo class
• if (this.state.loading) {
• return <span>Loading...</span>;
• }
• else if (this.state.error !== null) {
• return <span>Error: {this.state.error.message}</span>;
• }
• else {
• let queryArray = [this.state.data];
• let queryInfo = queryArray.map( (row,index) => {
• return <li key={index}>ID: {row.data.user.id} First Name: {row.data.user.fname} Last
Name: {row.data.user.lname}</li>
• })
• return (
• <div> <ul>{queryInfo}</ul> </div>
• )
• }
• }
}
26. ReactJS Client Code (3)
• let userDetails = "{user(id:%22200%22){fname,lname,id}}";
• let userQuery =
"http://localhost:3000/graphql?query="+userDetails;
• ReactDOM.render( // pass user data to UserInfo
• <UserInfo
• promise={$.getJSON(userQuery)} />,
• document.getElementById("userinfo")
• );
• </script>
27. Starting the Server and Client
• Step #1 (install the dependencies):
• npm install
• Step #2 (start the GraphQL server) in 1st command shell:
node index.js
• Step #3 (launch the ReactJS client) in 2nd command shell:
open ReactClientGraphQL.html
28. Second GraphQL Example
• combines Express, MongoDB and Mongoose
• uses express-graphql
• cd graphql-nodejs-master
• npm install
• npm start
• navigate to localhost:8000
29. Third Example: GraphQL and SQLite
• cd graphql-nodejs-newsfeed-master/
• npm install
• npm start
• sample query:
curl --data "query={ __schema { types { name } } }" localhost:8000
30. “Instant” GraphQL Server for ReactJS
• https://www.reindex.io
• Create your schema with your types
• Deploy GraphQL API with this command:
reindex schema-push
• Documentation/examples:
https://www.reindex.io/docs/
31. Falcor: An Alternative to GraphQL?
• Falcor Home Page: http://netflix.github.io/falcor/
• Falcor does not involve a schema
• Single data model (virtual JSON graph)
• JavaScript-like path syntax
• https://github.com/Netflix/falcor
32. The Flux Pattern
• developed by Facebook (2012?)
• a unidirectional data flow (not MVC)
• complements React's view components
• technology agnostic
• Flux implementations work with Angular 2
• implementations: Redux, Fluxxor, et al
33. Flux-based Applications
• apps comprise three major parts:
• the dispatcher
• the stores
• the views (React components)
• Components and their flow:
• Action->Dispatcher->Store(s)->View->Action
• Action = Event Type (=verb) + Payload (=data)
34. A Flux Implementation
• cd flux-master
• npm install
• npm start
• In a second command shell:
• cd flux-master/examples/flux-chat
• npm install
• open index.html
35. What is Redux?
• One implementation of Flux
• Created by Dan Abramov
• Most popular implementation of Flux
• https://github.com/reactjs/redux
• egghead.io tutorials
45. ReactFire versus Redux+Firebase
• ReactFire: a mechanism for communicating
between a React application and Firebase
• ReactFire “bypasses” Redux
• Combine React with Redux and Firebase:
• + insert a Firebase Ref into an action creator
• + when Firebase emits data, create an action
• + the action is sent to the dispatcher (reducer)
46. Other Flux Implementations
• Alt, flummox, fluxury, mcfly, reflux, etc
• At least 16 implementations
• Note: Relay is also a Flux implementation
• Comparison of Flux implementations:
https://github.com/voronianski/flux-comparison
=> Github links are available as well
47. What is Relay?
• a JavaScript Framework (Facebook)
• for creating data-driven React apps
• declarative syntax
• works with GraphQL
• creates efficient network requests
• fetches only what is requested
• aggregates queries
• caches data on client
52. Miscellaneous Topics (1)
• Combining TypeScript with ReactJS:
https://github.com/ServiceStackApps/typescript-redux
• Very good article regarding webpack:
https://medium.com/@rajaraodv/webpack-the-confusing-
parts-58712f8fcad9
React Hot Module / HMR (Hot Module Reloading):
https://github.com/gaearon/react-hot-loader/pull/240
53. Miscellaneous Topics (2)
• Time travel debugging:
https://code-cartoons.com/hot-reloading-and-time-travel-
debugging-what-are-they-3c8ed2812f35#.fqf5b9uwd
GraphQL and PostgreSQL:
https://github.com/calebmer/postgraphql
#1: sudo npm install -g postgraphql
#2: postgraphql postgres://localhost:5432/mydb --schema
forum --development
54. Recent/Upcoming Books and Training
1) HTML5 Canvas and CSS3 Graphics (2013)
2) jQuery, CSS3, and HTML5 for Mobile (2013)
3) HTML5 Pocket Primer (2013)
4) jQuery Pocket Primer (2013)
5) HTML5 Mobile Pocket Primer (2014)
6) D3 Pocket Primer (2015)
7) Python Pocket Primer (2015)
8) SVG Pocket Primer (2016)
9) CSS3 Pocket Primer (2016)
10) Angular 2 Pocket Primer (2016)