Skip to content

Commit

Permalink
feat: migrate away from prisma-nexus-plugin. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
cball authored May 11, 2021
1 parent dd9113b commit 093e8c4
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Creating a new app", () => {
describe("prisma .env", () => {
it("contains the proper database URL", () => {
cy.task("getAppName").then((appName) => {
cy.task("readProjectFile", "prisma/.env")
cy.task("readProjectFile", ".env")
.should("contain", `postgresql://postgres@localhost:5432`)
.should("contain", `${appName}_dev`);
});
Expand Down
7 changes: 6 additions & 1 deletion packages/create-bison-app/tasks/copyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ async function copyFiles({ variables, targetFolder }) {
copyWithTemplate(fromPath("README.md.ejs"), toPath("README.md"), variables),
copyWithTemplate(fromPath("_.gitignore"), toPath(".gitignore"), variables),

copyWithTemplate(
fromPath("_.env.ejs"),
toPath(".env"),
variables
),

copyWithTemplate(
fromPath("_.env.local.ejs"),
toPath(".env.local"),
Expand Down Expand Up @@ -80,7 +86,6 @@ async function copyFiles({ variables, targetFolder }) {
"layouts",
"lib",
"prisma",
"!prisma/_.env*",
"public",
"scripts",
"services",
Expand Down
3 changes: 2 additions & 1 deletion packages/create-bison-app/template/_.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.env

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
prisma/.env

.vercel

Expand Down
108 changes: 75 additions & 33 deletions packages/create-bison-app/template/_templates/graphql/new/graphql.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ to: graphql/modules/<%= name %>.ts
---
<% camelized = h.inflection.camelize(name) -%>
<% plural = h.inflection.pluralize(camelized) -%>
import { objectType, extendType } from 'nexus';
import { objectType, extendType, inputObjectType, stringArg, arg, nonNull, enumType } from 'nexus';
import { Role } from '@prisma/client';
import { UserInputError, /*ForbiddenError*/ } from 'apollo-server-micro';

// import { isAdmin } from '../services/permissions';
Expand All @@ -13,54 +14,95 @@ export const <%= camelized %> = objectType({
name: '<%= camelized %>',
description: 'A <%= camelized %>',
definition(t) {
t.model.id();
t.model.createdAt();
t.model.updatedAt();
t.nonNull.id('id');
t.nonNull.date('createdAt');
t.nonNull.date('updatedAt');
},
});

/*
// Enums
export const CallPreference = enumType({
name: 'CallPreference',
members: ['WEEKDAY', 'WEEKEND', 'WEEKNIGHT'],
});

// Queries
export const <%= camelized %>Queries = extendType({
type: 'Query',
definition: (t) => {
// List <%= plural %> Query (admin only)
t.crud.<%= plural.toLowerCase() %>({
filtering: true,
ordering: true,
// use resolve for permission checks or to remove fields
resolve: async (root, args, ctx, info, originalResolve) => {
if (!isAdmin(ctx.user)) throw new ForbiddenError('Unauthorized');
// List <%= plural %> Query
t.list.field('<%= plural.toLowerCase() %>', {
type: '<%= camelized %>',
authorize: (_root, _args, ctx) => !!ctx.user,
args: nonNull(arg({ type: 'SomethingQueryInput' })),
description: 'Returns available <%= plural.toLowerCase() %>',
})

return await originalResolve(root, args, ctx, info);
// single query
t.field('something', {
type: '<%= camelized %>',
description: 'Returns a specific <%= camelized %>',
authorize: (_root, _args, ctx) => !!ctx.user,
args: nonNull(arg({ type: 'SomethingQueryInput' })),
resolve: (_root, args, ctx) => {
// TODO
},
});

// Custom Query
t.field('me', {
type: 'User',
description: 'Returns the currently logged in user',
nullable: true,
resolve: (_root, _args, ctx) => ctx.user,
});

t.list.field('availabilityForUser', {
type: 'Event',
description: 'Returns available time slots to schedule calls with an expert',
})
},
});

// Mutations
export const <%= camelized %>Mutations = extendType({
type: 'Mutation',
definition: (t) => {
t.field('somethingMutation', {
type: 'String',
description: 'Does something',
args: {
data: nonNull(arg({ type: 'SomethingMutationInput' })),
},
authorize: (_root, _args, ctx) => !!ctx.user,
resolve: async (_root, args, ctx) => {
console.log(args.data.hello)
return args.data.hello
}
});
},
});
*/

// Inputs
export const SomethingMutationInput = inputObjectType({
name: 'SomethingMutationInput',
description: 'Input used to do something',
definition: (t) => {
t.nonNull.string('hello');
},
})

export const SomethingQueryInput = inputObjectType({
name: 'SomethingQueryInput',
description: 'Input used to do something',
definition: (t) => {
t.nonNull.string('hello');
},
});

export const <%= camelized %>OrderByInput = inputObjectType({
name: '<%= camelized %>OrderByInput',
description: 'Order <%= camelized.toLowerCase() %> by a specific field',
definition(t) {
t.field('hello', { type: 'SortOrder' });
},
});

export const UserWhereUniqueInput = inputObjectType({
name: 'UserWhereUniqueInput',
description: 'Input to find users based on unique fields',
definition(t) {
t.id('id');
t.email('email');
},
});

export const UserWhereInput = inputObjectType({
name: 'UserWhereInput',
description: 'Input to find users based other fields',
definition(t) {
t.int('id');
t.field('email', { type: 'StringFilter' });
},
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './scalars';
export * from './user';
export * from './profile';
export * from './shared'
20 changes: 13 additions & 7 deletions packages/create-bison-app/template/graphql/modules/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ export const Profile = objectType({
name: 'Profile',
description: 'A User Profile',
definition(t) {
t.model.id();
t.model.firstName();
t.model.lastName();
t.model.createdAt();
t.model.updatedAt();
t.model.user();
t.nonNull.id('id');
t.nonNull.date('createdAt');
t.nonNull.date('updatedAt');
t.nonNull.string('firstName');
t.nonNull.string('lastName');
t.nonNull.field('user', {
type: 'User',
resolve: (parent, _, context) => {
return context.prisma.profile.findUnique({
where: { id: parent.id }
}).user()
}
})

t.string('fullName', {
nullable: true,
description: 'The first and last name of a user',
resolve({ firstName, lastName }) {
return [firstName, lastName].filter((n) => Boolean(n)).join(' ');
Expand Down
25 changes: 25 additions & 0 deletions packages/create-bison-app/template/graphql/modules/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { enumType, inputObjectType } from 'nexus';

// the following items are migrated from the prisma plugin
export const SortOrder = enumType({
name: 'SortOrder',
description: 'Sort direction for filtering queries (ascending or descending)',
members: ['asc', 'desc'],
});

export const StringFilter = inputObjectType({
name: 'StringFilter',
description: 'A way to filter string fields. Meant to pass to prisma where clause',
definition(t) {
t.string('contains');
t.string('endsWith');
t.string('equals');
t.string('gt');
t.string('gte');
t.list.nonNull.string('in');
t.string('lt');
t.string('lte');
t.list.nonNull.string('notIn');
t.string('startsWith');
},
});
Loading

0 comments on commit 093e8c4

Please sign in to comment.