How to exploit the return of a resolver?

Hello,

My question is maybe basic but I doesn’t how to get the return from my resolver although I have the hello word code example for confluence macro.

my resolvers.js is

import Resolver from '@forge/resolver';
import api, { route } from "@forge/api";

// XRAY API base URL
const XRAY_API_BASE = 'https://xray.cloud.getxray.app/api/v2/graphql';
const resolver = new Resolver();


resolver.define('getToken', async () => {
  console.log("Making XRAY GraphQL API to get token...");
  const response = await api.fetch(
    `${XRAY_API_BASE}/authenticate`, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
    }
  );

console.log("Response", await response.json())
//return await response.json()
return 'world'
});


export const handler = resolver.getDefinitions();

and my index.jsx

import React, { useEffect, useState } from 'react';
import ForgeReconciler, { Text } from '@forge/react';
import { invoke } from '@forge/bridge';
const result = null;

const App = () => {


useEffect(() => {
  invoke('getToken').then(result);
}, []);

  console.log("Log --- ", result);
    return <Text>Hello {result}</Text> 

};

ForgeReconciler.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

My goal is to have

return Hello world

result variable is the result of getToken. and getToken return world

Do you have advice to catch error on API call ?

Hello @ThibautFAURE,

I think this area needs to be updated to something like:

const App = () => {

  const [result, setResult] = useState(null);

  useEffect(() => {
    // "then" accepts a function to run once the promise is fulfilled
    // the "then" will now call the function "setResult" which sets the value of "result"
    invoke('getToken').then(setResult);
  }, []);

  console.log("Log --- ", result);
  return <Text>Hello {result}</Text> 
};

Kindly try it out and let us know how it goes.

Cheers,
Ian

3 Likes

Thank you very much it’s works

1 Like

Happy to help and glad to know you got it working, @ThibautFAURE :slight_smile:

1 Like