This guide will walk you through the process of connecting Graphweaver to the Xero API. An example of a Xero integration can be found on the Graphweaver Github.
Prerequisites
Before you begin, ensure that you have the following prerequisites:
- Node.js 18 or greater installed
pnpmversion 8 or greater installed
Step 1: Obtain Xero API Credentials
Before you start, you'll need to obtain API credentials from Xero. These include a client_id, client_secret, redirect_uri. Refer to the Xero developer documentation for details on obtaining these credentials.
Step 2: Project Initialisation
Create a new Graphweaver project by running the following command:
npx graphweaver@latest initFollow the prompts to set up your project. Provide a name when prompted, and select the REST backend for connecting to Xero.
Install the dependencies in your project and install xero-node:
cd your-project-name
pnpm i
pnpm i xero-nodeEnsure your Graphweaver instance starts with:
pnpm startStep 3: Configure Xero API Connection
Create a file named .env in your project directory and configure the connection details for the Xero API:
XERO_CLIENT_ID="[value from Xero]"
XERO_CLIENT_SECRET="[value from Xero]"
XERO_CLIENT_REDIRECT_URIS="http://localhost:9000/xero-auth-code"Step 4: Create Xero Entities
xero-node provides classes for the entities which we will extend for our GraphQL entities. For the Account entity, we’ll import Account as XeroAccount, and AccountType from xero-node
and define the following fields for our GraphQL schema. See the Decorators docs for more details.
Step 5: Create Data Provider
To access our Xero accounts we need to handle Xero Tenants to query the Xero API. Let’s create a helper function to handle each tenant.
Now we can create resolvers for our entities with the Xero API. Use the provided XeroBackendProvider and implement the Provider methods. Our find function gets all the accounts by our tenants, and passes sorting and filtering info into the xero.accountingApi.getAccounts method.
The definitions for all of the sorting, filtering, and offsetAndLimiting can be found in the Xero Graphweaver Example.
Step 6: Authenticate with Xero
Now we need a way to authenticate with Xero from the dashboard. The @exogee/graphweaver-xero includes an apollo plugin which redirects user’s who first land on the admin UI to Xero to login. The XeroAuthApolloPlugin is included when instantiating the Graphweaver instance.
import 'reflect-metadata';
import Graphweaver from '@exogee/graphweaver-server';
import './schema';
import { XeroAuthApolloPlugin } from '@exogee/graphweaver-xero';
const graphweaver = new Graphweaver({
apolloServerOptions: {
plugins: [XeroAuthApolloPlugin],
},
});
exports.handler = graphweaver.handler();Finally we need to receive the code from Xero from our XERO_CLIENT_REDIRECT_URIS . We can create a component that picks up the code from the URL like this:
And we place this in our router at the route that matches our XERO_CLIENT_REDIRECT_URIS in this case /xero-auth-code.
Conclusion
Congratulations! You've successfully connected Graphweaver to the Xero API. You can now explore and interact with Xero data using GraphQL queries.
Feel free to customize this guide based on the specifics of your Xero API and Graphweaver project configuration. For more information on connecting multiple data sources, refer to the Graphweaver Documentation.