Was this helpful?

Making GraphQL Calls using JavaScript

GraphQL is easy to call in a number of technologies because underlying it is simply a HTTP call encapsulating your query.

GraphQL is sent as JSON across HTTP. This makes it possible for many different languages and frameworks capable of being able to execute GraphQL calls to the Alpaca Travel GraphQL API.

A number of languages and frameworks already offer tools to connect and make calling GraphQL easy. See this Awesome GraphQL list to see alternatives.

Considering choices

For basic requirements, consider using:

For comprehensive requirements, consider using:

Requirements for configuring clients

For all the clients, you will need to configure your GraphQL Root Endpoint.

GraphQL will send queries and mutations to a single end-point. As such, the only configuration that is required in order to send and receive will be the root end-point.


Getting Started using Fetch

The Fetch API provides a JavaScript interface to making requests and reading responses.

Fetch is widely supported on modern browsers and provides a replacement for the older XMLHttpRequest style requests.

You can develop your query using the GraphQL Playground and then update your query below.

1// Using FETCH
2// Update the accessToken with your API Key
3fetch("https://graphql.withalpaca.travel?accessToken=xxx", {
4  method: "POST",
5  headers: {
6    "Content-Type": "application/json",
7    Accept: "application/json",
8  },
9  // Body takes a query string within the a json object
10  body: JSON.stringify({ query: `query { node(id: "itinerary/123") { id } }` }),
12  .then((r) => r.json())
13  .then((data) => console.log("data returned:", data));

Using Apollo React Client

You can view the topic React + Apollo Client which will provide you information about how to configure your react application with Apollo.


Copyright © 2023 - Made with love ❤️ in Australia.