Skip to content

What is GraphQL?

GraphQL is a strongly typed API query language. It allows clients to define the structure of data required and the server will respond with data in the same structure. This helps avoid the problems of both over and under-fetching data, while also allowing for a more powerful and flexible API.

Prerequisite Knowledge

Before continuing, it is highly recommended that you familiarize yourself with GraphQL. The official GraphQL documentation is a good place to start.

Making a GraphQL API request

Nexoya requires all GraphQL requests be made as POST requests to https://gw.nexoya.io/api/graphql.

When making a request, you will need to include 2 objects in your payload: query and variables.

  • query: This is your query or mutation string. It defines the data and shape of data that you would like.
  • variables: Contains the values for the variables used within your query.

The variables object is optional if you are not using any, however, it is recommended that you use variables wherever it makes sense.

Example Query

Let's create a simple query to get a list of portfolios

Apollo Studio

js
// Here we define our query as a multi-line string
// Storing it in a separate .graphql/.gql file is also possible
var query = `
query Portfolios($teamId: Int!) {
  portfoliosV2(teamId: $teamId) {
    teamId
    portfolioId
    title
    start
    end
  }
}
`;

// Define our query variables and values that will be used in the query request
var variables = {
  team_id: 1142,
};

// Implement this function to get the access token
const accessToken = getAccessToken();

// Define the config we'll need for our Api request
var url = 'https://gw.nexoya.io/api/graphql',
  options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: 'Bearer ' + accessToken,
    },
    body: JSON.stringify({
      query: query,
      variables: variables,
    }),
  };

// Make the HTTP Api request
fetch(url, options).then(handleResponse).then(handleData).catch(handleError);

function handleResponse(response) {
  return response.json().then(function (json) {
    return response.ok ? json : Promise.reject(json);
  });
}

function handleData(data) {
  console.log(JSON.stringify(data, null, 2));
}

function handleError(error) {
  console.error(error);
}

The request in the above example will return the following JSON response:

json
{
    "data": {
        "portfoliosV2": [
            {
                "teamId": 1,
                "portfolioId": 1,
                "title": "Portfolio 1",
                "start": "2024-12-31",
                "end": "2025-07-31"
            },
            {
                "teamId": 1,
                "portfolioId": 2,
                "title": "Portfolio 2",
                "start": "2024-12-31",
                "end": "2025-07-31"
            }
            ...
        ]
    }
}

Tooling

GraphQL has been used in major projects throughout the industry for over a decade, including Airbnb, Intuit, Microsoft, and more. It is battle tested and has many powerful tools for you to use.

These are some recommended tools for working with Nexoya and GraphQL:

  • Apollo Studio

    Apollo Studio is a powerful tool for exploring and testing your GraphQL queries. It is a great way to get started with GraphQL and Nexoya.

  • GraphQL Codegen

    Skip service-specific API clients and generate always up-to-date typings for your language of choice.