How to use enum of Apollo-Server Graphql in resolver?
Environment
- apollo-server
- express
- typescript
- typeorm
Type Definitions (typeDefs.ts)
import { gql } from 'apollo-server-express';
const typeDefs = gql`
enum Part {
Hand, Arm, Waist, Bottom
}
type PartInfo {
team: Int,
tag: String,
part: Part
}
...
type Query {
...
hand(team: Int): PartInfo,
...
}
`;
export default typeDefs;
Resolvers (resolvers.ts)
const resolvers = {
Query: {
...
hand: async (parent, args, context, info) => {
const { team } = args;
...
return {
team,
tag: "handTag",
part: Part.hand
}
}
...
},
};
export default resolvers;
Problem
I want to use enum Part of typeDefs.ts
at resolvers.ts
I tried
return {
team,
tag: "handTag",
part: "Hand"
}
also, but dosent work.
How to use enum type which is defined in typeDefs.ts
at resolvers.ts
?
check please!
Solution 1:
Besides Schema (typeDef.ts), you should also define your enum in resolver.
const resolvers = {
Query: {
...
hand: async (parent, args, context, info) => {
const { team } = args;
...
return {
team,
tag: "handTag",
part: Part.hand
}
}
...
},
Part: {
Hand: Part.hand
}
};
export default resolvers;