Skip to content

Commit

Permalink
feat(profiles): adds a profiles search query
Browse files Browse the repository at this point in the history
Adds the profiles search query to fetch a list of profiles
  • Loading branch information
anguspiv committed Jul 31, 2022
1 parent e36c241 commit 001b835
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
116 changes: 115 additions & 1 deletion schema/types/Profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { objectType, extendType, inputObjectType } from 'nexus';
import { objectType, extendType, inputObjectType, stringArg, intArg } from 'nexus';
import { UserInputError } from 'apollo-server-micro';

interface ProfileWhere {
Expand Down Expand Up @@ -46,6 +46,58 @@ export const ProfileInput = inputObjectType({
},
});

const getProfileSearchTermWhere = (searchTerm: string) => {
const search = {
contains: searchTerm,
mode: 'insensitive',
};

const where = {
OR: [
{
firstName: search,
},
{
lastName: search,
},
{
nickname: search,
},
],
};

return where;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getProfileFieldsWhere = ({ firstName, lastName, nickname }: any) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const where: any = {};

if (firstName) {
where.firstName = {
contains: firstName,
mode: 'insensitive',
};
}

if (lastName) {
where.lastName = {
contains: lastName,
mode: 'insensitive',
};
}

if (nickname) {
where.nickname = {
contains: nickname,
mode: 'insensitive',
};
}

return where;
};

export const ProfileQuery = extendType({
type: 'Query',
definition(t) {
Expand All @@ -72,6 +124,68 @@ export const ProfileQuery = extendType({
});
},
});

t.nonNull.list.field('profiles', {
type: 'Profile',
description: 'Find a list of User Profiles',
args: {
searchTerm: stringArg({
description: 'The search term to search for',
}),
firstName: stringArg({
description: "The User Profile's First Name",
}),
lastName: stringArg({
description: "The User Profile's Last Name",
}),
nickname: stringArg({
description: "The User Profile's Nickname",
}),
limit: intArg({
description: 'The number of results to return',
default: 10,
}),
offset: intArg({
description: 'The number of results to skip',
default: 0,
}),
sortBy: stringArg({
description: 'The field to order by',
default: 'id',
}),
order: stringArg({
description: 'The order to sort by',
default: 'asc',
}),
},
resolve(_parent, { searchTerm, limit, offset, sortBy, order, ...fields }, { prisma }) {
let where = {};

if (searchTerm) {
where = getProfileSearchTermWhere(searchTerm);
}

if (!searchTerm) {
where = getProfileFieldsWhere(fields);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const query: any = {
where,
take: limit,
};

if (offset) {
query.skip = offset;
}

if (sortBy) {
query.orderBy = [{ [sortBy]: order }];
}

return prisma.profile.findMany(query);
},
});
},
});

Expand Down
27 changes: 27 additions & 0 deletions src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ type Query {
"""Find a User's profile"""
profile(input: ProfileInput): Profile

"""Find a list of User Profiles"""
profiles(
"""The User Profile's First Name"""
firstName: String

"""The User Profile's Last Name"""
lastName: String

"""The number of results to return"""
limit: Int = 10

"""The User Profile's Nickname"""
nickname: String

"""The number of results to skip"""
offset: Int = 0

"""The order to sort by"""
order: String = "asc"

"""The search term to search for"""
searchTerm: String

"""The field to order by"""
sortBy: String = "id"
): [Profile]!

"""Find a single user"""
user(input: UserInput): User

Expand Down

0 comments on commit 001b835

Please sign in to comment.