Graphql find user by username or email

Alex C

So I have this simple schema:

type User {
        _id: String,
        username: String,
        age: Int,
        email: String,
        password: String,
    }
    type Query {
        user(_id: String!): User,
    }

Resolver:

import User from './User';

export default {
    Query: {
        user: (_, { _id }) => User.findById(_id),
    },
}

how can I get a user by some specified fields like username, email without creating a query function for each one (if the user would have 1000 unique fields it would be pretty painful to create 1000 queries for each type) ?

Daniel Rearden

If the names of your arguments match the names of your fields, it's easy enough to pass the entire args object as a parameter to find() or findOne():

Query: {
  user: (_, args) => User.findOne(args),
},

In this case, you'll get the first user matching the arguments passed in and return it. In the event you want to return all possible matches, you can have two separate queries, one returning a single User, and another returning a List of Users:

Query: {
  user: (_, args) => User.findOne(args),
  users: (_, args) => User.find(args),
},

If there are any arguments that need to be treated differently (for example, if you wanted to pass in a limit or add some kind of pagination), you can always use a helper library like lodash or ramda to pull out just those arguments. For example:

users: (_, args) => {
  const where = omit(args, ['limit'])
  const limit = args.limit || 10
  return User.find(where).sort({'someField': -1}).limit(limit).exec()
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find a User by Username OR Email with One Field

Sails js,find user using either email or username

Can't find user by username or email from database in bundle

username and email interchanged for social user

Find User in Laravel by Username

Find if username and email exist in MongoDB

How can I get user by userName in GraphQL?

Retrieve user email for username signing firebase

Firebase. Change `username`(email) of registered user

Add user to role where username is email address

Get email from username of user in Phabricator

Username cannot be of email format, since user pool is configured for email

Login user for a custom user model with email as username field

Find user from email address

Access VBA: Find if computer username is an active user

Find out username of the current user in Windows in Haskell?

Find all tokens issues for a specified user by username

How to find user with exact username with kcadm?

How to add username with email and password in realtime while user creation?

How to create a new user in Firebase with Username, Email and Password?

Log in user using either email address or username in Django

Allow user to log in with either Email OR UserName (AspNet.Identity)

Support login with email or username for same user in ASP.NET Core

How to register a user with Email instead of username in Django Rest Framework

How to allow user to login with both username and email in php?

How to get the Azure AD authenticated user’s username and email

Find username from Active Directory using email id

Find whether email and username already exist in Firebase Auth in Flutter App

Why is UserManager.Find() returning null if my UserName is email based?