Alpaca Travel GraphQL SDK for graphql-request
This package assist with a basic SDK for developing applications on top of
Alpaca Travel GraphQL API via graphql-request.
Installation
Via NPM:
npm install graphql-request graphql graphql-tag @alpaca-travel/graph-sdk-graphql-request --save
Via Yarn:
yarn add graphql-request graphql graphql-tag @alpaca-travel/graph-sdk-graphql-request
Typescript Types
The SDK includes typescript types to assist you in working with data responses.
Getting Started
The below shows a way to create a SDK instance with a configured graphql-request
client.
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '@alpaca-travel/graph-sdk-graphql-request';
const client = new GraphQLClient(`https://withalpaca.com/api/graphql?accessToken=xxx`);
const sdk = getSdk(client);
Example call
const data = await sdk.getItinerary({ id: 'itinerary/123' });
console.log(data.itinerary.locations.totalCount);
API Documentation
See API Docs
Extending Functionality
The SDK is driven by a number of GraphQL documents. You can
fork this project if you wish to extend your own queries and mutations as
documented on the GraphQL Documentation
and consider self-hosting for your use case or submitting a pull-request.
GraphQL Calls via graphql-request
You can craft your own GraphQL queries following the documentation available
for Alpaca GraphQL.
Using graphql-request, you can make your requests as needed;
const query = gql`
query getAuthorizedProfiles {
query
authorizedProfiles(first: 1) {
nodes {
id
name
}
}
}
`;
client.request(query).then((data) => console.log(data));
Using graphql-codegen
You can leverage the tool graphql-codegen
in order to generate additional
hooks and capability for your application.
Install graphql-codegen and the related libraries to your project
npm install -D @graphql-codegen/cli @graphql-codegen/fragment-matcher @graphql-codegen/introspection @graphql-codegen/jsdoc @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-graphql-request
See the graphql-codegen configuration for an example of
the configuration used for generating out the SDK.
- Copy the
codegen-typescript-example.yml
file as codegen.yml
in your workspace - Create the folder in
src/graphql
and place in your graphql operations - Add the script
"graphql-codegen": "graphql-codegen --config codegen.yml"
to your package.json "scripts" section - Run
npm run graphql-codegen
to generate your own src/graphql/index.ts
file
The benefit of using graphql-codegen is that your Typescript types will be
created, as well as providing the API surface for you to call without embedding
your GraphQL queries/mutations within the component.
Further Reading