Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

skyra-project/star-wars-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Star Wars API

GraphQL API similar to swapi.dev

GitHub codecov npm bundle size npm

Support Server

Table of Contents

Description

This API was inspired by swapi.dev, however we noticed that getting nested data to present on Discord was really slow because of the way that API worked. So we decided to take the data provided on the API, and make it into a GraphQL api.

Features

  • Fully generated client-side TypeScript typings published to
  • Docker images of the API for private hosting published to
  • Provides information about various assets in Star Wars
    • Films
    • People
    • Planets
    • Spaceships
    • Species
    • Vehicles

Installation

Note: This is only needed if you are writing TypeScript, or if you're using a GraphQL schema validator. If you're using neither of these, you do not need to install this package. The package does NOT include the actual API, ONLY type information.

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install -D @skyra/star-wars-api

API Documentation

For the full documentation of the deployed version please see the GraphQL Playground on the API.

Usage

These examples are written as based on TypeScript. For JavaScript simply change out the imports to require syntax and remove any type information.

Using Fetch

import type { Query } from '@skyra/star-wars-api';

interface StarWarsGraphqlApiResponse<K extends keyof Omit<Query, '__typename'>> {
	data: Record<K, Omit<Query[K], '__typename'>>;
}

fetch('https://swapi.skyra.pw/', {
	method: 'POST',
	headers: {
		'Content-Type': 'application/json'
	},
	body: JSON.stringify({
		query: `
      {
        getPerson(person: lukeskywalker) {
            name
            birthYear
            eyeColors
            gender
        }
      }
    `
	})
})
	.then((res) => res.json() as Promise<StarWarsGraphqlApiResponse<'getPerson'>>)
	.then((json) => console.log(json.data));

Using Apollo Boost

import type { Query, QueryGetPersonFuzzyArgs } from '@skyra/star-wars-api';
import ApolloClient from 'apollo-boost';
import fetch from 'cross-fetch';
import gql from 'graphql-tag';

type StarWarsGraphqlApiResponse<K extends keyof Omit<Query, '__typename'>> = Record<K, Omit<Query[K], '__typename'>>;

const getPersonFuzzy = gql`
	query getPerson($person: String!) {
		getFuzzyPerson(person: $person, take: 10) {
			name
			birthYear
			eyeColors
			gender
		}
	}
`;

const apolloClient = new ApolloClient({
	uri: 'https://swapi.skyra.pw/',
	fetch
});

const {
	data: { getPersonFuzzy: peopleData }
} = await apolloClient.query<StarWarsGraphqlApiResponse<'getPersonFuzzy'>, QueryGetPersonFuzzyArgs>({
	query: getPersonFuzzy,
	variables: { person: 'luke' }
});

console.log(peopleData);

Using Apollo Client React

// ApolloClient setup
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';

// Instantiate required constructor fields
const cache = new InMemoryCache();
const link = new HttpLink({
	uri: 'https://swapi.skyra.pw/'
});

export const client = new ApolloClient({
	// Provide required constructor fields
	cache: cache,
	link: link,

	// Provide some optional constructor fields
	name: 'graphql-star-wars-api-client',
	version: '1.0',
	queryDeduplication: false,
	defaultOptions: {
		watchQuery: {
			fetchPolicy: 'cache-and-network'
		}
	}
});
// Component
import React from 'react';
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';
import type { Query } from '@skyra/star-wars-api';
import { client } from './ApolloClient';

interface StarWarsGraphqlApiResponse<K extends keyof Omit<Query, '__typename'>> {
	data: Record<K, Omit<Query[K], '__typename'>>;
}

const GET_PERSON_DATA = gql`
	{
		getPerson(person: lukeskywalker) {
			name
			birthYear
			eyeColors
			gender
		}
	}
`;

export const StarWarsPerson: React.FC = () => {
	const { loading, error, data } = useQuery<StarWarsGraphqlApiResponse<'getPerson'>>(GET_PERSON_DATA, {
		client: client
	});

	if (loading) return 'Loading...';
	if (error) return `Error! ${error.message}`;

	return <div> Insert how you want to display the data here </div>;
};

Meta

License

Copyright Β© 2021, Skyra Project. Released under the MIT License.

Buy us some doughnuts

Skyra Project is open source and always will be, even if we don't get donations. That said, we know there are amazing people who may still want to donate just to show their appreciation. Thanks you very much in advance!

We accept donations through Patreon, BitCoin, Ethereum, and Litecoin. You can use the buttons below to donate through your method of choice.

Donate With QR Address
Patreon PatreonImage Click Here
PayPal PayPalImage Click Here
BitCoin BitcoinImage 3JNzCHMTFtxYFWBnVtDM9Tt34zFbKvdwco
Ethereum EthereumImage 0xcB5EDB76Bc9E389514F905D9680589004C00190c
Litecoin LitecoinImage MNVT1keYGMfGp7vWmcYjCS8ntU8LNvjnqM

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Jeroen Claassens

πŸ› πŸ’» 🎨 πŸ’‘ πŸ€” πŸš‡ 🚧 πŸ“¦ πŸ“† πŸ‘€ πŸ’¬ πŸ›‘οΈ ⚠️ πŸ““

dependabot[bot]

🚧

github-actions[bot]

🚧

thehairy

πŸ’» πŸ›

This project follows the all-contributors specification. Contributions of any kind welcome!