From 99610be997bb8f906c2f27cfe010e69693ad2e9e Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Wed, 28 Nov 2018 17:20:27 +1100 Subject: [PATCH] Get chat example up to date --- Gopkg.lock | 10 ++++++++++ Gopkg.toml | 6 +++++- example/chat/package.json | 28 +++++++++++++++++++--------- example/chat/server/server.go | 10 ++++++++-- example/chat/src/index.js | 29 ++++++++++++++++++++++++----- 5 files changed, 66 insertions(+), 17 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 0eb9b6f393..ba7c4bd22b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -118,6 +118,14 @@ revision = "792786c7400a136282c1664665ae0a8db921c6c2" version = "v1.0.0" +[[projects]] + digest = "1:b0c25f00bad20d783d259af2af8666969e2fc343fa0dc9efe52936bbd67fb758" + name = "github.com/rs/cors" + packages = ["."] + pruneopts = "UT" + revision = "9a47f48565a795472d43519dd49aac781f3034fb" + version = "v1.6.0" + [[projects]] branch = "master" digest = "1:7ca2584fa7da0520cd2d1136a10194fe5a5b220bdb215074ab6f7b5ad91115f4" @@ -175,6 +183,7 @@ ] pruneopts = "UT" revision = "e805d08bb209b1accdea76bd2327811858d81985" + version = "v1.0.0" [[projects]] branch = "master" @@ -239,6 +248,7 @@ "github.com/mitchellh/mapstructure", "github.com/opentracing/opentracing-go", "github.com/pkg/errors", + "github.com/rs/cors", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", "github.com/urfave/cli", diff --git a/Gopkg.toml b/Gopkg.toml index 494e8df5e5..7876142a7e 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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 @@ -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" diff --git a/example/chat/package.json b/example/chat/package.json index 119c7ea2ed..ec70c1f671 100644 --- a/example/chat/package.json +++ b/example/chat/package.json @@ -3,14 +3,18 @@ "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": { @@ -18,5 +22,11 @@ "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" + ] } diff --git a/example/chat/server/server.go b/example/chat/server/server.go index b3f313acd3..0bcee17872 100644 --- a/example/chat/server/server.go +++ b/example/chat/server/server.go @@ -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" @@ -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)) } diff --git a/example/chat/src/index.js b/example/chat/src/index.js index 7f412654dc..c3a501c941 100644 --- a/example/chat/src/index.js +++ b/example/chat/src/index.js @@ -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(), });