Skip to content

Instantly share code, notes, and snippets.

View AlecAivazis's full-sized avatar
💭
I may be slow to respond.

Alec Aivazis AlecAivazis

💭
I may be slow to respond.
View GitHub Profile
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
// add the playground endpoint to the router
http.HandleFunc("/graphql", withUserInfo(gw.PlaygroundHandler))
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
// 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
import { makeExecutableSchema } from 'graphql-tools'
const schema = `
type Auction {
itemName: String!
}
type Query {
allAuctions: [Auction!]!
}
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 => {
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(),
},
{
allAuctions {
offers {
user {
favoritePhoto {
url
}
}
}
}
node(id: $id) {
... on Photo {
url
}
}
{
allAuctions {
photo {
id
}
user {
username
}
}
}