Last active
October 18, 2020 20:32
-
-
Save shrirambalakrishnan/325de85a3410b8dc02951509e808fd68 to your computer and use it in GitHub Desktop.
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 { 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