Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darahayes/5f72b063a3b5a14e61e163e3ea5aa05d to your computer and use it in GitHub Desktop.
Save darahayes/5f72b063a3b5a14e61e163e3ea5aa05d to your computer and use it in GitHub Desktop.
From my medium post about Keycloak Auth in GraphQL
const typeDefs = gql`
type Article {
id: ID!
title: String!
content: String!
}
type Query {
listArticles: [Article]! @auth
}
type Mutation {
publishArticle(title: String!, content: String!): Article! @hasRole(role: "editor")
}
`
const resolvers = {
Query: {
listArticles: (obj, args, context, info) => {
return Database.listArticles()
}
},
mutation: {
publishArticle: (object, args, context, info) => {
const user = context.kauth.accessToken.content // get the user details from the access token
return Database.createArticle(args.title, args.content, user)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment