@ev-graphql/resolver
v0.4.0
Published
> Resolver function for GraphQL
Downloads
12
Readme
@ev-graphql/resolver
Resolver function for GraphQL
- resolver
resolver: (fn: ResolverFn) => ResolverFn
Install
yarn add graphql @ev-graphql/resolver
Usage
const { AuthenticationError } = require("apollo-server-express");
const { resolver } = require("@ev-graphql/resolver");
const { User } = require("./models/User");
const isAuth = resolver(async (_, args, { req }) => {
const auth = req.headers.authorization;
if (!auth || !auth.toLowerCase().startsWith("bearer ")) {
throw new AuthenticationError("Unauthorized");
}
if (auth.slice("bearer ".length) !== process.env.API_TOKEN) {
throw new AuthenticationError("Unauthorized");
}
});
const allUsers = resolver(isAuth, async () => {
const users = await User.find();
return users;
});
const user = resolver(isAuth, async (_, { _id }) => {
const user = await User.findById(_id);
return user;
});
module.exports.resolvers = {
Query: {
allUsers,
user,
},
};