Skip to content

Instantly share code, notes, and snippets.

@Slashgear
Created December 14, 2020 14:35
Show Gist options
  • Select an option

  • Save Slashgear/690b53eac172e0e9716b00a7faef1a65 to your computer and use it in GitHub Desktop.

Select an option

Save Slashgear/690b53eac172e0e9716b00a7faef1a65 to your computer and use it in GitHub Desktop.
const { ApolloServer, gql } = require("apollo-server");
const { RESTDataSource } = require("apollo-datasource-rest");
const typeDefs = gql`
type Book {
url: String
name: String
isbn: String
authors: [String]
numberOfPages: Int
publiser: String
country: String
mediaType: String
released: String
characters: [Character]
povCharacters: [String]
}
type Character {
url: String
name: String
gender: String
culture: String
born: String
died: String
titles: [String]
aliases: [String]
father: Character
mother: Character
spouse: Character
allegiances: [House]
books: [Book]
povBooks: [Book]
tvSeries: [String]
playedBy: [String]
}
type House {
url: String
name: String
region: String
coatOfArms: String
words: String
titles: [String]
seats: [String]
currentLord: Character
heir: Character
overlord: House
founded: String
founder: Character
diedOut: String
ancestralWeapons: [String]
cadetBranches: [House]
swornMembers: [Character]
}
type Query {
books: [Book]
book(id: Int!): Book
character(id: Int!): Character
house(id: Int!): House
}
`;
class IceAndFireApi extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://anapioficeandfire.com/api/";
}
async getBook(id) {
return this.get(`books/${id}`);
}
async getBooks() {
return this.get("books/");
}
async getCharacter(id) {
return this.get(`characters/${id}`);
}
}
const resolvers = {
Query: {
books: (parent, args, context) => context.dataSources.iceAndFire.getBooks(),
book: (parent, { id }, context) =>
context.dataSources.iceAndFire.getBook(id),
character: (parent, { id }, context) => {
return context.dataSources.iceAndFire.getCharacter(id);
}
},
Book: {
characters: (parent, args, context) => {
return parent.characters
.map(url => url.split("characters/")[1])
.map(id => context.dataSources.iceAndFire.getCharacter(id));
}
},
Character: {
books: (parent, args, context) => {
return parent.books
.map(url => url.split("books/")[1])
.map(id => context.dataSources.iceAndFire.getBook(id));
},
father: (parent, args, context) => {
if (parent.father) {
return context.dataSources.iceAndFire.getCharacter(
parent.father.split("characters/")[1]
);
}
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
tracing: true,
dataSources: () => {
return {
iceAndFire: new IceAndFireApi()
};
}
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
{
"name": "got-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"apollo-datasource-rest": "^0.9.5",
"apollo-server": "^2.19.0",
"graphql": "^15.4.0",
"nodemon": "^2.0.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment