Skip to content

Instantly share code, notes, and snippets.

@Furkan-Gulsen
Last active May 20, 2022 22:06
Show Gist options
  • Save Furkan-Gulsen/e3e5a3c0b1623bc585c9d3bf48923e16 to your computer and use it in GitHub Desktop.
Save Furkan-Gulsen/e3e5a3c0b1623bc585c9d3bf48923e16 to your computer and use it in GitHub Desktop.
graphql-patika-odev-1
const { ApolloServer, gql } = require("apollo-server");
const { events, locations, participants, users } = require("./data");
const typeDefs = gql`
type event {
id: ID!
title: String!
desc: String!
date: String!
from: String!
to: String!
location_id: Int!
user_id: Int!
participants: [participant!]!
}
type location {
id: ID!
name: String!
desc: String!
lat: Float!
lng: Float!
event: event
}
type user {
id: ID!
username: String!
email: String!
participants: [participant!]!
event: event
}
type participant {
id: ID!
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
}
`;
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),
},
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