This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var forwardUserID = gateway.RequestMiddleware(func(r *http.Request) error { | |
// the initial context of the request is set to match the one we modified earlier | |
// we are safe to extract the value we saved in context | |
if userID := r.Context().Value("user-id"); userID != nil { | |
// set the header with the value we pulled out of context | |
r.Header.Set("USER_ID", userID.(string)) | |
} | |
// there was no error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add the playground endpoint to the router | |
http.HandleFunc("/graphql", withUserInfo(gw.PlaygroundHandler)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func withUserInfo(handler http.HandlerFunc) http.HandlerFunc { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// look up the value of the Authorization header | |
tokenValue := r.Header.Get("Authorization") | |
// here is where you would perform some kind of validation on the token | |
// but we're going to skip that for this example and save it as the | |
// id directly. PLEASE, DO NOT DO THIS IN PRODUCTION. | |
// invoke the handler with the new context |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a reference to the hub contract we created earlier | |
const hub = new web3.eth.Contract(HubABI, 'hub address from earlier') | |
// build the list of auctions | |
const auctions = [] | |
for (let i = 0; i < (await hub.methods.auctionCount().call()); i++) { | |
auctions.push(new web3.eth.Contract(AuctionABI, await hub.methods.auctions(i).call())) | |
} | |
return auctions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeExecutableSchema } from 'graphql-tools' | |
const schema = ` | |
type Auction { | |
itemName: String! | |
} | |
type Query { | |
allAuctions: [Auction!]! | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloLink } from 'apollo-link' | |
import { Observable } from 'apollo-link-core' | |
import { graphql } from 'graphql' | |
import { print } from 'graphql/language/printer' | |
import schema from './schema' | |
const blockChainLink = new ApolloLink( | |
(operation) => new Observable(observer => { | |
graphql(schema, print(operation.query), null, null, operation.variables) | |
.then(result => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeExecutableSchema } from 'graphql-tools' | |
import { HubABI, AuctionABI, web3 } from '../contracts' | |
const schema = ... | |
const resolvers = { | |
Auction: { | |
// auction is an instance of the Auction contract wrapper | |
itemName: auction => auction.methods.itemName().call(), | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
allAuctions { | |
offers { | |
user { | |
favoritePhoto { | |
url | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node(id: $id) { | |
... on Photo { | |
url | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
allAuctions { | |
photo { | |
id | |
} | |
user { | |
username | |
} | |
} | |
} |