{"size": 708, "ext": "js", "max_stars_repo_path": "app/graphql/permissions.js", "max_stars_repo_name": "Nasseratic/try-gql-express-apollo-server-with-auth-middlewares", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/graphql/permissions.js", "max_issues_repo_name": "Nasseratic/try-gql-express-apollo-server-with-auth-middlewares", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/graphql/permissions.js", "max_forks_repo_name": "Nasseratic/try-gql-express-apollo-server-with-auth-middlewares", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 16.8571428571, "max_line_length": 67, "alphanum_fraction": 0.6172316384}
const { rule, shield, and, or, not } = require('graphql-shield');

// Rules

const isAuthenticated = rule()(async (parent, args, ctx, info) => {
  return ctx.user.email !== undefined
})

const isAdmin = rule()(async (parent, args, ctx, info) => {
  return ctx.user.role === 'admin'
})


const isSelf = rule()(async (parent, args, ctx, info) => {
  return ctx.user._id === args.id
})

const isEditor = rule()(async (parent, args, ctx, info) => {
  return ctx.user.role === 'editor'
})

module.exports = shield({
  Query: {
    user: isAuthenticated,
    users: isAuthenticated
  },
  Mutation: {
    newUser: and( isAuthenticated , isAdmin ) ,
    editUser: and(  isAuthenticated , isSelf )
  },
})










