mirror of
https://github.com/status-im/subspace-docs.git
synced 2025-02-08 02:43:25 +00:00
added apollo and graphql code
This commit is contained in:
parent
75d8f28a6a
commit
aa6522fdf6
@ -0,0 +1,74 @@
|
|||||||
|
```js
|
||||||
|
import gql from "graphql-tag";
|
||||||
|
import { ApolloClient } from "apollo-client";
|
||||||
|
import { ApolloProvider, Query } from "react-apollo";
|
||||||
|
import { InMemoryCache } from "apollo-cache-inmemory";
|
||||||
|
import Phoenix from "phoenix";
|
||||||
|
import Web3 from "web3";
|
||||||
|
import { makeExecutableSchema } from "graphql-tools";
|
||||||
|
import PhoenixRxLink from "./phoenix-rx-link";
|
||||||
|
|
||||||
|
const MY_QUERY = gql`
|
||||||
|
query {
|
||||||
|
escrows {
|
||||||
|
escrowId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const eventSyncer = new Phoenix(web3.currentProvider);
|
||||||
|
|
||||||
|
await eventSyncer.init();
|
||||||
|
|
||||||
|
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 client = new ApolloClient({
|
||||||
|
// If addTypename:true, the query will fail due to __typename
|
||||||
|
// being added to the schema. reactive-graphql does not
|
||||||
|
// support __typename at this moment.
|
||||||
|
cache: new InMemoryCache({ addTypename: false }),
|
||||||
|
link: PhoenixRxLink(schema)
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setState({ client });
|
||||||
|
|
||||||
|
<ApolloProvider client={this.state.client}>
|
||||||
|
<Query query={MY_QUERY}>
|
||||||
|
{({ loading, error, data }) => {
|
||||||
|
if (loading) return <div>Loading...</div>;
|
||||||
|
if (error) {
|
||||||
|
console.error(error);
|
||||||
|
return <div>Error :(</div>;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<p>The data returned by the query: {JSON.stringify(data)}</p>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
</Query>
|
||||||
|
</ApolloProvider>
|
||||||
|
```
|
46
graphql.md
46
graphql.md
@ -1 +1,47 @@
|
|||||||
# Reactive GraphQL
|
# 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
|
||||||
|
});
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user