Skip to content

Instantly share code, notes, and snippets.

@shrirambalakrishnan
Last active October 18, 2020 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrirambalakrishnan/325de85a3410b8dc02951509e808fd68 to your computer and use it in GitHub Desktop.
Save shrirambalakrishnan/325de85a3410b8dc02951509e808fd68 to your computer and use it in GitHub Desktop.
const { AuthDirective } = require("./src/directives/auth")
// The GraphQL schema
const typeDefs = gql`
directive @auth(
requires: Role = ADMIN
) on FIELD_DEFINITION | OBJECT
enum Role {
ADMIN
REVIEWER
USER
UNKNOWN
}
input PostInputType {
id: Int!
title: String!
description: String
}
type CommentType @auth(requires: USER) {
id: Int!
comment: String!
}
type PostType {
id: Int!
title: String
description: String
comments: [CommentType]
}
type UserType {
id: Int
name: String
email: String
posts: [PostType]
}
type Query {
users: [UserType]
posts: [PostType]
}
type Mutation {
updatePost(data: PostInputType): PostType
}
`;
};
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
auth: AuthDirective
},
context: async ({req}) => {
return {
postsLoader: postsLoader,
commentsLoader: commentsLoader,
// hard-coded user
user: {
id: 100,
email: "admin@admin.com",
roles: ["USER"]
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment