Created
April 7, 2021 15:04
-
-
Save bloo/2d009d427a7a4c1e761efcfd394da81d to your computer and use it in GitHub Desktop.
Resilient Tech Blog - Code-first GraphQL with Nexus - budget objectType.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
// schema.ts | |
import { makeSchema, asNexusMethod } from 'nexus' | |
import { nexusPrisma } from 'nexus-plugin-prisma' | |
import { DateTimeResolver } from 'graphql-scalars' | |
import path from 'path' | |
// This is types we had created | |
import * as types from './typeDefs' | |
// This is custom scalar type that allows us to do t.date because date is not | |
// a primitive type in GraphQL | |
const dateTimeScalar = asNexusMethod(DateTimeResolver, 'date') | |
export const schema = makeSchema({ | |
// This is all the types that we had defined | |
types: [types, dateTimeScalar], | |
// This method is invoked during server initialization, therefore we can add this | |
// flag to avoid generated artifacts because we are not using ts-node. If you are | |
// running ts-node, you will need this to be true otherwise TS will fail because | |
// it will not be able to find the types we import from our GraphQL schema | |
shouldGenerateArtifacts: process.env.NODE_ENV !== 'production', | |
plugins: [ | |
// We are using a plugin to connect Prisma and Nexus | |
nexusPrisma({ | |
// We are letting the plugin know to use the prisma client instance in the | |
// context object so it doesn’t create a new prisma client instance. | |
prismaClient: (context) => context.db, | |
}) | |
], | |
outputs: { | |
// This is the schema that we would have created using SDL-first approach. | |
// Alternative to providing the result of makeSchema call to GraphQL server, | |
// we can use this instead | |
schema: path.join(__dirname, 'graphql.schema'), | |
// This is where the types of our GraphQL schema is saved, it allows us to | |
// import {NexusGenAllType} from ‘typegen-nexus’ | |
typegen: path.join( | |
__dirname, | |
'node_modules/@types/typegen-nexus/index.d.ts', | |
), | |
}, | |
// This allows the scalar field resolver to correctly get the type of the field | |
// object. The caveat is the name of the field object must match the data model. | |
// If you do not use nexus-plugin-prisma, you can still use this for your field | |
// resolvers to get the type of the field object IF AND ONLY IF the field object | |
// matches the interface. There are ways to work around this limitation. Please | |
// refer to Nexus discussion on source types. | |
sourceTypes: { | |
modules: [ | |
{ | |
module: '@prisma/client', | |
alias: 'db', | |
}, | |
], | |
}, | |
// Allows Nexus to know the shape of the context parameter in resolver. We have to | |
// prevent this code from running during server initialization because we are | |
// deploying the transpiled artifacts. It is not an issue if you use ts-node | |
// in production. | |
...(process.env.NODE_ENV !== 'production' && { | |
contextType: { | |
module: path.join(__dirname, './context.ts'), | |
export: 'Context', | |
}, | |
}), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment