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

Angular 2 and Relay

Angular 2 and Relay

Relay and GraphQL are new technologies for client-server communication. Originally from the React universe, Relay is a declarative way of defining all the data a component needs. Relay retrieves, caches and keeps your data consistent. Based on real life experiences, we will give an introduction into Relay and show how to use it with Angular 2.

Sebastian Fröstl

February 18, 2016
Tweet

More Decks by Sebastian Fröstl

Other Decks in Technology

Transcript

  1. Let’s build an app! • We want to create a

    conference planner • A user can see a list of conferences • A user can attend and leave conferences
  2. • Component needs to rely on that data needed is

    provided • No clear way to see what data a component needs • Reduced reusability of components • Over- or under-fetching of data Challenge #1
  3. • Actions cause side effects • Data has to be

    consistent within the app • Ensuring consistent data requires complex updating or re-fetching Challenge #2
  4. • Server API is constantly evolving • Client requests data

    the server can not provide • Breaking changes: No clear contract between client and server • Consequences are runtime exceptions and hard to trace user facing bugs Challenge #3
  5. GraphQL Example { fragment on User { id firstName, lastName

    } } Request Response { "user": { "id": 1, "firstName": "Lisa", "lastName": "Simpson" } }
  6. const UserAccountContainer = Relay.createGenericContainer('UserAccount', { fragments: { user: () =>

    Relay.QL` fragment on User { firstName, } `, }, }); @Component({ selector: 'user-account' }) @View({ template: `<div> {{ user.firstName }} </div>`, }) class UserAccount { constructor() { } }
  7. const UserAccountContainer = Relay.createGenericContainer('UserAccount', { fragments: { user: () =>

    Relay.QL` fragment on User { firstName, } `, }, }); @connectRelay({ container: UserAccountContainer }) @Component({ selector: 'user-account' }) @View({ template: `<div> {{ relayData.user.firstName }} </div>`, }) class UserAccount { constructor() { this.initWithRelay(); } }
  8. Challenge #3 How to ensure that we don’t break the

    contract between client and server?
  9. Summary • Relay solves common problems of client-server communication •

    Declarative way to specify what data a component needs • Central store managed by Relay • Strongly typed query language (GraphQL) • It works with Angular 2!
  10. Thank you! Ask Me Anything Microsoft warm bites area Code

    https://github.com/angular2-relay/angular2-relay Slides https://speakerdeck.com/sfroestl/angular2-relay @AndiMarek @SFroestl