-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to @nexus/schema from nexus framework (#68)
* migrate to nexus schema + fixes * add tsconfig.cjs.json * fix package.json * add copyDirWiithTemplate * tests and utils * Update template/pages/api/graphql.ts.ejs * fix up
- Loading branch information
Showing
24 changed files
with
371 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
inject: true | ||
to: graphql/schema.ts | ||
after: import './context' | ||
skip_if: import './<%= name %>' | ||
to: graphql/modules/index.ts | ||
after: export * from './scalars' | ||
skip_if: export * from './<%= name %>' | ||
--- | ||
import './<%= name %>'; | ||
export * from './<%= name %>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
import { schema } from 'nexus'; | ||
import { IncomingMessage } from 'http'; | ||
|
||
import { Context as ApolloContext } from 'apollo-server-core'; | ||
import { PrismaClient, User } from '@prisma/client'; | ||
|
||
import { prisma } from '../lib/prisma'; | ||
import { verifyAuthHeader } from '../services/auth'; | ||
|
||
schema.addToContext(async (data) => { | ||
const authHeader = verifyAuthHeader((data as any).req.headers.authorization); | ||
let user = null; | ||
/** | ||
* Populates a context object for use in resolvers. | ||
* If there is a valid auth token in the authorization header, it will add the user to the context | ||
* @param context context from apollo server | ||
*/ | ||
export async function createContext(context: ApolloApiContext): Promise<Context> { | ||
const authHeader = verifyAuthHeader(context.req.headers.authorization); | ||
let user: User | null = null; | ||
|
||
if (authHeader) { | ||
user = await prisma.user.findOne({ where: { id: authHeader.userId } }); | ||
} | ||
|
||
return { user }; | ||
}); | ||
return { | ||
db: prisma, | ||
user, | ||
}; | ||
} | ||
|
||
type ApolloApiContext = ApolloContext<{ req: IncomingMessage }>; | ||
|
||
export type Context = { | ||
db: PrismaClient; | ||
user: User; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './scalars'; | ||
export * from './user'; | ||
export * from './profile'; |
4 changes: 2 additions & 2 deletions
4
template/graphql/profile.ts → template/graphql/modules/profile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { DateTimeResolver, JSONObjectResolver } from 'graphql-scalars'; | ||
import { asNexusMethod } from '@nexus/schema'; | ||
|
||
export const jsonScalar = asNexusMethod(JSONObjectResolver, 'json'); | ||
export const dateTimeScalar = asNexusMethod(DateTimeResolver, 'date'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.