From aa6522fdf6e84be554c65f1bf976a6c1c84b4e77 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 4 Sep 2019 21:42:27 -0400 Subject: [PATCH] added apollo and graphql code --- apollo-client.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ graphql.md | 46 ++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/apollo-client.md b/apollo-client.md index e69de29..eeb9557 100644 --- a/apollo-client.md +++ b/apollo-client.md @@ -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 }); + + + + {({ loading, error, data }) => { + if (loading) return
Loading...
; + if (error) { + console.error(error); + return
Error :(
; + } + return ( +

The data returned by the query: {JSON.stringify(data)}

+ ); + }} +
+
+``` \ No newline at end of file diff --git a/graphql.md b/graphql.md index 58bf5cb..8a53ef1 100644 --- a/graphql.md +++ b/graphql.md @@ -1 +1,47 @@ # 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 +}); +``` \ No newline at end of file