fix: reactive-graphql docs

This commit is contained in:
Richard Ramos 2019-09-10 13:33:00 -04:00
parent 10087ce991
commit 1223db334d
4 changed files with 65 additions and 49 deletions

View File

@ -32,7 +32,7 @@ module.exports = {
'/redux-observable'
]
},
'/graphql',
'/reactive-graphql',
'/apollo-client'
]
},

2
api.md
View File

@ -2,7 +2,7 @@
## General
### `new Phoenix(web3Provider, [, options])`
### `new Phoenix(web3Provider [, options])`
Phoenix constructor.
**Parameters**

View File

@ -1,47 +0,0 @@
# Reactive GraphQL
```js
import { makeExecutableSchema } from "graphql-tools";
import gql from "graphql-tag";
import {graphql} from "reactive-graphql";
const typeDefs = `
type Escrow {
buyer: String!
seller: String!
escrowId: Int!
}
type Query {
escrows: Escrow!
}
`;
const resolvers = {
Query: {
escrows: () => {
return eventSyncer.trackEvent(this.EscrowContract, 'Created', { filter: { buyer: accounts[0] }, fromBlock: 1 })
}
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const query = gql`
query {
escrows {
buyer
seller
escrowId
}
}
`;
const stream = graphql(schema, query).pipe(pluck("data", "escrows"));
this.setState({
escrow: stream
});
```

63
reactive-graphql.md Normal file
View File

@ -0,0 +1,63 @@
# reactive-graphql
Using `reactive-graphql` you can execute GraphQL queries against Phoenix observables to it, and create your own type definitions and queries.
### Example
```js
const Phoenix = require('phoenix');
const MyContract = require('./MyContract');
const { pluck } = require('rxjs/operators');
const { makeExecutableSchema } = require("graphql-tools");
const gql = require("graphql-tag");
const { graphql } = require("reactive-graphql");
const run = async () => {
const eventSyncer = new Phoenix(web3.currentProvider); // Use a valid websocket provider (geth, parity, infura...)
await eventSyncer.init();
const MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance
const typeDefs = `
type MyEvent {
someValue: Int
anotherValue: String
}
type Query {
myEvents: MyEvent!
}
`;
const resolvers = {
Query: {
myEvents: () => {
return eventSyncer.trackEvent(MyContractInstance, 'MyEvent', { filter: {}, fromBlock: 1 })
}
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const query = gql`
query {
myEvents {
someValue
anotherValue
}
}
`;
const stream = graphql(schema, query).pipe(pluck('data', 'myEvents'));
stream.subscribe(data => {
console.log(data);
})
}
run();
```
::: tip
This example is available in [Github](https://github.com/status-im/phoenix/tree/master/examples/reactive-graphql)
:::