Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增 relay初体验的demo #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
新增 Relay项目Demo,Connection的使用
  • Loading branch information
HyperSimon committed Mar 10, 2017
commit 59de4a314813a6424eb12ac9897825ca2220243b
12 changes: 12 additions & 0 deletions Appendix04/Relay-Example/demo1-基本/data/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Query {
store: Store
}

type Store {
teas: [Tea]
}

type Tea {
name: String
steepingTime: Int
}
31 changes: 31 additions & 0 deletions Appendix04/Relay-Example/demo2-使用Connection/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BSD License

For Relay Starter Kit software

Copyright (c) 2013-2015, Facebook, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name Facebook nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 changes: 33 additions & 0 deletions Appendix04/Relay-Example/demo2-使用Connection/PATENTS
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Additional Grant of Patent Rights Version 2

"Software" means the Relay Starter Kit software distributed by Facebook, Inc.

Facebook, Inc. ("Facebook") hereby grants to each recipient of the Software
("you") a perpetual, worldwide, royalty-free, non-exclusive, irrevocable
(subject to the termination provision below) license under any Necessary
Claims, to make, have made, use, sell, offer to sell, import, and otherwise
transfer the Software. For avoidance of doubt, no license is granted under
Facebook's rights in any patent claims that are infringed by (i) modifications
to the Software made by you or any third party or (ii) the Software in
combination with any software or other technology.

The license granted hereunder will terminate, automatically and without notice,
if you (or any of your subsidiaries, corporate affiliates or agents) initiate
directly or indirectly, or take a direct financial interest in, any Patent
Assertion: (i) against Facebook or any of its subsidiaries or corporate
affiliates, (ii) against any party if such Patent Assertion arises in whole or
in part from any software, technology, product or service of Facebook or any of
its subsidiaries or corporate affiliates, or (iii) against any party relating
to the Software. Notwithstanding the foregoing, if Facebook or any of its
subsidiaries or corporate affiliates files a lawsuit alleging patent
infringement against you in the first instance, and you respond by filing a
patent infringement counterclaim in that lawsuit against that party that is
unrelated to the Software, the license granted hereunder will not terminate
under section (i) of this paragraph due to such counterclaim.

A "Necessary Claim" is a claim of a patent owned by Facebook that is
necessarily infringed by the Software standing alone.

A "Patent Assertion" is any lawsuit or other action alleging direct, indirect,
or contributory infringement or inducement to infringe any patent, including a
cross-claim or counterclaim.
34 changes: 34 additions & 0 deletions Appendix04/Relay-Example/demo2-使用Connection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Relay Starter Kit

This kit includes an app server, a GraphQL server, and a transpiler that you can use to get started building an app with Relay. For a walkthrough, see the [Relay tutorial](https://facebook.github.io/relay/docs/tutorial.html).

## Installation

```
npm install
```

## Running

Start a local server:

```
npm start
```

## Developing

Any changes you make to files in the `js/` directory will cause the server to
automatically rebuild the app and refresh your browser.

If at any time you make changes to `data/schema.js`, stop the server,
regenerate `data/schema.json`, and restart the server:

```
npm run update-schema
npm start
```

## License

Relay Starter Kit is [BSD licensed](./LICENSE). We also provide an additional [patent grant](./PATENTS).
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var getbabelRelayPlugin = require('babel-relay-plugin');
var schema = require('../data/schema.json');

module.exports = getbabelRelayPlugin(schema.data);
141 changes: 141 additions & 0 deletions Appendix04/Relay-Example/demo2-使用Connection/data/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* Created by simon on 2017/3/10.
*/

// client side 暂存 store,GraphQL Server reponse 会更新 store,再透过 props 传递给 Component
const STORE = {};

// 中国十大名茶
// 碧螺春、
// 西湖龙井、
// 安徽毛峰、
// 安徽瓜片、
// 恩施玉露、
// 福建铁观音、
// 福建银针、
// 云南普洱茶、
// 福建云茶、
// 江西云雾茶

// 日本三大名茶 [PS: 光听名字就不好喝]
// 宇治茶(京都府)、
// 狭山茶(崎玉县)、
// 静冈茶(静冈县)
const teas = [
{ name: '碧螺春', id: 1 },
{ name: '西湖龙井', id: 2 },
{ name: '安徽毛峰', id: 3 },
{ name: '安徽瓜片', id: 4 },
{ name: '恩施玉露', id: 5 },
{ name: '福建铁观音', id: 6 },
{ name: '福建银针', id: 7 },
{ name: '云南普洱茶', id: 8 },
{ name: '福建云茶', id: 9 },
{ name: '江西云雾茶', id: 10 },
{ name: '宇治茶', id: 11 },
{ name: '狭山茶', id: 12 },
{ name: '静冈茶', id: 13 },
]

const chineseTea = {
id: '1',
name: 'China',
teaIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

const japaneseTea = {
id: '10086',
name: 'japan',
teaIds: [11, 12, 13]
}

const data = {
contries: [
chineseTea,
japaneseTea
],

teas: teas
}

/**
* 获取所有国家
* @returns {Array}
*/
export function getContries(names) {
return names.map(name => {
if (name === 'China') {
return data.contries[0];
}
if (name === 'japan') {
return data.contries[1];
}
return null;
});
}

export function getContry(id) {
return data.contries.filter(contry => contry.id === id)[0]
}

export function getContryByName(name) {
console.info(name)
return data.contries.filter(contry => contry.name === name)[0]
}

/**
* 获取某一个国家的所有茶叶品种
* @param contryName
* @returns {Array}
*/
export function getTeas(contryName) {
const index = data.contries.find(c => c.name === contryName)
return data.contries[index].teaIds.map(id => teas[id])
}

export function getTea(teaId) {
return data.teas.filter(tea => tea.id === teaId)[0]
}

export function createTea(name, contryName) {
const newTea = { name: name, id: teas.length }
data.teaIds.push(newTea)

const index = data.contries.find(c => c.name === contryName)
data.contries[index].teaIds.push(newTea.id)
}



































Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 国家
type Contry implements Node {
# The ID of an object
id: ID!

# 国家名称
name: String

# 属于这个国家的著名茶叶
teas(after: String, first: Int, before: String, last: Int): TeaConnection
}

# An object with an ID
interface Node {
# The id of the object.
id: ID!
}

# Information about pagination in a connection.
type PageInfo {
# When paginating forwards, are there more items?
hasNextPage: Boolean!

# When paginating backwards, are there more items?
hasPreviousPage: Boolean!

# When paginating backwards, the cursor to continue.
startCursor: String

# When paginating forwards, the cursor to continue.
endCursor: String
}

type Query {
# Fetches an object given its ID
node(
# The ID of an object
id: ID!
): Node
contry(name: String): Contry
}

type Tea implements Node {
# The ID of an object
id: ID!
name: String
}

# A connection to a list of items.
type TeaConnection {
# Information to aid in pagination.
pageInfo: PageInfo!

# A list of edges.
edges: [TeaEdge]
}

# An edge in a connection.
type TeaEdge {
# The item at the end of the edge
node: Tea

# A cursor for use in pagination
cursor: String!
}
Loading