Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React - Relay - GraphQL

igorapa
April 07, 2016

React - Relay - GraphQL

Overview how to work React, Relay and GraphQL together.

This talk was:
Organized by FEMUGSP (https://sp.femug.com/t/femug-sp-31-cubo/668)
At Cubo (https://cubo.network/)

igorapa

April 07, 2016
Tweet

More Decks by igorapa

Other Decks in Technology

Transcript

  1. What is React? A JavaScript library for building user interfaces

    Just the UI - Virtual DOM - Data Flow Components
  2. without JSX > https://jsfiddle.net/reactjs/5vjqabv3/ var Hello = React.createClass({ displayName: 'Hello',

    render: function() { return React.createElement("div", null, "Hello ", this.props.name); } }); ReactDOM.render( React.createElement(Hello, {name: "World"}), document.getElementById('container') );
  3. with JSX > https://jsfiddle.net/reactjs/69z2wepo/ var Hello = React.createClass({ render: function()

    { return <div>Hello {this.props.name}</div>; } }); ReactDOM.render( <Hello name="World" />, document.getElementById('container') );
  4. What is GraphQL? GraphQL is a data query language and

    runtime designed and used at Facebook to request and deliver data to mobile and web apps since 2012. Declarative - Compositional - Strong Typed
  5. { user(id: 30) { name, foo: friend(id: 2) { name

    } bar: friend(id: 13) { name, email } } } Aliases
  6. { "data": { "user": { "name": "Igor APA", "foo": {

    "name": "Foo" }, "bar": { "name": "Bar", "email": "[email protected]" } } } } Aliases
  7. Fragment > http://graphql.org/docs/queries/ { luke: human(id: "1000") { ...HumanFragment }

    leia: human(id: "1003") { ...HumanFragment } } fragment HumanFragment on Human { name homePlanet }
  8. Fragment > http://graphql.org/docs/queries/ { "data": { "luke": { "name": "Luke

    Skywalker", "homePlanet": "Tatooine" }, "leia": { "name": "Leia Organa", "homePlanet": "Alderaan" } } }
  9. let Schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query',

    fields: () => ({ store: { type: storeType, resolve: () => store } }) }), mutation: new GraphQLObjectType({ name: 'Mutation', fields: () => ({ createData: createDataMutation }) }) }); export default Schema; Server Schema.js
  10. Api.js import {post} from 'jquery'; import ServerActions from './actions/ServerActions'; let

    API = { fetchDatas() { post('/graphql', { query: `{ value { _id, title, url } }` }).done(resp => { ServerActions.receiveDatas(resp.data.value); }); } }; export default API;
  11. What is Relay? A JavaScript framework for building data-driven React

    Applications Declarative - Colocation - Mutations
  12. Main.js class HomeRoute extends Relay.Route { static routeName = 'Home';

    static queries = { store: (Component) => Relay.QL` query AppQuery { store { ${Component.getFragment('store')} } } ` } } ReactDOM.render( <Relay.RootContainer Component={App} route={new HomeRoute()} />, document.getElementById('foo') );
  13. App.js class App extends React.Component { render() {…} } App

    = Relay.createContainer(App, { initialVariables: { limit: 30 }, fragments: { store: () => Relay.QL` fragment on Store { dataConnection(first: $limit) { edges { node { id, ${TableItem.getFragment('data')} } } } } ` } }); export default App;
  14. TableItem.js class TableItem extends React.Component { render() {…} } TableItem

    = Relay.createContainer(TableItem, { fragments: { data: () => Relay.QL` fragment on Data { url, title } ` } }); export default TableItem;
  15. How does it relate to Flux? At Facebook, they have

    apps built entirely using Flux, entirely using Relay, or with both.