diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1c8aa37..ea259ca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,13 +16,17 @@ jobs: runs-on: ubuntu-20.04 permissions: pull-requests: write + # Required: allow read access to the content for analysis. + contents: read + # Optional: Allow write access to checks to allow the action to annotate code in the PR. + checks: write steps: - name: Checkout - uses: actions/checkout@v3 - - uses: actions/setup-go@v4 + uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: "1.20" - - uses: actions/cache@v3 + - uses: actions/cache@v4 with: path: | ~/go/pkg/mod @@ -42,14 +46,27 @@ jobs: run: | cd ./example/hasura docker-compose up -d + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + only-new-issues: true + skip-cache: false + - name: Run Go unit tests for example/subscription + run: | + cd example/subscription + go get -t -v ./... + go test -v -race -timeout 3m ./... - name: Run Go unit tests run: go test -v -race -timeout 3m -coverprofile=coverage.out ./... - name: Go coverage format + if: ${{ github.event_name == 'pull_request' }} run: | go get github.com/boumenot/gocover-cobertura go install github.com/boumenot/gocover-cobertura gocover-cobertura < coverage.out > coverage.xml - name: Code Coverage Summary Report + if: ${{ github.event_name == 'pull_request' }} uses: irongut/CodeCoverageSummary@v1.3.0 with: filename: coverage.xml @@ -63,7 +80,7 @@ jobs: thresholds: "60 80" - name: Add Coverage PR Comment uses: marocchino/sticky-pull-request-comment@v2 - if: ${{ github.event_name == 'pull_request_target' }} + if: ${{ github.event_name == 'pull_request' }} with: path: code-coverage-results.md - name: Dump docker logs on failure diff --git a/.gitignore b/.gitignore index 137e4f2..f3de1db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ -coverage.out \ No newline at end of file +coverage.out +go.work +go.work.sum \ No newline at end of file diff --git a/README.md b/README.md index fca8dea..b75540e 100644 --- a/README.md +++ b/README.md @@ -571,6 +571,16 @@ client := graphql.NewSubscriptionClient("wss://example.com/graphql"). }) ``` +Some servers validate custom auth tokens on the header instead. To authenticate with headers, use `WebsocketOptions`: + +```go +client := graphql.NewSubscriptionClient(serverEndpoint). + WithWebSocketOptions(graphql.WebsocketOptions{ + HTTPHeader: http.Header{ + "Authorization": []string{"Bearer random-secret"}, + }, + }) +``` #### Options diff --git a/example/graphql-ws-bc/README.md b/example/graphql-ws-bc/README.md new file mode 100644 index 0000000..0d8a762 --- /dev/null +++ b/example/graphql-ws-bc/README.md @@ -0,0 +1,33 @@ +# Subscription example with graphql-ws backwards compatibility + +The example demonstrates the subscription client with the native graphql-ws Node.js server, using [ws server usage with subscriptions-transport-ws backwards compatibility](https://the-guild.dev/graphql/ws/recipes#ws-server-usage-with-subscriptions-transport-ws-backwards-compatibility) and [custom auth handling](https://the-guild.dev/graphql/ws/recipes#server-usage-with-ws-and-custom-auth-handling) recipes. The client authenticates with the server via HTTP header. + +```go +client := graphql.NewSubscriptionClient(serverEndpoint). + WithWebSocketOptions(graphql.WebsocketOptions{ + HTTPHeader: http.Header{ + "Authorization": []string{"Bearer random-secret"}, + }, + }) +``` + +## Get started + +### Server + +Requires Node.js and npm + +```bash +cd server +npm install +npm start +``` + +The server will be hosted on `localhost:4000`. + +### Client + +```bash +go run ./client +``` + diff --git a/example/graphql-ws-bc/client/main.go b/example/graphql-ws-bc/client/main.go new file mode 100644 index 0000000..f8aaee1 --- /dev/null +++ b/example/graphql-ws-bc/client/main.go @@ -0,0 +1,85 @@ +// subscription is a test program currently being used for developing graphql package. +// It performs queries against a local test GraphQL server instance. +// +// It's not meant to be a clean or readable example. But it's functional. +// Better, actual examples will be created in the future. +package main + +import ( + "flag" + "log" + "net/http" + + graphql "github.com/hasura/go-graphql-client" +) + +func main() { + protocol := graphql.GraphQLWS + protocolArg := flag.String("protocol", "graphql-ws", "The protocol is used for the subscription") + flag.Parse() + + if protocolArg != nil { + switch *protocolArg { + case "graphql-ws": + case "": + case "ws": + protocol = graphql.SubscriptionsTransportWS + default: + panic("invalid protocol. Accept [ws, graphql-ws]") + } + } + + if err := startSubscription(protocol); err != nil { + panic(err) + } +} + +const serverEndpoint = "http://localhost:4000" + +func startSubscription(protocol graphql.SubscriptionProtocolType) error { + log.Printf("start subscription with protocol: %s", protocol) + client := graphql.NewSubscriptionClient(serverEndpoint). + WithWebSocketOptions(graphql.WebsocketOptions{ + HTTPHeader: http.Header{ + "Authorization": []string{"Bearer random-secret"}, + }, + }). + WithLog(log.Println). + WithProtocol(protocol). + WithoutLogTypes(graphql.GQLData, graphql.GQLConnectionKeepAlive). + OnError(func(sc *graphql.SubscriptionClient, err error) error { + log.Print("err", err) + return err + }) + + defer client.Close() + + /* + subscription { + greetings + } + */ + var sub struct { + Greetings string `graphql:"greetings"` + } + + _, err := client.Subscribe(sub, nil, func(data []byte, err error) error { + + if err != nil { + log.Println(err) + return nil + } + + if data == nil { + return nil + } + log.Printf("hello: %+v", string(data)) + return nil + }) + + if err != nil { + panic(err) + } + + return client.Run() +} diff --git a/example/graphql-ws-bc/server/.gitignore b/example/graphql-ws-bc/server/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/example/graphql-ws-bc/server/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/example/graphql-ws-bc/server/index.ts b/example/graphql-ws-bc/server/index.ts new file mode 100644 index 0000000..3194644 --- /dev/null +++ b/example/graphql-ws-bc/server/index.ts @@ -0,0 +1,86 @@ +// The example is copied from ws server usage with subscriptions-transport-ws backwards compatibility example +// https://the-guild.dev/graphql/ws/recipes#ws-server-usage-with-subscriptions-transport-ws-backwards-compatibility + +import http from "http"; +import { WebSocketServer } from "ws"; // yarn add ws +// import ws from 'ws'; yarn add ws@7 +// const WebSocketServer = ws.Server; +import { execute, subscribe } from "graphql"; +import { GRAPHQL_TRANSPORT_WS_PROTOCOL } from "graphql-ws"; +import { useServer } from "graphql-ws/lib/use/ws"; +import { SubscriptionServer, GRAPHQL_WS } from "subscriptions-transport-ws"; +import { schema } from "./schema"; + +// extra in the context +interface Extra { + readonly request: http.IncomingMessage; +} + +// your custom auth +class Forbidden extends Error {} +function handleAuth(request: http.IncomingMessage) { + // do your auth on every subscription connect + const token = request.headers["authorization"]; + + // or const { iDontApprove } = session(request.cookies); + if (token !== "Bearer random-secret") { + // throw a custom error to be handled + throw new Forbidden(":("); + } +} + +// graphql-ws +const graphqlWs = new WebSocketServer({ noServer: true }); +useServer( + { + schema, + onConnect: async (ctx) => { + // do your auth on every connect (recommended) + await handleAuth(ctx.extra.request); + }, + }, + graphqlWs +); + +// subscriptions-transport-ws +const subTransWs = new WebSocketServer({ noServer: true }); +SubscriptionServer.create( + { + schema, + execute, + subscribe, + }, + subTransWs +); + +// create http server +const server = http.createServer(function weServeSocketsOnly(_, res) { + res.writeHead(404); + res.end(); +}); + +// listen for upgrades and delegate requests according to the WS subprotocol +server.on("upgrade", (req, socket, head) => { + // extract websocket subprotocol from header + const protocol = req.headers["sec-websocket-protocol"]; + const protocols = Array.isArray(protocol) + ? protocol + : protocol?.split(",").map((p) => p.trim()); + + // decide which websocket server to use + const wss = + protocols?.includes(GRAPHQL_WS) && // subscriptions-transport-ws subprotocol + !protocols.includes(GRAPHQL_TRANSPORT_WS_PROTOCOL) // graphql-ws subprotocol + ? subTransWs + : // graphql-ws will welcome its own subprotocol and + // gracefully reject invalid ones. if the client supports + // both transports, graphql-ws will prevail + graphqlWs; + wss.handleUpgrade(req, socket, head, (ws) => { + wss.emit("connection", ws, req); + }); +}); + +const port = 4000; +console.log(`listen server on localhost:${port}`); +server.listen(port); diff --git a/example/graphql-ws-bc/server/package-lock.json b/example/graphql-ws-bc/server/package-lock.json new file mode 100644 index 0000000..ce9c413 --- /dev/null +++ b/example/graphql-ws-bc/server/package-lock.json @@ -0,0 +1,326 @@ +{ + "name": "server", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "server", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "graphql": "^16.8.1", + "graphql-ws": "^5.14.3", + "subscriptions-transport-ws": "^0.11.0", + "ws": "^8.16.0" + }, + "devDependencies": { + "@types/ws": "^8.5.10", + "ts-node": "^10.9.2", + "typescript": "^5.3.3" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz", + "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==", + "dev": true, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==", + "dev": true + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", + "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==", + "dev": true + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", + "dev": true + }, + "node_modules/@types/node": { + "version": "20.11.16", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.16.tgz", + "integrity": "sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/@types/ws": { + "version": "8.5.10", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz", + "integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/acorn": { + "version": "8.11.3", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", + "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", + "dev": true, + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz", + "integrity": "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, + "node_modules/backo2": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", + "integrity": "sha512-zj6Z6M7Eq+PBZ7PQxl5NT665MvJdAkzp0f60nAJ+sLaSCBPMwVak5ZegFbgVCzFcCJTKFoMizvM5Ld7+JrRJHA==" + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/eventemitter3": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz", + "integrity": "sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==" + }, + "node_modules/graphql": { + "version": "16.8.1", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.8.1.tgz", + "integrity": "sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==", + "engines": { + "node": "^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0" + } + }, + "node_modules/graphql-ws": { + "version": "5.14.3", + "resolved": "https://registry.npmjs.org/graphql-ws/-/graphql-ws-5.14.3.tgz", + "integrity": "sha512-F/i2xNIVbaEF2xWggID0X/UZQa2V8kqKDPO8hwmu53bVOcTL7uNkxnexeEgSCVxYBQUTUNEI8+e4LO1FOhKPKQ==", + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "graphql": ">=0.11 <=16" + } + }, + "node_modules/iterall": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.3.0.tgz", + "integrity": "sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==" + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", + "dev": true + }, + "node_modules/subscriptions-transport-ws": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/subscriptions-transport-ws/-/subscriptions-transport-ws-0.11.0.tgz", + "integrity": "sha512-8D4C6DIH5tGiAIpp5I0wD/xRlNiZAPGHygzCe7VzyzUoxHtawzjNAY9SUTXU05/EY2NMY9/9GF0ycizkXr1CWQ==", + "deprecated": "The `subscriptions-transport-ws` package is no longer maintained. We recommend you use `graphql-ws` instead. For help migrating Apollo software to `graphql-ws`, see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws For general help using `graphql-ws`, see https://github.com/enisdenjo/graphql-ws/blob/master/README.md", + "dependencies": { + "backo2": "^1.0.2", + "eventemitter3": "^3.1.0", + "iterall": "^1.2.1", + "symbol-observable": "^1.0.4", + "ws": "^5.2.0 || ^6.0.0 || ^7.0.0" + }, + "peerDependencies": { + "graphql": "^15.7.2 || ^16.0.0" + } + }, + "node_modules/subscriptions-transport-ws/node_modules/ws": { + "version": "7.5.9", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.9.tgz", + "integrity": "sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/symbol-observable": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz", + "integrity": "sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, + "node_modules/ws": { + "version": "8.16.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true, + "engines": { + "node": ">=6" + } + } + } +} diff --git a/example/graphql-ws-bc/server/package.json b/example/graphql-ws-bc/server/package.json new file mode 100644 index 0000000..827fa39 --- /dev/null +++ b/example/graphql-ws-bc/server/package.json @@ -0,0 +1,21 @@ +{ + "name": "server", + "version": "1.0.0", + "description": "graphql-ws backward compatibility server example", + "main": "index.ts", + "scripts": { + "start": "ts-node index.ts" + }, + "license": "MIT", + "dependencies": { + "graphql": "^16.8.1", + "graphql-ws": "^5.14.3", + "subscriptions-transport-ws": "^0.11.0", + "ws": "^8.16.0" + }, + "devDependencies": { + "@types/ws": "^8.5.10", + "ts-node": "^10.9.2", + "typescript": "^5.3.3" + } +} diff --git a/example/graphql-ws-bc/server/schema.ts b/example/graphql-ws-bc/server/schema.ts new file mode 100644 index 0000000..801c10d --- /dev/null +++ b/example/graphql-ws-bc/server/schema.ts @@ -0,0 +1,36 @@ +import { GraphQLSchema, GraphQLObjectType, GraphQLString } from "graphql"; + +/** + * Construct a GraphQL schema and define the necessary resolvers. + * + * type Query { + * hello: String + * } + * type Subscription { + * greetings: String + * } + */ +export const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: "Query", + fields: { + hello: { + type: GraphQLString, + resolve: () => "world", + }, + }, + }), + subscription: new GraphQLObjectType({ + name: "Subscription", + fields: { + greetings: { + type: GraphQLString, + subscribe: async function* () { + for (const hi of ["Hi", "Bonjour", "Hola", "Ciao", "Zdravo"]) { + yield { greetings: hi }; + } + }, + }, + }, + }), +}); diff --git a/example/graphql-ws-bc/server/tsconfig.json b/example/graphql-ws-bc/server/tsconfig.json new file mode 100644 index 0000000..3642e31 --- /dev/null +++ b/example/graphql-ws-bc/server/tsconfig.json @@ -0,0 +1,109 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": false, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/example/graphqldev/go.mod b/example/graphqldev/go.mod new file mode 100644 index 0000000..c8bd327 --- /dev/null +++ b/example/graphqldev/go.mod @@ -0,0 +1,15 @@ +module github.com/hasura/go-graphql-client/example/graphqldev + +go 1.20 + +require ( + github.com/graph-gophers/graphql-go v1.5.0 + github.com/hasura/go-graphql-client v0.11.0 +) + +require ( + github.com/google/uuid v1.6.0 // indirect + nhooyr.io/websocket v1.8.10 // indirect +) + +replace github.com/hasura/go-graphql-client => ../../ diff --git a/example/graphqldev/go.sum b/example/graphqldev/go.sum new file mode 100644 index 0000000..57eaa55 --- /dev/null +++ b/example/graphqldev/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc= +github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI= +go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= +nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/example/hasura/client/graphql-ws/client.go b/example/hasura/client/graphql-ws/client.go index f544ccd..3cc696f 100644 --- a/example/hasura/client/graphql-ws/client.go +++ b/example/hasura/client/graphql-ws/client.go @@ -20,7 +20,9 @@ const ( func main() { go insertUsers() - startSubscription() + if err := startSubscription(); err != nil { + panic(err) + } } func startSubscription() error { diff --git a/example/hasura/client/subscriptions-transport-ws/client.go b/example/hasura/client/subscriptions-transport-ws/client.go index cba1648..c627d5c 100644 --- a/example/hasura/client/subscriptions-transport-ws/client.go +++ b/example/hasura/client/subscriptions-transport-ws/client.go @@ -19,7 +19,9 @@ const ( func main() { go insertUsers() - startSubscription() + if err := startSubscription(); err != nil { + panic(err) + } } func startSubscription() error { @@ -73,7 +75,7 @@ func startSubscription() error { // automatically unsubscribe after 10 seconds go func() { time.Sleep(10 * time.Second) - client.Unsubscribe(subId) + _ = client.Unsubscribe(subId) }() return client.Run() diff --git a/example/realworld/main.go b/example/realworld/main.go index 1bd8949..cd8a446 100644 --- a/example/realworld/main.go +++ b/example/realworld/main.go @@ -5,8 +5,6 @@ import ( "encoding/json" "flag" "log" - "net/http" - "net/http/httptest" "os" graphql "github.com/hasura/go-graphql-client" @@ -61,15 +59,3 @@ func print(v interface{}) { panic(err) } } - -// localRoundTripper is an http.RoundTripper that executes HTTP transactions -// by using handler directly, instead of going over an HTTP connection. -type localRoundTripper struct { - handler http.Handler -} - -func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - w := httptest.NewRecorder() - l.handler.ServeHTTP(w, req) - return w.Result(), nil -} diff --git a/example/subscription/client.go b/example/subscription/client.go index 49e9a23..7a5e3ed 100644 --- a/example/subscription/client.go +++ b/example/subscription/client.go @@ -66,7 +66,7 @@ func startSubscription() error { // automatically unsubscribe after 10 seconds go func() { time.Sleep(10 * time.Second) - client.Unsubscribe(subId) + _ = client.Unsubscribe(subId) }() return client.Run() diff --git a/example/subscription/go.mod b/example/subscription/go.mod new file mode 100644 index 0000000..bf47fc8 --- /dev/null +++ b/example/subscription/go.mod @@ -0,0 +1,17 @@ +module github.com/hasura/go-graphql-client/example/subscription + +go 1.20 + +require ( + github.com/graph-gophers/graphql-go v1.5.0 + github.com/graph-gophers/graphql-transport-ws v0.0.2 + github.com/hasura/go-graphql-client v0.11.0 +) + +require ( + github.com/google/uuid v1.6.0 // indirect + github.com/gorilla/websocket v1.4.1 // indirect + nhooyr.io/websocket v1.8.10 // indirect +) + +replace github.com/hasura/go-graphql-client => ../../ diff --git a/example/subscription/go.sum b/example/subscription/go.sum new file mode 100644 index 0000000..58280d4 --- /dev/null +++ b/example/subscription/go.sum @@ -0,0 +1,30 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc= +github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= +github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo= +github.com/graph-gophers/graphql-transport-ws v0.0.2/go.mod h1:5BVKvFzOd2BalVIBFfnfmHjpJi/MZ5rOj8G55mXvZ8g= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI= +go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= +nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/example/subscription/main.go b/example/subscription/main.go index c5f610e..0c453f5 100644 --- a/example/subscription/main.go +++ b/example/subscription/main.go @@ -8,5 +8,7 @@ package main func main() { go startServer() go startSendHello() - startSubscription() + if err := startSubscription(); err != nil { + panic(err) + } } diff --git a/example/subscription/subscription_test.go b/example/subscription/subscription_test.go new file mode 100644 index 0000000..be3a6e8 --- /dev/null +++ b/example/subscription/subscription_test.go @@ -0,0 +1,559 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log" + "net/http" + "sync" + "testing" + "time" + + "github.com/graph-gophers/graphql-go" + "github.com/graph-gophers/graphql-go/relay" + "github.com/graph-gophers/graphql-transport-ws/graphqlws" + gql "github.com/hasura/go-graphql-client" +) + +func subscription_setupClients(port int) (*gql.Client, *gql.SubscriptionClient) { + endpoint := fmt.Sprintf("http://localhost:%d/graphql", port) + + client := gql.NewClient(endpoint, &http.Client{Transport: http.DefaultTransport}) + + subscriptionClient := gql.NewSubscriptionClient(endpoint). + WithConnectionParams(map[string]interface{}{ + "headers": map[string]string{ + "foo": "bar", + }, + }).WithLog(log.Println) + + return client, subscriptionClient +} + +func subscription_setupServer(port int) *http.Server { + + // init graphQL schema + s, err := graphql.ParseSchema(schema, newResolver()) + if err != nil { + panic(err) + } + + // graphQL handler + mux := http.NewServeMux() + graphQLHandler := graphqlws.NewHandlerFunc(s, &relay.Handler{Schema: s}) + mux.HandleFunc("/graphql", graphQLHandler) + server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux} + + return server +} + +func TestTransportWS_basicTest(t *testing.T) { + stop := make(chan bool) + server := subscription_setupServer(8081) + client, subscriptionClient := subscription_setupClients(8081) + msg := randomID() + go func() { + if err := server.ListenAndServe(); err != nil { + log.Println(err) + } + }() + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer func() { + _ = server.Shutdown(ctx) + }() + defer cancel() + + subscriptionClient. + OnError(func(sc *gql.SubscriptionClient, err error) error { + return err + }) + + /* + subscription { + helloSaid { + id + msg + } + } + */ + var sub struct { + HelloSaid struct { + ID gql.String + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + log.Println("result", string(data)) + e = json.Unmarshal(data, &sub) + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + if sub.HelloSaid.Message != gql.String(msg) { + t.Fatalf("subscription message does not match. got: %s, want: %s", sub.HelloSaid.Message, msg) + } + + return errors.New("exit") + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + go func() { + if err := subscriptionClient.Run(); err == nil || err.Error() != "exit" { + t.Errorf("got error: %v, want: exit", err) + } + stop <- true + }() + + defer subscriptionClient.Close() + + // wait until the subscription client connects to the server + time.Sleep(2 * time.Second) + + // call a mutation request to send message to the subscription + /* + mutation ($msg: String!) { + sayHello(msg: $msg) { + id + msg + } + } + */ + var q struct { + SayHello struct { + ID gql.String + Msg gql.String + } `graphql:"sayHello(msg: $msg)"` + } + variables := map[string]interface{}{ + "msg": gql.String(msg), + } + err = client.Mutate(context.Background(), &q, variables, gql.OperationName("SayHello")) + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + <-stop +} + +func TestTransportWS_exitWhenNoSubscription(t *testing.T) { + server := subscription_setupServer(8085) + client, subscriptionClient := subscription_setupClients(8085) + msg := randomID() + go func() { + if err := server.ListenAndServe(); err != nil { + log.Println(err) + } + }() + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer func() { + _ = server.Shutdown(ctx) + }() + defer cancel() + + subscriptionClient = subscriptionClient. + WithTimeout(3 * time.Second). + OnError(func(sc *gql.SubscriptionClient, err error) error { + t.Fatalf("got error: %v, want: nil", err) + return err + }). + OnDisconnected(func() { + log.Println("disconnected") + }) + /* + subscription { + helloSaid { + id + msg + } + } + */ + var sub struct { + HelloSaid struct { + ID gql.String + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + subId1, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + log.Println("result", string(data)) + e = json.Unmarshal(data, &sub) + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + if sub.HelloSaid.Message != gql.String(msg) { + t.Fatalf("subscription message does not match. got: %s, want: %s", sub.HelloSaid.Message, msg) + } + + return nil + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + /* + subscription { + helloSaid { + id + msg + } + } + */ + var sub2 struct { + HelloSaid struct { + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + subId2, err := subscriptionClient.Subscribe(sub2, nil, func(data []byte, e error) error { + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + log.Println("result", string(data)) + e = json.Unmarshal(data, &sub2) + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + if sub2.HelloSaid.Message != gql.String(msg) { + t.Fatalf("subscription message does not match. got: %s, want: %s", sub2.HelloSaid.Message, msg) + } + + return nil + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + go func() { + // wait until the subscription client connects to the server + time.Sleep(2 * time.Second) + + // call a mutation request to send message to the subscription + /* + mutation ($msg: String!) { + sayHello(msg: $msg) { + id + msg + } + } + */ + var q struct { + SayHello struct { + ID gql.String + Msg gql.String + } `graphql:"sayHello(msg: $msg)"` + } + variables := map[string]interface{}{ + "msg": gql.String(msg), + } + err = client.Mutate(context.Background(), &q, variables, gql.OperationName("SayHello")) + if err != nil { + t.Errorf("got error: %v, want: nil", err) + return + } + + time.Sleep(2 * time.Second) + _ = subscriptionClient.Unsubscribe(subId1) + _ = subscriptionClient.Unsubscribe(subId2) + }() + + defer subscriptionClient.Close() + + if err := subscriptionClient.Run(); err != nil { + t.Fatalf("got error: %v, want: nil", err) + } +} + +func TestTransportWS_onDisconnected(t *testing.T) { + port := 8083 + server := subscription_setupServer(port) + var wasConnected bool + disconnected := make(chan bool) + go func() { + if err := server.ListenAndServe(); err != nil { + log.Println(err) + } + }() + + // init client + _, subscriptionClient := subscription_setupClients(port) + subscriptionClient = subscriptionClient. + WithTimeout(5 * time.Second). + OnError(func(sc *gql.SubscriptionClient, err error) error { + panic(err) + }). + OnConnected(func() { + log.Println("OnConnected") + wasConnected = true + }). + OnDisconnected(func() { + log.Println("OnDisconnected") + disconnected <- true + }) + + /* + subscription { + helloSaid { + id + msg + } + } + */ + var sub struct { + HelloSaid struct { + ID gql.String + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + } + return nil + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + // run client + go func() { + _ = subscriptionClient.Run() + }() + defer subscriptionClient.Close() + + // wait until the subscription client connects to the server + time.Sleep(2 * time.Second) + if err := server.Close(); err != nil { + panic(err) + } + + <-disconnected + + if !wasConnected { + t.Fatal("the OnConnected event must be triggered") + } +} + +func testSubscription_LifeCycleEvents(t *testing.T, syncMode bool) { + + server := subscription_setupServer(8082) + client, subscriptionClient := subscription_setupClients(8082) + msg := randomID() + + var lock sync.Mutex + subscriptionResults := []gql.Subscription{} + wasConnected := false + wasDisconnected := false + addResult := func(s gql.Subscription) int { + lock.Lock() + defer lock.Unlock() + subscriptionResults = append(subscriptionResults, s) + return len(subscriptionResults) + } + + fixtures := []struct { + Query interface{} + Variables map[string]interface{} + ExpectedID string + ExpectedPayload gql.GraphQLRequestPayload + }{ + { + Query: func() interface{} { + var t struct { + HelloSaid struct { + ID gql.String + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + return t + }(), + Variables: nil, + ExpectedPayload: gql.GraphQLRequestPayload{ + Query: "subscription{helloSaid{id,msg}}", + }, + }, + { + Query: func() interface{} { + var t struct { + HelloSaid struct { + Message gql.String `graphql:"msg" json:"msg"` + } `graphql:"helloSaid" json:"helloSaid"` + } + + return t + }(), + Variables: nil, + ExpectedPayload: gql.GraphQLRequestPayload{ + Query: "subscription{helloSaid{msg}}", + }, + }, + } + + go func() { + if err := server.ListenAndServe(); err != nil { + log.Println(err) + } + }() + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer func() { + _ = server.Shutdown(ctx) + }() + + defer cancel() + + subscriptionClient = subscriptionClient. + WithExitWhenNoSubscription(false). + WithTimeout(3 * time.Second). + WithSyncMode(syncMode). + OnConnected(func() { + lock.Lock() + defer lock.Unlock() + log.Println("connected") + wasConnected = true + }). + OnError(func(sc *gql.SubscriptionClient, err error) error { + t.Fatalf("got error: %v, want: nil", err) + return err + }). + OnDisconnected(func() { + lock.Lock() + defer lock.Unlock() + log.Println("disconnected") + wasDisconnected = true + }). + OnSubscriptionComplete(func(s gql.Subscription) { + log.Println("OnSubscriptionComplete: ", s) + length := addResult(s) + if length == len(fixtures) { + log.Println("done, closing...") + subscriptionClient.Close() + } + }) + + for i, f := range fixtures { + id, err := subscriptionClient.Subscribe(f.Query, f.Variables, func(data []byte, e error) error { + lock.Lock() + defer lock.Unlock() + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + log.Println("result", string(data)) + e = json.Unmarshal(data, &f.Query) + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + + return nil + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + fixtures[i].ExpectedID = id + log.Printf("subscribed: %s", id) + } + + go func() { + // wait until the subscription client connects to the server + time.Sleep(2 * time.Second) + + // call a mutation request to send message to the subscription + /* + mutation ($msg: String!) { + sayHello(msg: $msg) { + id + msg + } + } + */ + var q struct { + SayHello struct { + ID gql.String + Msg gql.String + } `graphql:"sayHello(msg: $msg)"` + } + variables := map[string]interface{}{ + "msg": gql.String(msg), + } + err := client.Mutate(context.Background(), &q, variables, gql.OperationName("SayHello")) + if err != nil { + t.Errorf("got error: %v, want: nil", err) + return + } + + time.Sleep(2 * time.Second) + for _, f := range fixtures { + if err := subscriptionClient.Unsubscribe(f.ExpectedID); err != nil { + panic(err) + + } + time.Sleep(time.Second) + } + }() + + defer subscriptionClient.Close() + + if err := subscriptionClient.Run(); err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + if len(subscriptionResults) != len(fixtures) { + t.Fatalf("failed to listen OnSubscriptionComplete event. got %+v, want: %+v", len(subscriptionResults), len(fixtures)) + } + for i, s := range subscriptionResults { + if s.GetID() != fixtures[i].ExpectedID { + t.Fatalf("%d: subscription id not matched, got: %s, want: %s", i, s.GetPayload().Query, fixtures[i].ExpectedPayload.Query) + } + if s.GetPayload().Query != fixtures[i].ExpectedPayload.Query { + t.Fatalf("%d: query output not matched, got: %s, want: %s", i, s.GetPayload().Query, fixtures[i].ExpectedPayload.Query) + } + } + + if !wasConnected { + t.Fatalf("expected OnConnected event, got none") + } + if !wasDisconnected { + t.Fatalf("expected OnDisconnected event, got none") + } +} + +func TestSubscription_LifeCycleEvents(t *testing.T) { + testSubscription_LifeCycleEvents(t, false) +} + +func TestSubscription_WithSyncMode(t *testing.T) { + testSubscription_LifeCycleEvents(t, true) +} diff --git a/example/tibber/client.go b/example/tibber/client.go index 716e1d5..94f99ec 100644 --- a/example/tibber/client.go +++ b/example/tibber/client.go @@ -16,7 +16,9 @@ const ( ) func main() { - startSubscription() + if err := startSubscription(); err != nil { + panic(err) + } } // the subscription uses the Real time subscription demo diff --git a/go.mod b/go.mod index 85acea0..6910049 100644 --- a/go.mod +++ b/go.mod @@ -3,18 +3,6 @@ module github.com/hasura/go-graphql-client go 1.20 require ( - github.com/google/uuid v1.5.0 - github.com/graph-gophers/graphql-go v1.5.0 - github.com/graph-gophers/graphql-transport-ws v0.0.2 + github.com/google/uuid v1.6.0 nhooyr.io/websocket v1.8.10 ) - -require ( - github.com/gorilla/websocket v1.5.1 // indirect - github.com/klauspost/compress v1.17.4 // indirect - golang.org/x/crypto v0.17.0 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/sys v0.16.0 // indirect -) - -replace github.com/gin-gonic/gin v1.6.3 => github.com/gin-gonic/gin v1.9.1 diff --git a/go.sum b/go.sum index 0953f3e..b14acc6 100644 --- a/go.sum +++ b/go.sum @@ -1,116 +1,4 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= -github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= -github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= -github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= -github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= -github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= -github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= -github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= -github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo= -github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= -github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= -github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc= -github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= -github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo= -github.com/graph-gophers/graphql-transport-ws v0.0.2/go.mod h1:5BVKvFzOd2BalVIBFfnfmHjpJi/MZ5rOj8G55mXvZ8g= -github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= -github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= -github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= -github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= -github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= -github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI= -go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b h1:Qwe1rC8PSniVfAFPFJeyUkB+zcysC3RgJBAGk7eqBEU= -golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= -golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= -nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/graphql.go b/graphql.go index 7fe1ed7..706092d 100644 --- a/graphql.go +++ b/graphql.go @@ -154,7 +154,7 @@ func (c *Client) request(ctx context.Context, query string, variables map[string resp, err := c.httpClient.Do(request) if c.debug { - reqReader.Seek(0, io.SeekStart) + _, _ = reqReader.Seek(0, io.SeekStart) } if err != nil { @@ -206,7 +206,7 @@ func (c *Client) request(ctx context.Context, query string, variables map[string err = json.NewDecoder(r).Decode(&out) if c.debug { - respReader.Seek(0, io.SeekStart) + _, _ = respReader.Seek(0, io.SeekStart) } if err != nil { @@ -332,22 +332,38 @@ type Error struct { Line int `json:"line"` Column int `json:"column"` } `json:"locations"` + Path []interface{} `json:"path"` + err error } // Error implements error interface. func (e Error) Error() string { - return fmt.Sprintf("Message: %s, Locations: %+v, Extensions: %+v", e.Message, e.Locations, e.Extensions) + return fmt.Sprintf("Message: %s, Locations: %+v, Extensions: %+v, Path: %+v", e.Message, e.Locations, e.Extensions, e.Path) +} + +// Unwrap implement the unwrap interface. +func (e Error) Unwrap() error { + return e.err } // Error implements error interface. func (e Errors) Error() string { b := strings.Builder{} for _, err := range e { - b.WriteString(err.Error()) + _, _ = b.WriteString(err.Error()) } return b.String() } +// Unwrap implements the error unwrap interface. +func (e Errors) Unwrap() []error { + var errs []error + for _, err := range e { + errs = append(errs, err.err) + } + return errs +} + func (e Error) getInternalExtension() map[string]interface{} { if e.Extensions == nil { return make(map[string]interface{}) @@ -366,6 +382,7 @@ func newError(code string, err error) Error { Extensions: map[string]interface{}{ "code": code, }, + err: err, } } diff --git a/graphql_test.go b/graphql_test.go index dfc7c1c..224e926 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -60,7 +60,7 @@ func TestClient_Query_partialDataWithErrorResponse(t *testing.T) { if err == nil { t.Fatal("got error: nil, want: non-nil") } - if got, want := err.Error(), "Message: Could not resolve to a node with the global id of 'NotExist', Locations: [{Line:10 Column:4}], Extensions: map[]"; got != want { + if got, want := err.Error(), "Message: Could not resolve to a node with the global id of 'NotExist', Locations: [{Line:10 Column:4}], Extensions: map[], Path: [node2]"; got != want { t.Errorf("got error: %v, want: %v", got, want) } @@ -110,7 +110,7 @@ func TestClient_Query_partialDataRawQueryWithErrorResponse(t *testing.T) { if err == nil { t.Fatal("got error: nil, want: non-nil\n") } - if got, want := err.Error(), "Message: Could not resolve to a node with the global id of 'NotExist', Locations: [{Line:10 Column:4}], Extensions: map[]"; got != want { + if got, want := err.Error(), "Message: Could not resolve to a node with the global id of 'NotExist', Locations: [{Line:10 Column:4}], Extensions: map[], Path: [node2]"; got != want { t.Errorf("got error: %v, want: %v\n", got, want) } if q.Node1 == nil || string(q.Node1) != `{"id":"MDEyOklzc3VlQ29tbWVudDE2OTQwNzk0Ng=="}` { @@ -165,7 +165,7 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) { if err == nil { t.Fatal("got error: nil, want: non-nil") } - if got, want := err.Error(), "Message: Field 'user' is missing required arguments: login, Locations: [{Line:7 Column:3}], Extensions: map[]"; got != want { + if got, want := err.Error(), "Message: Field 'user' is missing required arguments: login, Locations: [{Line:7 Column:3}], Extensions: map[], Path: []"; got != want { t.Errorf("got error: %v, want: %v", got, want) } if q.User.Name != "" { @@ -215,7 +215,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) { if err == nil { t.Fatal("got error: nil, want: non-nil") } - if got, want := err.Error(), `Message: 500 Internal Server Error; body: "important message\n", Locations: [], Extensions: map[code:request_error]`; got != want { + if got, want := err.Error(), `Message: 500 Internal Server Error; body: "important message\n", Locations: [], Extensions: map[code:request_error], Path: []`; got != want { t.Errorf("got error: %v, want: %v", got, want) } if q.User.Name != "" { @@ -253,6 +253,63 @@ func TestClient_Query_errorStatusCode(t *testing.T) { } } +func TestClient_Query_requestError(t *testing.T) { + want := errors.New("bad error") + client := graphql.NewClient("/graphql", &http.Client{Transport: errorRoundTripper{err: want}}) + + var q struct { + User struct { + Name string + } + } + err := client.Query(context.Background(), &q, nil) + if err == nil { + t.Fatal("got error: nil, want: non-nil") + } + if got, want := err.Error(), `Message: Post "/graphql": bad error, Locations: [], Extensions: map[code:request_error], Path: []`; got != want { + t.Errorf("got error: %v, want: %v", got, want) + } + if q.User.Name != "" { + t.Errorf("got non-empty q.User.Name: %v", q.User.Name) + } + if got := err; !errors.Is(got, want) { + t.Errorf("got error: %v, want: %v", got, want) + } + + gqlErr := err.(graphql.Errors) + if got, want := gqlErr[0].Extensions["code"], graphql.ErrRequestError; got != want { + t.Errorf("got error: %v, want: %v", got, want) + } + if _, ok := gqlErr[0].Extensions["internal"]; ok { + t.Errorf("expected empty internal error") + } + if got := gqlErr[0]; !errors.Is(err, want) { + t.Errorf("got error: %v, want %v", got, want) + } + + // test internal error data + client = client.WithDebug(true) + err = client.Query(context.Background(), &q, nil) + if err == nil { + t.Fatal("got error: nil, want: non-nil") + } + if !errors.As(err, &graphql.Errors{}) { + t.Errorf("the error type should be graphql.Errors") + } + gqlErr = err.(graphql.Errors) + if got, want := gqlErr[0].Message, `Post "/graphql": bad error`; got != want { + t.Errorf("got error: %v, want: %v", got, want) + } + if got, want := gqlErr[0].Extensions["code"], graphql.ErrRequestError; got != want { + t.Errorf("got error: %v, want: %v", got, want) + } + interErr := gqlErr[0].Extensions["internal"].(map[string]interface{}) + + if got, want := interErr["request"].(map[string]interface{})["body"], "{\"query\":\"{user{name}}\"}\n"; got != want { + t.Errorf("got error: %v, want: %v", got, want) + } +} + // Test that an empty (but non-nil) variables map is // handled no differently than a nil variables map. func TestClient_Query_emptyVariables(t *testing.T) { @@ -425,6 +482,16 @@ func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) return w.Result(), nil } +// errorRoundTripper is an http.RoundTripper that always returns the supplied +// error. +type errorRoundTripper struct { + err error +} + +func (e errorRoundTripper) RoundTrip(_ *http.Request) (*http.Response, error) { + return nil, e.err +} + func mustRead(r io.Reader) string { b, err := io.ReadAll(r) if err != nil { diff --git a/pkg/jsonutil/graphql_test.go b/pkg/jsonutil/graphql_test.go index 9c3ef84..d6b2ecf 100644 --- a/pkg/jsonutil/graphql_test.go +++ b/pkg/jsonutil/graphql_test.go @@ -508,13 +508,17 @@ func TestUnmarshalGraphQL_unexportedField(t *testing.T) { type query struct { foo *string } - err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}`), new(query)) + q := new(query) + err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}`), q) if err == nil { t.Fatal("got error: nil, want: non-nil") } if got, want := err.Error(), "struct field for \"foo\" doesn't exist in any of 1 places to unmarshal"; got != want { t.Errorf("got error: %v, want: %v", got, want) } + if q.foo != nil { + t.Errorf("expected foo = nil, got: %v", q.foo) + } } func TestUnmarshalGraphQL_multipleValues(t *testing.T) { diff --git a/query.go b/query.go index 1ee9d7c..dfd6edc 100644 --- a/query.go +++ b/query.go @@ -140,9 +140,9 @@ func queryArguments(variables map[string]interface{}) string { var buf bytes.Buffer for _, k := range keys { - io.WriteString(&buf, "$") - io.WriteString(&buf, k) - io.WriteString(&buf, ":") + _, _ = io.WriteString(&buf, "$") + _, _ = io.WriteString(&buf, k) + _, _ = io.WriteString(&buf, ":") writeArgumentType(&buf, reflect.TypeOf(variables[k]), variables[k], true) // Don't insert a comma here. // Commas in GraphQL are insignificant, and we want minified output. @@ -168,10 +168,10 @@ func writeArgumentType(w io.Writer, t reflect.Type, v interface{}, value bool) { graphqlType, ok = reflect.Zero(t).Interface().(GraphQLType) } if ok { - io.WriteString(w, graphqlType.GetGraphQLType()) + _, _ = io.WriteString(w, graphqlType.GetGraphQLType()) if value { // Value is a required type, so add "!" to the end. - io.WriteString(w, "!") + _, _ = io.WriteString(w, "!") } return } @@ -186,27 +186,27 @@ func writeArgumentType(w io.Writer, t reflect.Type, v interface{}, value bool) { switch t.Kind() { case reflect.Slice, reflect.Array: // List. E.g., "[Int]". - io.WriteString(w, "[") + _, _ = io.WriteString(w, "[") writeArgumentType(w, t.Elem(), nil, true) - io.WriteString(w, "]") + _, _ = io.WriteString(w, "]") case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - io.WriteString(w, "Int") + _, _ = io.WriteString(w, "Int") case reflect.Float32, reflect.Float64: - io.WriteString(w, "Float") + _, _ = io.WriteString(w, "Float") case reflect.Bool: - io.WriteString(w, "Boolean") + _, _ = io.WriteString(w, "Boolean") default: n := t.Name() if n == "string" { n = "String" } - io.WriteString(w, n) + _, _ = io.WriteString(w, n) } if value { // Value is a required type, so add "!" to the end. - io.WriteString(w, "!") + _, _ = io.WriteString(w, "!") } } @@ -241,7 +241,7 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error return nil } if !inline { - io.WriteString(w, "{") + _, _ = io.WriteString(w, "{") } iter := 0 for i := 0; i < t.NumField(); i++ { @@ -252,16 +252,16 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error continue } if iter != 0 { - io.WriteString(w, ",") + _, _ = io.WriteString(w, ",") } iter++ inlineField := f.Anonymous && !ok if !inlineField { if ok { - io.WriteString(w, value) + _, _ = io.WriteString(w, value) } else { - io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase()) + _, _ = io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase()) } } // Skip writeQuery if the GraphQL type associated with the filed is scalar @@ -274,7 +274,7 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error } } if !inline { - io.WriteString(w, "}") + _, _ = io.WriteString(w, "}") } case reflect.Slice: if t.Elem().Kind() != reflect.Array { diff --git a/subscription.go b/subscription.go index 0318fc1..ce61286 100644 --- a/subscription.go +++ b/subscription.go @@ -408,6 +408,11 @@ func (sc *SubscriptionClient) GetContext() context.Context { return sc.getContext().GetContext() } +// GetSubscription get the subscription state by id +func (sc *SubscriptionClient) GetSubscription(id string) *Subscription { + return sc.getContext().GetSubscription(id) +} + // WithWebSocket replaces customized websocket client constructor // In default, subscription client uses https://github.com/nhooyr/websocket func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (WebsocketConn, error)) *SubscriptionClient { @@ -742,7 +747,7 @@ func (sc *SubscriptionClient) Run() error { } if err := sc.init(); err != nil { - return fmt.Errorf("retry timeout. exiting...") + return fmt.Errorf("retry timeout, %w", err) } subContext := sc.getContext() @@ -885,7 +890,7 @@ func (sc *SubscriptionClient) reset() { continue } if sub.status == SubscriptionRunning { - sc.protocol.Unsubscribe(subContext, sub) + _ = sc.protocol.Unsubscribe(subContext, sub) } // should restart subscriptions with new id @@ -1063,8 +1068,12 @@ func (wh *WebsocketHandler) GetCloseStatus(err error) int32 { func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) { options := &websocket.DialOptions{ - Subprotocols: sc.protocol.GetSubprotocols(), - HTTPClient: sc.websocketOptions.HTTPClient, + Subprotocols: sc.protocol.GetSubprotocols(), + HTTPClient: sc.websocketOptions.HTTPClient, + HTTPHeader: sc.websocketOptions.HTTPHeader, + Host: sc.websocketOptions.Host, + CompressionMode: sc.websocketOptions.CompressionMode, + CompressionThreshold: sc.websocketOptions.CompressionThreshold, } c, _, err := websocket.Dial(sc.GetContext(), sc.GetURL(), options) @@ -1082,5 +1091,26 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) { // WebsocketOptions allows implementation agnostic configuration of the websocket client type WebsocketOptions struct { // HTTPClient is used for the connection. + // Its Transport must return writable bodies for WebSocket handshakes. + // http.Transport does beginning with Go 1.12. HTTPClient *http.Client + + // HTTPHeader specifies the HTTP headers included in the handshake request. + HTTPHeader http.Header + + // Host optionally overrides the Host HTTP header to send. If empty, the value + // of URL.Host will be used. + Host string + + // CompressionMode controls the compression mode. + // Defaults to CompressionDisabled. + // + // See docs on CompressionMode for details. + CompressionMode websocket.CompressionMode + + // CompressionThreshold controls the minimum size of a message before compression is applied. + // + // Defaults to 512 bytes for CompressionNoContextTakeover and 128 bytes + // for CompressionContextTakeover. + CompressionThreshold int } diff --git a/subscription_graphql_ws_test.go b/subscription_graphql_ws_test.go index 051e8e6..5dee600 100644 --- a/subscription_graphql_ws_test.go +++ b/subscription_graphql_ws_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "log" + "math/rand" "net/http" "testing" "time" @@ -77,6 +78,16 @@ func waitService(endpoint string, timeoutSecs int) error { return errors.New("unknown error") } +func randomID() string { + var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + + b := make([]rune, 16) + for i := range b { + b[i] = letter[rand.Intn(len(letter))] + } + return string(b) +} + func waitHasuraService(timeoutSecs int) error { return waitService(fmt.Sprintf("%s/healthz", hasuraTestHost), timeoutSecs) } @@ -137,7 +148,7 @@ func TestGraphqlWS_Subscription(t *testing.T) { go func() { if err := subscriptionClient.Run(); err == nil || err.Error() != "exit" { - (*t).Fatalf("got error: %v, want: exit", err) + t.Errorf("got error: %v, want: exit", err) } stop <- true }() @@ -236,7 +247,7 @@ func TestGraphqlWS_SubscriptionRerun(t *testing.T) { go func() { if err := subscriptionClient.Run(); err != nil { - (*t).Fatalf("got error: %v, want: nil", err) + t.Errorf("got error: %v, want: nil", err) } }() @@ -280,11 +291,11 @@ func TestGraphqlWS_SubscriptionRerun(t *testing.T) { time.Sleep(2 * time.Second) go func() { time.Sleep(2 * time.Second) - subscriptionClient.Unsubscribe(subId1) + _ = subscriptionClient.Unsubscribe(subId1) }() if err := subscriptionClient.Run(); err != nil { - (*t).Fatalf("got error: %v, want: nil", err) + t.Fatalf("got error: %v, want: nil", err) } } @@ -351,7 +362,7 @@ func TestGraphQLWS_OnError(t *testing.T) { go func() { if err := subscriptionClient.Run(); err == nil || websocket.CloseStatus(err) != 4400 { - (*t).Fatalf("got error: %v, want: 4400", err) + t.Errorf("got error: %v, want: 4400", err) } stop <- true }() diff --git a/subscription_test.go b/subscription_test.go deleted file mode 100644 index 5add1f2..0000000 --- a/subscription_test.go +++ /dev/null @@ -1,428 +0,0 @@ -package graphql - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "log" - "sync" - "testing" - "time" - - "nhooyr.io/websocket" -) - -func testSubscription_LifeCycleEvents(t *testing.T, syncMode bool) { - - server := subscription_setupServer(8082) - client, subscriptionClient := subscription_setupClients(8082) - msg := randomID() - - var lock sync.Mutex - subscriptionResults := []Subscription{} - wasConnected := false - wasDisconnected := false - addResult := func(s Subscription) int { - lock.Lock() - defer lock.Unlock() - subscriptionResults = append(subscriptionResults, s) - return len(subscriptionResults) - } - - fixtures := []struct { - Query interface{} - Variables map[string]interface{} - Subscription *Subscription - }{ - { - Query: func() interface{} { - var t struct { - HelloSaid struct { - ID String - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` - } - - return t - }(), - Variables: nil, - Subscription: &Subscription{ - payload: GraphQLRequestPayload{ - Query: "subscription{helloSaid{id,msg}}", - }, - }, - }, - { - Query: func() interface{} { - var t struct { - HelloSaid struct { - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` - } - - return t - }(), - Variables: nil, - Subscription: &Subscription{ - payload: GraphQLRequestPayload{ - Query: "subscription{helloSaid{msg}}", - }, - }, - }, - } - - go func() { - if err := server.ListenAndServe(); err != nil { - log.Println(err) - } - }() - - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer server.Shutdown(ctx) - defer cancel() - - subscriptionClient = subscriptionClient. - WithExitWhenNoSubscription(false). - WithTimeout(3 * time.Second). - WithSyncMode(syncMode). - OnConnected(func() { - lock.Lock() - defer lock.Unlock() - log.Println("connected") - wasConnected = true - }). - OnError(func(sc *SubscriptionClient, err error) error { - t.Fatalf("got error: %v, want: nil", err) - return err - }). - OnDisconnected(func() { - lock.Lock() - defer lock.Unlock() - log.Println("disconnected") - wasDisconnected = true - }). - OnSubscriptionComplete(func(s Subscription) { - log.Println("OnSubscriptionComplete: ", s) - length := addResult(s) - if length == len(fixtures) { - log.Println("done, closing...") - subscriptionClient.Close() - } - }) - - for _, f := range fixtures { - id, err := subscriptionClient.Subscribe(f.Query, f.Variables, func(data []byte, e error) error { - lock.Lock() - defer lock.Unlock() - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - log.Println("result", string(data)) - e = json.Unmarshal(data, &f.Query) - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - return nil - }) - - if err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - f.Subscription.id = id - log.Printf("subscribed: %s; subscriptions %+v", id, subscriptionClient.context.subscriptions) - } - - go func() { - // wait until the subscription client connects to the server - time.Sleep(2 * time.Second) - - // call a mutation request to send message to the subscription - /* - mutation ($msg: String!) { - sayHello(msg: $msg) { - id - msg - } - } - */ - var q struct { - SayHello struct { - ID String - Msg String - } `graphql:"sayHello(msg: $msg)"` - } - variables := map[string]interface{}{ - "msg": String(msg), - } - err := client.Mutate(context.Background(), &q, variables, OperationName("SayHello")) - if err != nil { - (*t).Fatalf("got error: %v, want: nil", err) - } - - time.Sleep(2 * time.Second) - for _, f := range fixtures { - log.Println("unsubscribing ", f.Subscription.id) - if err := subscriptionClient.Unsubscribe(f.Subscription.id); err != nil { - log.Printf("subscriptions: %+v", subscriptionClient.context.subscriptions) - panic(err) - - } - time.Sleep(time.Second) - } - }() - - defer subscriptionClient.Close() - - if err := subscriptionClient.Run(); err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - - if len(subscriptionResults) != len(fixtures) { - t.Fatalf("failed to listen OnSubscriptionComplete event. got %+v, want: %+v", len(subscriptionResults), len(fixtures)) - } - for i, s := range subscriptionResults { - if s.id != fixtures[i].Subscription.id { - t.Fatalf("%d: subscription id not matched, got: %s, want: %s", i, s.GetPayload().Query, fixtures[i].Subscription.payload.Query) - } - if s.GetPayload().Query != fixtures[i].Subscription.payload.Query { - t.Fatalf("%d: query output not matched, got: %s, want: %s", i, s.GetPayload().Query, fixtures[i].Subscription.payload.Query) - } - } - - if !wasConnected { - t.Fatalf("expected OnConnected event, got none") - } - if !wasDisconnected { - t.Fatalf("expected OnDisconnected event, got none") - } -} - -func TestSubscription_LifeCycleEvents(t *testing.T) { - testSubscription_LifeCycleEvents(t, false) -} - -func TestSubscription_WithSyncMode(t *testing.T) { - testSubscription_LifeCycleEvents(t, true) -} - -func TestSubscription_WithRetryStatusCodes(t *testing.T) { - stop := make(chan bool) - msg := randomID() - disconnectedCount := 0 - subscriptionClient := NewSubscriptionClient(fmt.Sprintf("%s/v1/graphql", hasuraTestHost)). - WithProtocol(GraphQLWS). - WithRetryStatusCodes("4400"). - WithConnectionParams(map[string]interface{}{ - "headers": map[string]string{ - "x-hasura-admin-secret": "test", - }, - }).WithLog(log.Println). - OnDisconnected(func() { - disconnectedCount++ - if disconnectedCount > 5 { - stop <- true - } - }). - OnError(func(sc *SubscriptionClient, err error) error { - t.Fatal("should not receive error") - return err - }) - - /* - subscription { - user { - id - name - } - } - */ - var sub struct { - Users []struct { - ID int `graphql:"id"` - Name string `graphql:"name"` - } `graphql:"user(order_by: { id: desc }, limit: 5)"` - } - - _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - log.Println("result", string(data)) - e = json.Unmarshal(data, &sub) - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - if len(sub.Users) > 0 && sub.Users[0].Name != msg { - t.Fatalf("subscription message does not match. got: %s, want: %s", sub.Users[0].Name, msg) - } - - return nil - }) - - if err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - - go func() { - if err := subscriptionClient.Run(); err != nil && websocket.CloseStatus(err) == 4400 { - (*t).Fatalf("should not get error 4400, got error: %v, want: nil", err) - } - }() - - defer subscriptionClient.Close() - - // wait until the subscription client connects to the server - if err := waitHasuraService(60); err != nil { - t.Fatalf("failed to start hasura service: %s", err) - } - - <-stop -} - -func TestSubscription_parseInt32Ranges(t *testing.T) { - fixtures := []struct { - Input []string - Expected [][]int32 - Error error - }{ - { - Input: []string{"1", "2", "3-5"}, - Expected: [][]int32{{1}, {2}, {3, 5}}, - }, - { - Input: []string{"a", "2", "3-5"}, - Error: errors.New("invalid status code; input: a"), - }, - } - - for i, f := range fixtures { - output, err := parseInt32Ranges(f.Input) - if f.Expected != nil && fmt.Sprintf("%v", output) != fmt.Sprintf("%v", f.Expected) { - t.Fatalf("%d: got: %+v, want: %+v", i, output, f.Expected) - } - if f.Error != nil && f.Error.Error() != err.Error() { - t.Fatalf("%d: error should equal, got: %+v, want: %+v", i, err, f.Error) - } - } -} - -func TestSubscription_closeThenRun(t *testing.T) { - _, subscriptionClient := hasura_setupClients(GraphQLWS) - - fixtures := []struct { - Query interface{} - Variables map[string]interface{} - Subscription *Subscription - }{ - { - Query: func() interface{} { - var t struct { - Users []struct { - ID int `graphql:"id"` - Name string `graphql:"name"` - } `graphql:"user(order_by: { id: desc }, limit: 5)"` - } - - return t - }(), - Variables: nil, - Subscription: &Subscription{ - payload: GraphQLRequestPayload{ - Query: "subscription{helloSaid{id,msg}}", - }, - }, - }, - { - Query: func() interface{} { - var t struct { - Users []struct { - ID int `graphql:"id"` - } `graphql:"user(order_by: { id: desc }, limit: 5)"` - } - - return t - }(), - Variables: nil, - Subscription: &Subscription{ - payload: GraphQLRequestPayload{ - Query: "subscription{helloSaid{msg}}", - }, - }, - }, - } - - subscriptionClient = subscriptionClient. - WithExitWhenNoSubscription(false). - WithTimeout(3 * time.Second). - OnError(func(sc *SubscriptionClient, err error) error { - t.Fatalf("got error: %v, want: nil", err) - return err - }) - - bulkSubscribe := func() { - - for _, f := range fixtures { - id, err := subscriptionClient.Subscribe(f.Query, f.Variables, func(data []byte, e error) error { - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - return nil - }) - - if err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - log.Printf("subscribed: %s", id) - } - } - - bulkSubscribe() - - go func() { - if err := subscriptionClient.Run(); err != nil { - (*t).Fatalf("got error: %v, want: nil", err) - } - }() - - time.Sleep(3 * time.Second) - if err := subscriptionClient.Close(); err != nil { - (*t).Fatalf("got error: %v, want: nil", err) - } - - bulkSubscribe() - - go func() { - length := subscriptionClient.getContext().GetSubscriptionsLength(nil) - if length != 2 { - (*t).Fatalf("unexpected subscription client. got: %d, want: 2", length) - } - - waitingLen := subscriptionClient.getContext().GetSubscriptionsLength([]SubscriptionStatus{SubscriptionWaiting}) - if waitingLen != 2 { - (*t).Fatalf("unexpected waiting subscription client. got: %d, want: 2", waitingLen) - } - if err := subscriptionClient.Run(); err != nil { - (*t).Fatalf("got error: %v, want: nil", err) - panic(err) - } - }() - - time.Sleep(3 * time.Second) - length := subscriptionClient.getContext().GetSubscriptionsLength(nil) - if length != 2 { - (*t).Fatalf("unexpected subscription client after restart. got: %d, want: 2, subscriptions: %+v", length, subscriptionClient.context.subscriptions) - } - if err := subscriptionClient.Close(); err != nil { - t.Fatalf("got error: %v, want: nil", err) - } -} diff --git a/subscriptions_transport_ws_test.go b/subscriptions_transport_ws_test.go index 9baaa78..e2bb921 100644 --- a/subscriptions_transport_ws_test.go +++ b/subscriptions_transport_ws_test.go @@ -6,204 +6,48 @@ import ( "errors" "fmt" "log" - "math/rand" - "net/http" "testing" "time" - "github.com/graph-gophers/graphql-go" - "github.com/graph-gophers/graphql-go/relay" - "github.com/graph-gophers/graphql-transport-ws/graphqlws" + "nhooyr.io/websocket" ) -const schema = ` -schema { - subscription: Subscription - mutation: Mutation - query: Query -} -type Query { - hello: String! -} -type Subscription { - helloSaid(): HelloSaidEvent! -} -type Mutation { - sayHello(msg: String!): HelloSaidEvent! -} -type HelloSaidEvent { - id: String! - msg: String! -} -` - -func subscription_setupClients(port int) (*Client, *SubscriptionClient) { - endpoint := fmt.Sprintf("http://localhost:%d/graphql", port) - - client := NewClient(endpoint, &http.Client{Transport: http.DefaultTransport}) - - subscriptionClient := NewSubscriptionClient(endpoint). +func TestSubscription_WithRetryStatusCodes(t *testing.T) { + stop := make(chan bool) + msg := randomID() + disconnectedCount := 0 + subscriptionClient := NewSubscriptionClient(fmt.Sprintf("%s/v1/graphql", hasuraTestHost)). + WithProtocol(GraphQLWS). + WithRetryStatusCodes("4400"). WithConnectionParams(map[string]interface{}{ "headers": map[string]string{ - "foo": "bar", + "x-hasura-admin-secret": "test", }, - }).WithLog(log.Println) - - return client, subscriptionClient -} - -func subscription_setupServer(port int) *http.Server { - - // init graphQL schema - s, err := graphql.ParseSchema(schema, newResolver()) - if err != nil { - panic(err) - } - - // graphQL handler - mux := http.NewServeMux() - graphQLHandler := graphqlws.NewHandlerFunc(s, &relay.Handler{Schema: s}) - mux.HandleFunc("/graphql", graphQLHandler) - server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux} - - return server -} - -type resolver struct { - helloSaidEvents chan *helloSaidEvent - helloSaidSubscriber chan *helloSaidSubscriber -} - -func newResolver() *resolver { - r := &resolver{ - helloSaidEvents: make(chan *helloSaidEvent), - helloSaidSubscriber: make(chan *helloSaidSubscriber), - } - - go r.broadcastHelloSaid() - - return r -} - -func (r *resolver) Hello() string { - return "Hello world!" -} - -func (r *resolver) SayHello(args struct{ Msg string }) *helloSaidEvent { - e := &helloSaidEvent{msg: args.Msg, id: randomID()} - go func() { - select { - case r.helloSaidEvents <- e: - case <-time.After(1 * time.Second): - } - }() - return e -} - -type helloSaidSubscriber struct { - stop <-chan struct{} - events chan<- *helloSaidEvent -} - -func (r *resolver) broadcastHelloSaid() { - subscribers := map[string]*helloSaidSubscriber{} - unsubscribe := make(chan string) - - // NOTE: subscribing and sending events are at odds. - for { - select { - case id := <-unsubscribe: - delete(subscribers, id) - case s := <-r.helloSaidSubscriber: - id := randomID() - log.Println("new client subscribed: ", id) - subscribers[id] = s - case e := <-r.helloSaidEvents: - for id, s := range subscribers { - go func(id string, s *helloSaidSubscriber) { - select { - case <-s.stop: - unsubscribe <- id - return - default: - } - - select { - case <-s.stop: - unsubscribe <- id - case s.events <- e: - case <-time.After(time.Second): - } - }(id, s) + }).WithLog(log.Println). + OnDisconnected(func() { + disconnectedCount++ + if disconnectedCount > 5 { + stop <- true } - } - } -} - -func (r *resolver) HelloSaid(ctx context.Context) <-chan *helloSaidEvent { - c := make(chan *helloSaidEvent) - // NOTE: this could take a while - r.helloSaidSubscriber <- &helloSaidSubscriber{events: c, stop: ctx.Done()} - - return c -} - -type helloSaidEvent struct { - id string - msg string -} - -func (r *helloSaidEvent) Msg() string { - return r.msg -} - -func (r *helloSaidEvent) ID() string { - return r.id -} - -func randomID() string { - var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - - b := make([]rune, 16) - for i := range b { - b[i] = letter[rand.Intn(len(letter))] - } - return string(b) -} - -func TestTransportWS_basicTest(t *testing.T) { - stop := make(chan bool) - server := subscription_setupServer(8081) - client, subscriptionClient := subscription_setupClients(8081) - msg := randomID() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Println(err) - } - }() - - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer server.Shutdown(ctx) - defer cancel() - - subscriptionClient. + }). OnError(func(sc *SubscriptionClient, err error) error { + t.Fatal("should not receive error") return err }) /* subscription { - helloSaid { + user { id - msg + name } } */ var sub struct { - HelloSaid struct { - ID String - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` + Users []struct { + ID int `graphql:"id"` + Name string `graphql:"name"` + } `graphql:"user(order_by: { id: desc }, limit: 5)"` } _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { @@ -219,11 +63,11 @@ func TestTransportWS_basicTest(t *testing.T) { return nil } - if sub.HelloSaid.Message != String(msg) { - t.Fatalf("subscription message does not match. got: %s, want: %s", sub.HelloSaid.Message, msg) + if len(sub.Users) > 0 && sub.Users[0].Name != msg { + t.Fatalf("subscription message does not match. got: %s, want: %s", sub.Users[0].Name, msg) } - return errors.New("exit") + return nil }) if err != nil { @@ -231,134 +75,213 @@ func TestTransportWS_basicTest(t *testing.T) { } go func() { - if err := subscriptionClient.Run(); err == nil || err.Error() != "exit" { - (*t).Fatalf("got error: %v, want: exit", err) + if err := subscriptionClient.Run(); err != nil && websocket.CloseStatus(err) == 4400 { + t.Errorf("should not get error 4400, got error: %v, want: nil", err) } - stop <- true }() defer subscriptionClient.Close() // wait until the subscription client connects to the server - time.Sleep(2 * time.Second) - - // call a mutation request to send message to the subscription - /* - mutation ($msg: String!) { - sayHello(msg: $msg) { - id - msg - } - } - */ - var q struct { - SayHello struct { - ID String - Msg String - } `graphql:"sayHello(msg: $msg)"` - } - variables := map[string]interface{}{ - "msg": String(msg), - } - err = client.Mutate(context.Background(), &q, variables, OperationName("SayHello")) - if err != nil { - t.Fatalf("got error: %v, want: nil", err) + if err := waitHasuraService(60); err != nil { + t.Fatalf("failed to start hasura service: %s", err) } <-stop } -func TestTransportWS_exitWhenNoSubscription(t *testing.T) { - server := subscription_setupServer(8085) - client, subscriptionClient := subscription_setupClients(8085) - msg := randomID() - go func() { - if err := server.ListenAndServe(); err != nil { - log.Println(err) +func TestSubscription_parseInt32Ranges(t *testing.T) { + fixtures := []struct { + Input []string + Expected [][]int32 + Error error + }{ + { + Input: []string{"1", "2", "3-5"}, + Expected: [][]int32{{1}, {2}, {3, 5}}, + }, + { + Input: []string{"a", "2", "3-5"}, + Error: errors.New("invalid status code; input: a"), + }, + } + + for i, f := range fixtures { + output, err := parseInt32Ranges(f.Input) + if f.Expected != nil && fmt.Sprintf("%v", output) != fmt.Sprintf("%v", f.Expected) { + t.Fatalf("%d: got: %+v, want: %+v", i, output, f.Expected) } - }() + if f.Error != nil && f.Error.Error() != err.Error() { + t.Fatalf("%d: error should equal, got: %+v, want: %+v", i, err, f.Error) + } + } +} - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer server.Shutdown(ctx) - defer cancel() +func TestSubscription_closeThenRun(t *testing.T) { + _, subscriptionClient := hasura_setupClients(GraphQLWS) + + fixtures := []struct { + Query interface{} + Variables map[string]interface{} + Subscription *Subscription + }{ + { + Query: func() interface{} { + var t struct { + Users []struct { + ID int `graphql:"id"` + Name string `graphql:"name"` + } `graphql:"user(order_by: { id: desc }, limit: 5)"` + } + + return t + }(), + Variables: nil, + Subscription: &Subscription{ + payload: GraphQLRequestPayload{ + Query: "subscription{helloSaid{id,msg}}", + }, + }, + }, + { + Query: func() interface{} { + var t struct { + Users []struct { + ID int `graphql:"id"` + } `graphql:"user(order_by: { id: desc }, limit: 5)"` + } + + return t + }(), + Variables: nil, + Subscription: &Subscription{ + payload: GraphQLRequestPayload{ + Query: "subscription{helloSaid{msg}}", + }, + }, + }, + } subscriptionClient = subscriptionClient. + WithExitWhenNoSubscription(false). WithTimeout(3 * time.Second). OnError(func(sc *SubscriptionClient, err error) error { t.Fatalf("got error: %v, want: nil", err) return err - }). - OnDisconnected(func() { - log.Println("disconnected") }) - /* - subscription { - helloSaid { - id - msg + + bulkSubscribe := func() { + + for _, f := range fixtures { + id, err := subscriptionClient.Subscribe(f.Query, f.Variables, func(data []byte, e error) error { + if e != nil { + t.Fatalf("got error: %v, want: nil", e) + return nil + } + return nil + }) + + if err != nil { + t.Fatalf("got error: %v, want: nil", err) } + log.Printf("subscribed: %s", id) } - */ - var sub struct { - HelloSaid struct { - ID String - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` } - subId1, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } + bulkSubscribe() - log.Println("result", string(data)) - e = json.Unmarshal(data, &sub) - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil + go func() { + if err := subscriptionClient.Run(); err != nil { + t.Errorf("got error: %v, want: nil", err) } + }() - if sub.HelloSaid.Message != String(msg) { - t.Fatalf("subscription message does not match. got: %s, want: %s", sub.HelloSaid.Message, msg) + time.Sleep(3 * time.Second) + if err := subscriptionClient.Close(); err != nil { + t.Fatalf("got error: %v, want: nil", err) + } + + bulkSubscribe() + + go func() { + length := subscriptionClient.getContext().GetSubscriptionsLength(nil) + if length != 2 { + t.Errorf("unexpected subscription client. got: %d, want: 2", length) + return } - return nil - }) + waitingLen := subscriptionClient.getContext().GetSubscriptionsLength([]SubscriptionStatus{SubscriptionWaiting}) + if waitingLen != 2 { + t.Errorf("unexpected waiting subscription client. got: %d, want: 2", waitingLen) + } + if err := subscriptionClient.Run(); err != nil { + t.Errorf("got error: %v, want: nil", err) + } + }() - if err != nil { + time.Sleep(3 * time.Second) + length := subscriptionClient.getContext().GetSubscriptionsLength(nil) + if length != 2 { + t.Fatalf("unexpected subscription client after restart. got: %d, want: 2, subscriptions: %+v", length, subscriptionClient.context.subscriptions) + } + if err := subscriptionClient.Close(); err != nil { t.Fatalf("got error: %v, want: nil", err) } +} + +func TestTransportWS_OnError(t *testing.T) { + stop := make(chan bool) + + subscriptionClient := NewSubscriptionClient(fmt.Sprintf("%s/v1/graphql", hasuraTestHost)). + WithTimeout(3 * time.Second). + WithProtocol(SubscriptionsTransportWS). + WithConnectionParams(map[string]interface{}{ + "headers": map[string]string{ + "x-hasura-admin-secret": "test", + }, + }).WithLog(log.Println) + + msg := randomID() + + subscriptionClient = subscriptionClient. + OnConnected(func() { + log.Println("client connected") + }). + OnError(func(sc *SubscriptionClient, err error) error { + log.Println("OnError: ", err) + return err + }) /* subscription { - helloSaid { + user { id - msg + name } } */ - var sub2 struct { - HelloSaid struct { - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` + var sub struct { + Users []struct { + ID int `graphql:"id"` + Name string `graphql:"name"` + } `graphql:"user(order_by: { id: desc }, limit: 5)"` } - subId2, err := subscriptionClient.Subscribe(sub2, nil, func(data []byte, e error) error { + _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { if e != nil { t.Fatalf("got error: %v, want: nil", e) return nil } log.Println("result", string(data)) - e = json.Unmarshal(data, &sub2) + e = json.Unmarshal(data, &sub) if e != nil { t.Fatalf("got error: %v, want: nil", e) return nil } - if sub2.HelloSaid.Message != String(msg) { - t.Fatalf("subscription message does not match. got: %s, want: %s", sub2.HelloSaid.Message, msg) + if len(sub.Users) > 0 && sub.Users[0].Name != msg { + t.Fatalf("subscription message does not match. got: %s, want: %s", sub.Users[0].Name, msg) } return nil @@ -369,42 +292,23 @@ func TestTransportWS_exitWhenNoSubscription(t *testing.T) { } go func() { - // wait until the subscription client connects to the server - time.Sleep(2 * time.Second) + unauthorizedErr := "invalid x-hasura-admin-secret/x-hasura-access-key" + err := subscriptionClient.Run() - // call a mutation request to send message to the subscription - /* - mutation ($msg: String!) { - sayHello(msg: $msg) { - id - msg - } - } - */ - var q struct { - SayHello struct { - ID String - Msg String - } `graphql:"sayHello(msg: $msg)"` - } - variables := map[string]interface{}{ - "msg": String(msg), - } - err = client.Mutate(context.Background(), &q, variables, OperationName("SayHello")) - if err != nil { - (*t).Fatalf("got error: %v, want: nil", err) + if err == nil || err.Error() != unauthorizedErr { + t.Errorf("got error: %v, want: %s", err, unauthorizedErr) } - - time.Sleep(2 * time.Second) - subscriptionClient.Unsubscribe(subId1) - subscriptionClient.Unsubscribe(subId2) + stop <- true }() defer subscriptionClient.Close() - if err := subscriptionClient.Run(); err != nil { - t.Fatalf("got error: %v, want: nil", err) + // wait until the subscription client connects to the server + if err := waitHasuraService(60); err != nil { + t.Fatalf("failed to start hasura service: %s", err) } + + <-stop } func TestTransportWS_ResetClient(t *testing.T) { @@ -535,40 +439,47 @@ func TestTransportWS_ResetClient(t *testing.T) { err = client.Mutate(context.Background(), &q, variables, OperationName("InsertUser")) if err != nil { - (*t).Fatalf("got error: %v, want: nil", err) + t.Errorf("got error: %v, want: nil", err) + return } time.Sleep(2 * time.Second) // test subscription ids - sub1 := subscriptionClient.getContext().GetSubscription(subId1) + sub1 := subscriptionClient.GetSubscription(subId1) if sub1 == nil { - (*t).Fatalf("subscription 1 not found: %s", subId1) + t.Errorf("subscription 1 not found: %s", subId1) + return } else { - if sub1.key != subId1 { - (*t).Fatalf("subscription key 1 not equal, got %s, want %s", subId1, sub1.key) + if sub1.GetKey() != subId1 { + t.Errorf("subscription key 1 not equal, got %s, want %s", subId1, sub1.GetKey()) + return } - if sub1.id != subId1 { - (*t).Fatalf("subscription id 1 not equal, got %s, want %s", subId1, sub1.id) + if sub1.GetID() != subId1 { + t.Errorf("subscription id 1 not equal, got %s, want %s", subId1, sub1.GetID()) + return } } - sub2 := subscriptionClient.getContext().GetSubscription(subId2) + sub2 := subscriptionClient.GetSubscription(subId2) if sub2 == nil { - (*t).Fatalf("subscription 2 not found: %s", subId2) + t.Errorf("subscription 2 not found: %s", subId2) + return } else { - if sub2.key != subId2 { - (*t).Fatalf("subscription id 2 not equal, got %s, want %s", subId2, sub2.key) + if sub2.GetKey() != subId2 { + t.Errorf("subscription id 2 not equal, got %s, want %s", subId2, sub2.GetKey()) + return } - if sub2.id != subId2 { - (*t).Fatalf("subscription id 2 not equal, got %s, want %s", subId2, sub2.id) + if sub2.GetID() != subId2 { + t.Errorf("subscription id 2 not equal, got %s, want %s", subId2, sub2.GetID()) + return } } // reset the subscription log.Printf("resetting the subscription client...") if err := subscriptionClient.Run(); err != nil { - (*t).Fatalf("failed to reset the subscription client. got error: %v, want: nil", err) + t.Errorf("failed to reset the subscription client. got error: %v, want: nil", err) } log.Printf("the second run was stopped") stop <- true @@ -578,32 +489,32 @@ func TestTransportWS_ResetClient(t *testing.T) { time.Sleep(8 * time.Second) // test subscription ids - sub1 := subscriptionClient.getContext().GetSubscription(subId1) + sub1 := subscriptionClient.GetSubscription(subId1) if sub1 == nil { - (*t).Fatalf("subscription 1 not found: %s", subId1) + t.Errorf("subscription 1 not found: %s", subId1) } else { - if sub1.key != subId1 { - (*t).Fatalf("subscription key 1 not equal, got %s, want %s", subId1, sub1.key) + if sub1.GetKey() != subId1 { + t.Errorf("subscription key 1 not equal, got %s, want %s", subId1, sub1.GetKey()) } - if sub1.id == subId1 { - (*t).Fatalf("subscription id 1 should equal, got %s, want %s", subId1, sub1.id) + if sub1.GetID() == subId1 { + t.Errorf("subscription id 1 should equal, got %s, want %s", subId1, sub1.GetID()) } } - sub2 := subscriptionClient.getContext().GetSubscription(subId2) + sub2 := subscriptionClient.GetSubscription(subId2) if sub2 == nil { - (*t).Fatalf("subscription 2 not found: %s", subId2) + t.Errorf("subscription 2 not found: %s", subId2) } else { - if sub2.key != subId2 { - (*t).Fatalf("subscription id 2 not equal, got %s, want %s", subId2, sub2.key) + if sub2.GetKey() != subId2 { + t.Errorf("subscription id 2 not equal, got %s, want %s", subId2, sub2.GetKey()) } - if sub2.id == subId2 { - (*t).Fatalf("subscription id 2 should equal, got %s, want %s", subId2, sub2.id) + if sub2.GetID() == subId2 { + t.Errorf("subscription id 2 should equal, got %s, want %s", subId2, sub2.GetID()) } } - subscriptionClient.Unsubscribe(subId1) - subscriptionClient.Unsubscribe(subId2) + _ = subscriptionClient.Unsubscribe(subId1) + _ = subscriptionClient.Unsubscribe(subId2) }() defer subscriptionClient.Close() @@ -614,157 +525,3 @@ func TestTransportWS_ResetClient(t *testing.T) { <-stop } - -func TestTransportWS_onDisconnected(t *testing.T) { - port := 8083 - server := subscription_setupServer(port) - var wasConnected bool - disconnected := make(chan bool) - go func() { - if err := server.ListenAndServe(); err != nil { - log.Println(err) - } - }() - - // init client - _, subscriptionClient := subscription_setupClients(port) - subscriptionClient = subscriptionClient. - WithTimeout(5 * time.Second). - OnError(func(sc *SubscriptionClient, err error) error { - panic(err) - }). - OnConnected(func() { - log.Println("OnConnected") - wasConnected = true - }). - OnDisconnected(func() { - log.Println("OnDisconnected") - disconnected <- true - }) - - /* - subscription { - helloSaid { - id - msg - } - } - */ - var sub struct { - HelloSaid struct { - ID String - Message String `graphql:"msg" json:"msg"` - } `graphql:"helloSaid" json:"helloSaid"` - } - - _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - } - return nil - }) - - if err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - - // run client - go func() { - subscriptionClient.Run() - }() - defer subscriptionClient.Close() - - // wait until the subscription client connects to the server - time.Sleep(2 * time.Second) - if err := server.Close(); err != nil { - panic(err) - } - - <-disconnected - - if !wasConnected { - t.Fatal("the OnConnected event must be triggered") - } -} - -func TestTransportWS_OnError(t *testing.T) { - stop := make(chan bool) - - subscriptionClient := NewSubscriptionClient(fmt.Sprintf("%s/v1/graphql", hasuraTestHost)). - WithTimeout(3 * time.Second). - WithProtocol(SubscriptionsTransportWS). - WithConnectionParams(map[string]interface{}{ - "headers": map[string]string{ - "x-hasura-admin-secret": "test", - }, - }).WithLog(log.Println) - - msg := randomID() - - subscriptionClient = subscriptionClient. - OnConnected(func() { - log.Println("client connected") - }). - OnError(func(sc *SubscriptionClient, err error) error { - log.Println("OnError: ", err) - return err - }) - - /* - subscription { - user { - id - name - } - } - */ - var sub struct { - Users []struct { - ID int `graphql:"id"` - Name string `graphql:"name"` - } `graphql:"user(order_by: { id: desc }, limit: 5)"` - } - - _, err := subscriptionClient.Subscribe(sub, nil, func(data []byte, e error) error { - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - log.Println("result", string(data)) - e = json.Unmarshal(data, &sub) - if e != nil { - t.Fatalf("got error: %v, want: nil", e) - return nil - } - - if len(sub.Users) > 0 && sub.Users[0].Name != msg { - t.Fatalf("subscription message does not match. got: %s, want: %s", sub.Users[0].Name, msg) - } - - return nil - }) - - if err != nil { - t.Fatalf("got error: %v, want: nil", err) - } - - go func() { - unauthorizedErr := "invalid x-hasura-admin-secret/x-hasura-access-key" - err := subscriptionClient.Run() - - if err == nil || err.Error() != unauthorizedErr { - (*t).Errorf("got error: %v, want: %s", err, unauthorizedErr) - } - stop <- true - }() - - defer subscriptionClient.Close() - - // wait until the subscription client connects to the server - if err := waitHasuraService(60); err != nil { - t.Fatalf("failed to start hasura service: %s", err) - } - - <-stop -}