Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql wrapper blog #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"bracketSpacing": true,
"endOfLine": "auto"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Starwars API Wrapper: Basics

## How to use this repo

There is 1 main folder:

- `server`: The starting point of our GraphQL server.

To get started:

1. Navigate to the `server` folder.
1. Run `yarn install`.
1. Run `yarn start`.

This will start the GraphQL API server.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "starwars-api-server",
"version": "1.0.0",
"description": "back-end demo app for Starwars API",
"main": "src/index.js",
"scripts": {
"start": "nodemon src/index",
"format": "prettier --write ."
},
"dependencies": {
"apollo-server": "^3.11.1",
"axios": "^1.1.3",
"graphql": "^16.6.0",
"prettier": "^2.8.0"
},
"devDependencies": {
"dotenv": "^8.6.0",
"nodemon": "^2.0.20"
},
"author": "William Hutt",
"license": "MIT",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema/typedefs');
const resolvers = require('./schema/resolvers');
const StarwarsAPI = require('./rest-api-sources/starwars-rest-api');

const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({}),
context: () => {
return {
starwarsAPI: new StarwarsAPI(),
};
},
});

server.listen().then(() => {
console.log(`
🚀 Server is running!
🔉 Listening on port 4000
📭 Query at http://localhost:4000
`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const axios = require('axios');

class StarwarsAPI {
constructor() {
this.axios = axios.create({
baseURL: 'https://swapi.dev/api/',
});
}

async getPerson(id) {
const { data } = await this.axios.get(`people/${id}`);
return data;
}

async getHomeworld(id) {
const { data } = await this.axios.get(`planets/${id}`);
return data;
}
}

module.exports = StarwarsAPI;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const nameFormatter = require('../utils/name');

const resolvers = {
Person: {
homeworld: async (id, _, { starwarsAPI }) => {
const homeworldId = new URL(id?.homeworld).pathname.replace(
'/api/planets/',
'',
);
return await starwarsAPI.getHomeworld(homeworldId);
},
name: ({ name }) => {
if (!name) {
return null;
}
return nameFormatter(name);
},
},
Query: {
person: async (_, { id }, { starwarsAPI }) => {
return await starwarsAPI.getPerson(id);
},
},
};

module.exports = resolvers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const { gql } = require('apollo-server');

const typeDefs = gql`
type Query {
person(id: ID, personID: ID): Person
}

"""
An individual person or character within the Star Wars universe.
"""
type Person {
"""
The name of this person.
"""
name: String

"""
The birth year of the person, using the in-universe standard of BBY or ABY -
Before the Battle of Yavin or After the Battle of Yavin. The Battle of Yavin is
a battle that occurs at the end of Star Wars episode IV: A New Hope.
"""
birthYear: String

"""
The eye color of this person. Will be "unknown" if not known or "n/a" if the
person does not have an eye.
"""
eyeColor: String

"""
The gender of this person. Either "Male", "Female" or "unknown",
"n/a" if the person does not have a gender.
"""
gender: String

"""
The hair color of this person. Will be "unknown" if not known or "n/a" if the
person does not have hair.
"""
hairColor: String

"""
The height of the person in centimeters.
"""
height: Int

"""
The mass of the person in kilograms.
"""
mass: Float

"""
The skin color of this person.
"""
skinColor: String

"""
A planet that this person was born on or inhabits.
"""
homeworld: Planet

"""
The ISO 8601 date format of the time that this resource was created.
"""
created: String

"""
The ISO 8601 date format of the time that this resource was edited.
"""
edited: String
}

"""
A large mass, planet or planetoid in the Star Wars Universe, at the time of
0 ABY.
"""
type Planet {
"""
The name of this planet.
"""
name: String

"""
The diameter of this planet in kilometers.
"""
diameter: Int

"""
The number of standard hours it takes for this planet to complete a single
rotation on its axis.
"""
rotationPeriod: Int

"""
The number of standard days it takes for this planet to complete a single orbit
of its local star.
"""
orbitalPeriod: Int

"""
A number denoting the gravity of this planet, where "1" is normal or 1 standard
G. "2" is twice or 2 standard Gs. "0.5" is half or 0.5 standard Gs.
"""
gravity: String

"""
The average population of sentient beings inhabiting this planet.
"""
population: Float

"""
The climates of this planet.
"""
climates: [String]

"""
The terrains of this planet.
"""
terrains: [String]

"""
The percentage of the planet surface that is naturally occurring water or bodies
of water.
"""
surfaceWater: Float

"""
The ISO 8601 date format of the time that this resource was created.
"""
created: String

"""
The ISO 8601 date format of the time that this resource was edited.
"""
edited: String
}
`;

module.exports = typeDefs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const nameFormatter = (name) => {
const [first, last] = name.split(' ');

if (last === undefined) {
return first;
}
return `${first} ${last[0].toUpperCase()}.`;
};

module.exports = nameFormatter;
Loading