Skip to content

Commit

Permalink
Get chat example up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Nov 28, 2018
1 parent 4bfc82d commit 99610be
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
10 changes: 10 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ required = ["github.com/vektah/dataloaden"]

[[constraint]]
name = "github.com/vektah/gqlparser"
revision = "^1.0.0"
version = "^1.0.0"

[prune]
go-tests = true
Expand All @@ -23,3 +23,7 @@ required = ["github.com/vektah/dataloaden"]
[[constraint]]
name = "gopkg.in/yaml.v2"
version = "2.2.1"

[[constraint]]
name = "github.com/rs/cors"
version = "1.6.0"
28 changes: 19 additions & 9 deletions example/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"apollo-cache-inmemory": "^1.1.9",
"apollo-client": "^2.2.5",
"graphql-tag": "^2.9.1",
"graphql": "^0.13.2",
"react": "^16.2.0",
"react-apollo": "^2.1.0-beta.2",
"react-dom": "^16.2.0",
"react-scripts": "1.1.1",
"apollo-cache-inmemory": "^1.3.11",
"apollo-client": "^2.4.7",
"apollo-link": "^1.2.4",
"apollo-link-http": "^1.5.7",
"apollo-link-ws": "^1.0.10",
"apollo-utilities": "^1.0.26",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.0",
"react": "^16.6.3",
"react-apollo": "^2.3.1",
"react-dom": "^16.6.3",
"react-scripts": "^2.1.1",
"subscriptions-transport-ws": "^0.9.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
10 changes: 8 additions & 2 deletions example/chat/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/99designs/gqlgen/handler"
"github.com/gorilla/websocket"
"github.com/opentracing/opentracing-go"
"github.com/rs/cors"
"sourcegraph.com/sourcegraph/appdash"
appdashtracer "sourcegraph.com/sourcegraph/appdash/opentracing"
"sourcegraph.com/sourcegraph/appdash/traceapp"
Expand All @@ -18,13 +19,18 @@ import (
func main() {
startAppdashServer()

c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"},
AllowCredentials: true,
})

http.Handle("/", handler.Playground("Todo", "/query"))
http.Handle("/query", handler.GraphQL(chat.NewExecutableSchema(chat.New()),
http.Handle("/query", c.Handler(handler.GraphQL(chat.NewExecutableSchema(chat.New()),
handler.WebsocketUpgrader(websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
})),
}))),
)
log.Fatal(http.ListenAndServe(":8085", nil))
}
Expand Down
29 changes: 24 additions & 5 deletions example/chat/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import ApolloClient from 'apollo-client';
import App from './App';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { split } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';


const client = new SubscriptionClient('ws://localhost:8085/query', {
reconnect: true,
const wsLink = new WebSocketLink({
uri: `ws://localhost:8085/query`,
options: {
reconnect: true
}
});

const httpLink = new HttpLink({ uri: 'http://localhost:8085/query' });


// depending on what kind of operation is being sent
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);

const apolloClient = new ApolloClient({
link: client,
link: link,
cache: new InMemoryCache(),
});

Expand Down

0 comments on commit 99610be

Please sign in to comment.