Skip to content

Commit

Permalink
fix(rn): getPort when daemon not started
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Sep 18, 2018
1 parent c59c6ba commit 62a8e46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
44 changes: 30 additions & 14 deletions client/react-native/common/relay/environment.js
Expand Up @@ -56,20 +56,37 @@ let getIP = () =>
}
})

const getPort = () =>
new Promise(resolve => {
let port = 0
const interval = setInterval(async () => {
try {
port = await CoreModule.getPort()

resolve(port)
clearInterval(interval)
} catch (error) {
console.warn(error)
}
}, 1000)
})

export const fetchQuery = async (operation, variables) => {
try {
const port = await CoreModule.getPort()
const response = await fetch(`http://${await getIP()}:${port}/query`, {
method: 'POST',
headers: {
// Add authentication and other headers here
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
})
const response = await fetch(
`http://${await getIP()}:${await getPort()}/query`,
{
method: 'POST',
headers: {
// Add authentication and other headers here
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
}
)
return await response.json()
} catch (err) {
console.error(err)
Expand All @@ -79,9 +96,8 @@ export const fetchQuery = async (operation, variables) => {
const setupSubscription = async (config, variables, cacheConfig, observer) => {
try {
const query = config.text
const port = await CoreModule.getPort()
const subscriptionClient = new SubscriptionClient(
`ws://${await getIP()}:${port}/query`,
`ws://${await getIP()}:${await getPort()}/query`,
{
reconnect: true,
}
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/gomobile/core.go
Expand Up @@ -65,7 +65,7 @@ func Start(datastorePath string) error {

func GetPort() (int, error) {
if port == 0 {
err := errors.New("port == 0")
err := errors.New("port is not defined: wait for daemon to start")
logger().Error(err.Error())
return 0, err
}
Expand Down

0 comments on commit 62a8e46

Please sign in to comment.