Skip to content

Instantly share code, notes, and snippets.

@Furkan-Gulsen
Created May 23, 2022 00:08
Show Gist options
  • Save Furkan-Gulsen/c7130392b39c83c02a5b10f43f9d3ab3 to your computer and use it in GitHub Desktop.
Save Furkan-Gulsen/c7130392b39c83c02a5b10f43f9d3ab3 to your computer and use it in GitHub Desktop.
Patika GraphQL - Odev - 2
const { ApolloServer, gql } = require("apollo-server");
const { events, locations, participants, users } = require("./data");
const { nanoid } = require("nanoid");
const {
ApolloServerPluginLandingPageGraphQLPlayground,
} = require("apollo-server-core");
const typeDefs = gql`
type event {
id: ID!
title: String!
desc: String!
date: String!
from: String!
to: String!
location_id: ID!
user_id: ID!
participants: [participant!]!
}
type location {
id: ID!
name: String!
desc: String!
lat: Float!
lng: Float!
event: event
}
# User
type user {
id: ID!
username: String!
email: String!
participants: [participant!]!
event: event
}
input CreateUserInput {
username: String!
email: String!
}
input UpdateUserInput {
username: String
email: String
}
type participant {
id: ID!
user_id: ID!
event_id: ID!
}
input CreateEventInput {
title: String!
desc: String!
date: String!
from: String!
to: String!
location_id: ID!
user_id: ID!
}
input UpdateEventInput {
title: String
desc: String
date: String
from: String
to: String
location_id: ID
user_id: ID
}
input CreateLocationInput {
name: String!
desc: String!
lat: Float!
lng: Float!
}
input UpdateLocationInput {
name: String
desc: String
lat: Float
lng: Float
}
input CreateParticipantInput {
user_id: ID!
event_id: ID!
}
input UpdateParticipantInput {
user_id: ID
event_id: ID
}
type Query {
users: [user!]!
user(id: ID!): user
events: [event!]!
event(id: ID!): event
locations: [location!]!
location(id: ID!): location
participants: [participant!]!
participant(id: ID!): participant
}
type Mutation {
# User Mutations
createUser(data: CreateUserInput!): user!
updateUser(id: ID!, data: UpdateUserInput!): user!
deleteUser(id: ID!): user!
deleteAllUsers: Boolean!
# Event Mutations
createEvent(data: CreateEventInput!): event!
updateEvent(id: ID!, data: UpdateEventInput!): event!
deleteEvent(id: ID!): event!
deleteAllEvents: Boolean!
# Event Mutations
createLocation(data: CreateLocationInput!): location!
updateLocation(id: ID!, data: UpdateLocationInput!): location!
deleteLocation(id: ID!): location!
deleteAllLocations: Boolean!
# Participant Mutations
createParticipant(data: CreateParticipantInput!): participant!
updateParticipant(id: ID!, data: UpdateParticipantInput!): participant!
deleteParticipant(id: ID!): participant!
deleteAllParticipants: Boolean!
}
`;
const resolvers = {
Query: {
users: () => users,
user: (parent, args) => users.find((user) => user.id == args.id),
events: () => events,
event: (parent, args) => events.find((event) => event.id == args.id),
locations: () => locations,
location: (parent, args) =>
locations.find((location) => location.id == args.id),
participants: () => participants,
participant: (parent, args) =>
participants.find((participant) => participant.id == args.id),
},
Mutation: {
// user mutations
createUser: (parent, { data }) => {
const newUser = {
id: nanoid(),
...data,
};
users.push(newUser);
return newUser;
},
updateUser: (parent, { id, data }) => {
const user_index = users.findIndex((user) => user.id == id);
if (user_index == -1) {
throw new Error("User is not found");
}
users[user_index] = {
...users[user_index],
...data,
};
return users[user_index];
},
deleteUser: (parent, { id }) => {
const user_index = users.findIndex((user) => user.id == id);
if (user_index == -1) {
throw new Error("User is not found");
}
const user = users[user_index];
users.splice(user_index, 1);
return user;
},
deleteAllUsers: () => {
try {
users.splice(0, users.length);
return true;
} catch (error) {
console.error(error);
return false;
}
},
// event mutations
createEvent: (parent, { data }) => {
const newEvent = {
id: nanoid(),
...data,
};
events.push(newEvent);
return newEvent;
},
updateEvent: (parent, { id, data }) => {
const event_index = events.findIndex((event) => event.id == id);
if (event_index == -1) {
throw new Error("Event is not found");
}
events[event_index] = {
...events[event_index],
...data,
};
return events[event_index];
},
deleteEvent: (parent, { id }) => {
const event_index = events.findIndex((event) => event.id == id);
if (event_index == -1) {
throw new Error("Event is not found");
}
const event = events[event_index];
events.splice(event_index, 1);
return event;
},
deleteAllEvents: () => {
try {
events.splice(0, events.length);
return true;
} catch (error) {
console.error(error);
return false;
}
},
// location mutations
createLocation: (parent, { data }) => {
const newLocation = {
id: nanoid(),
...data,
};
locations.push(newLocation);
return newLocation;
},
updateLocation: (parent, { id, data }) => {
const location_index = locations.findIndex(
(location) => location.id == id
);
if (location_index == -1) {
throw new Error("Location is not found");
}
locations[location_index] = {
...locations[location_index],
...data,
};
return locations[location_index];
},
deleteLocation: (parent, { id }) => {
const location_index = locations.findIndex(
(location) => location.id == id
);
if (location_index == -1) {
throw new Error("Location is not found");
}
const location = locations[location_index];
locations.splice(location_index, 1);
return location;
},
deleteAllLocations: () => {
try {
locations.splice(0, locations.length);
return true;
} catch (error) {
console.error(error);
return false;
}
},
// participant mutations
createParticipant: (parent, { data }) => {
const newParticipant = {
id: nanoid(),
...data,
};
participants.push(newParticipant);
return newParticipant;
},
updateParticipant: (parent, { id, data }) => {
const participant_index = participants.findIndex(
(participant) => participant.id == id
);
if (location_index == -1) {
throw new Error("Participant is not found");
}
participants[participant_index] = {
...participants[participant_index],
...data,
};
return participants[participant_index];
},
deleteParticipant: (parent, { id }) => {
const participant_index = participants.findIndex(
(participant) => participant.id == id
);
if (location_index == -1) {
throw new Error("Participant is not found");
}
const participant = participants[participant_index];
participants.splice(participant_index, 1);
return participant;
},
deleteAllParticipants: () => {
try {
participants.splice(0, participants.length);
return true;
} catch (error) {
console.error(error);
return false;
}
},
},
event: {
participants: (parent, args) =>
participants.filter((participant) => participant.user_id === parent.id),
},
user: {
participants: (parent, args) =>
participants.filter((participant) => participant.user_id === parent.id),
event: (parent, args) => events.find((event) => event.id === parent.id),
},
location: {
event: (parent, args) => events.find((event) => event.id === parent.id),
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment