Created
November 27, 2018 07:52
-
-
Save programaths/a1de2e794a0be9a1010eb32eb7d8857a to your computer and use it in GitHub Desktop.
Working case of connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const {ApolloClient,gql} = require('apollo-boost'); | |
const {createHttpLink} =require('apollo-link-http'); | |
const {WebSocketLink} =require('apollo-link-ws'); | |
const WebSocket = require('ws'); | |
const {InMemoryCache} = require('apollo-cache-inmemory'); | |
const TEST_EMAIL = 'test-email'; | |
const TEST_PWD = 'test-password'; | |
async function queryRefreshToken(provider){ | |
return fetch('http://localhost:9080/auth', { | |
method: 'POST', | |
body: JSON.stringify({ | |
app_id: '', | |
provider: provider, | |
data: TEST_EMAIL, | |
user_info: { | |
register: false, | |
email: TEST_EMAIL, | |
password: TEST_PWD | |
} | |
}), | |
headers: {'content-type': 'application/json'} | |
}) | |
} | |
async function getRefreshToken() { | |
const respPwd = await queryRefreshToken('password'); | |
if (respPwd.ok) { | |
console.log(`Authenticated through "password" provider`); | |
const result = await respPwd.json(); | |
return result.refresh_token.token; | |
} | |
throw "could not login"; | |
} | |
const respUser = await fetch("http://localhost:9080/auth", { | |
method: 'POST', | |
body: JSON.stringify({ | |
app_id: '', | |
provider: 'realm', | |
data: token, | |
path: "/user/1/__partial/1/ok" // Reamove "ok" and it fails | |
}), | |
headers: {'content-type': 'application/json'} | |
}); | |
let resultUser = await respUser.json(); | |
let clientUser = new ApolloClient({ | |
cache: new InMemoryCache(), | |
link: new WebSocketLink({ | |
uri:'ws://localhost:9080/graphql/user/1/__partial/1/ok', // Reamove "ok" and it fails | |
options:{ | |
reconnect:true, | |
connectionParams:{ | |
token: resultUser.access_token.token | |
} | |
}, | |
webSocketImpl: WebSocket | |
}) | |
}); | |
let teamQueryResult = await clientUser.query({ | |
query: gql` | |
query { | |
test{ | |
id | |
} | |
} | |
` | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment