From my medium post about Keycloak Auth in GraphQL
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 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