Igor Suvorov
Программист-предприниматель
Занятие 12
13 May 2017
Профессия
Node.js & React.js developer
продвинутый курс
и проблемы связанные с ней
для хорошей коммуникации
let app = express()
app.use(store.modelMiddleware())
app.use((req, res, next) => {
let model = req.getModel()
})
//////
let model = store.createModel()
let $user = model.doc('user', '1')
let $items = model.query('items', {})
await model.subscribe($user, 'users.2', ['users', '3'], $items, 'todos', ['products', {}])
let user1 = model.get('users.1')
let user2 = model.get('users.2')
let todos = model.query('todos', {}).get()
var sync = require('swarm-syncable');
var Model = sync.Model;
var Host = sync.Host;
// var replica = new Replica({upstream: 'ws://localhost:8000'});
// a replica manages logs and synchronizes to the upstream replica
// in this example, we'll create a replica implicitly through Host
// manages objects
new Host({
replica: true,
upstream: 'ws://localhost:8000'
});
var mouse = new swarm.Model({x:1, y:2});
mouse.on('change', function() {
console.log('mouse x:', mouse.x, 'y:', mouse.y);
});
mouse.set({x:3, y:4});
Replace AJAX-REST by Redux actions synchronization between client and server
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
Запросы
query {
stuff
}
query {
stuff {
eggs
shirt
pizza
}
}
query {
posts { # это массив
title
body
author { # мы может пойти глубже
name
avatarUrl
profileUrl
}
}
}
query {
post(id: "123foo"){
title
body
author{
name
avatarUrl
profileUrl
}
}
}
query getMyPost($id: String) {
post(id: $id){
title
body
author{
name
avatarUrl
profileUrl
}
}
}
query getMyPost($id: String) {
post(id: $id){
title
body
author{
name
avatarUrl
profileUrl
}
}
}
Запросы
Схемы
type Author {
id: ID! # the ! means that every author object _must_ have an id
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}
type Post {
id: ID!
title: String
author: Author
votes: Int
}
# the schema allows the following query:
type Query {
posts: [Post]
}
# we need to tell the server which types represent the root query
# and root mutation types. We call them RootQuery and RootMutation by convention.
schema {
query: Query
}
Схемы, arguments
type Starship {
id: ID!
name: String!
length(unit: LengthUnit = METER): Float
}
scalar types
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
#Lists and Non-Null
type Character {
name: String!
appearsIn: [Episode]!
myField: [String!]
}
interfaces
interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
starships: [Starship]
totalCredits: Int
}
type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}
union SearchResult = Human | Droid | Starship
review types
Resolvers (распознаватели)
Query: {
post(root, args) {
return Posts.find({ id: args.id });
}
},
Post: {
author(post) {
return Users.find({ id: post.authorId})
},
commentsCount(post) {
return Comments.find({ postId: post.id}).count()
}
}
variables
Mutations (изменения)
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
{
"ep": "JEDI",
"review": {
"stars": 5,
"commentary": "This is a great movie!"
}
}
fragments
any questions?
программист-предприниматель
By Igor Suvorov