-
-
Save eveporcello/28c0b8f99fd01b1a3d9bd9c86ba81379 to your computer and use it in GitHub Desktop.
Interfaces Lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
ApolloServer, | |
gql, | |
} = require("apollo-server"); | |
const employees = require("./data/employees"); | |
const typeDefs = gql` | |
interface Employee { | |
id: ID! | |
firstName: String! | |
lastName: String! | |
job: JobType | |
} | |
type LiftOperator implements Employee { | |
id: ID! | |
firstName: String! | |
lastName: String! | |
job: JobType | |
yearsExperience: Int | |
} | |
type SkiPatrol implements Employee { | |
id: ID! | |
firstName: String! | |
lastName: String! | |
job: JobType | |
certified: Boolean! | |
} | |
type Instructor implements Employee { | |
id: ID! | |
firstName: String! | |
lastName: String! | |
job: JobType | |
level: Level | |
} | |
type Bartender implements Employee { | |
id: ID! | |
firstName: String! | |
lastName: String! | |
job: JobType | |
assignment: Location | |
} | |
enum JobType { | |
LIFTOPERATOR | |
SKIPATROL | |
INSTRUCTOR | |
BARTENDER | |
} | |
enum Level { | |
LEVELONE | |
LEVELTWO | |
LEVELTHREE | |
} | |
enum Location { | |
SUMMIT | |
ICEBAR | |
EDGEDECK | |
} | |
type Query { | |
allEmployees(job: JobType): [Employee!]! | |
totalEmployees: Int! | |
allSkiPatrol: [SkiPatrol!]! | |
} | |
`; | |
const resolvers = { | |
Query: { | |
allSkiPatrol: ( | |
parent, | |
args, | |
{ employees } | |
) => { | |
return employees.filter( | |
(e) => e.job === "SKIPATROL" | |
); | |
}, | |
allEmployees: ( | |
parent, | |
{ job }, | |
{ employees } | |
) => { | |
if (job) { | |
return employees.filter( | |
(e) => e.job === job | |
); | |
} else { | |
return employees; | |
} | |
}, | |
totalEmployees: ( | |
parent, | |
args, | |
{ employees } | |
) => employees.length, | |
}, | |
Employee: { | |
__resolveType: (parent) => { | |
switch (parent.job) { | |
case "LIFTOPERATOR": | |
return "LiftOperator"; | |
case "SKIPATROL": | |
return "SkiPatrol"; | |
case "INSTRUCTOR": | |
return "Instructor"; | |
case "BARTENDER": | |
return "Bartender"; | |
} | |
}, | |
}, | |
}; | |
const context = { employees }; | |
const server = new ApolloServer({ | |
typeDefs, | |
resolvers, | |
context, | |
}); | |
server | |
.listen() | |
.then(({ url }) => | |
console.log(`Server running on ${url}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment