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

Stuck? Compare your code with the final state! #170

Open
wants to merge 11 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
9 changes: 6 additions & 3 deletions monolith/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { ApolloServer } = require("@apollo/server");
const { startStandaloneServer } = require("@apollo/server/standalone");
const { buildSubgraphSchema } = require("@apollo/subgraph");

const { readFileSync } = require("fs");
const axios = require("axios");
Expand All @@ -18,11 +19,13 @@ const PaymentsAPI = require("./datasources/payments");

async function startApolloServer() {
const server = new ApolloServer({
typeDefs,
resolvers,
schema: buildSubgraphSchema({
typeDefs,
resolvers,
}),
});

const port = 4000;
const port = 4001;

try {
const { url } = await startStandaloneServer(server, {
Expand Down
97 changes: 87 additions & 10 deletions monolith/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions monolith/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@apollo/datasource-rest": "^4.3.2",
"@apollo/server": "^4.0.1",
"@apollo/subgraph": "^2.8.0",
"axios": "^1.6.2",
"concurrently": "^6.3.0",
"date-fns": "^2.25.0",
Expand Down
18 changes: 12 additions & 6 deletions monolith/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ const resolvers = {
},
},
Listing: {
host: ({ hostId }, _, { dataSources }) => {
return dataSources.accountsAPI.getUser(hostId);
host: ({ hostId }) => {
return { id: hostId };
},
overallRating: ({ id }, _, { dataSources }) => {
return dataSources.reviewsDb.getOverallRatingForListing(id);
Expand Down Expand Up @@ -425,8 +425,8 @@ const resolvers = {
checkOutDate: ({ checkOutDate }, _, { dataSources }) => {
return dataSources.bookingsDb.getHumanReadableDate(checkOutDate);
},
guest: ({ guestId }, _, { dataSources }) => {
return dataSources.accountsAPI.getUser(guestId);
guest: ({ guestId }) => {
return { id: guestId };
},
totalPrice: async (
{ listingId, checkInDate, checkOutDate },
Expand All @@ -451,8 +451,14 @@ const resolvers = {
},
},
Review: {
author: ({ authorId }, _, { dataSources }) => {
return dataSources.accountsAPI.getUser(authorId);
author: (review) => {
let role = "";
if (review.targetType === "LISTING" || review.targetType === "HOST") {
role = "Guest";
} else {
role = "Host";
}
return { __typename: role, id: review.authorId, role };
},
},
AmenityCategory: {
Expand Down
23 changes: 16 additions & 7 deletions monolith/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])

type Query {
### User accounts
user(id: ID!): User
Expand Down Expand Up @@ -38,7 +41,10 @@ type Mutation {
"Creates a new listing for the currently authenticated host"
createListing(listing: CreateListingInput!): CreateListingResponse!
"Updates an existing listing"
updateListing(listingId: ID!, listing: UpdateListingInput!): UpdateListingResponse!
updateListing(
listingId: ID!
listing: UpdateListingInput!
): UpdateListingResponse!

### Bookings
createBooking(createBookingInput: CreateBookingInput): CreateBookingResponse!
Expand All @@ -51,7 +57,10 @@ type Mutation {
locationReview: ReviewInput!
): SubmitHostAndLocationReviewsResponse!
"Creates a review for the guest - must be authored by host of past booking"
submitGuestReview(bookingId: ID!, guestReview: ReviewInput!): SubmitGuestReviewResponse!
submitGuestReview(
bookingId: ID!
guestReview: ReviewInput!
): SubmitGuestReviewResponse!

### Wallet
addFundsToWallet(amount: Float!): AddFundsToWalletResponse!
Expand All @@ -78,7 +87,7 @@ interface User {
}

"A host is a type of Airlock user. They own listings."
type Host implements User {
type Host implements User @key(fields: "id") {
id: ID!
"The user's first and last name"
name: String!
Expand All @@ -91,7 +100,7 @@ type Host implements User {
}

"A guest is a type of Airlock user. They book places to stay."
type Guest implements User {
type Guest implements User @key(fields: "id") {
id: ID!
"The user's first and last name"
name: String!
Expand Down Expand Up @@ -126,7 +135,7 @@ type UpdateProfileResponse implements MutationResponse {
### Listings

"A listing is a location owned by a host. A listing has a list of amenities it offers. Listings have a fixed cost per night value."
type Listing {
type Listing @key(fields: "id") {
id: ID!
"The listing's title"
title: String!
Expand Down Expand Up @@ -273,7 +282,7 @@ type UpdateListingResponse implements MutationResponse {
### Bookings

"A booking is a reservation for a specific listing"
type Booking {
type Booking @key(fields: "id") {
id: ID!
"The listing associated with the reservation"
listing: Listing!
Expand Down Expand Up @@ -334,7 +343,7 @@ type CreateBookingResponse implements MutationResponse {
### Reviews

"A review consists of a numerical rating and written text. It can be written by a host or a guest."
type Review {
type Review @key(fields: "id") {
id: ID!
"Written comment the author has written about the review target"
text: String!
Expand Down
11 changes: 10 additions & 1 deletion router/router-config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
include_subgraph_errors:
all: true # Propagate errors from all subgraphs
all: true # Propagate errors from all subgraphs
headers:
all:
request:
- propagate:
named: "Authorization"
cors:
origins:
- http://localhost:3000 # Allows any locally-running client to run against your Router
- https://studio.apollographql.com # Allows Apollo Studio to still run queries against your Router
19 changes: 19 additions & 0 deletions subgraph-accounts/datasources/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { RESTDataSource } = require("@apollo/datasource-rest");

class AccountsAPI extends RESTDataSource {
baseURL = "http://localhost:4011/";

login(username) {
return this.get(`login/${username}`);
}

updateUser({ userId, userInfo }) {
return this.patch(`user/${userId}`, { body: { ...userInfo } });
}

getUser(userId) {
return this.get(`user/${userId}`);
}
}

module.exports = AccountsAPI;
1 change: 1 addition & 0 deletions subgraph-accounts/datasources/datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: rename file and add datasource code here
Loading