Skip to content

Commit

Permalink
feat: make contextType independent of source types
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- `contextType` is no longer nested in `typegenAutoConfig`
- `contextType` can no longer references `sources` registered in `typegenAutoConfig`
- `contextType` API was refactored to use  `module` instead of `path` and `export` instead of `name`
- `rootTyping` API was refactored to use  `module` instead of `path` and `export` instead of `name`
  • Loading branch information
Weakky committed Dec 10, 2020
1 parent 0a43cbc commit 642be8b
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 134 deletions.
8 changes: 8 additions & 0 deletions examples/apollo-fullstack/src/context.ts
@@ -0,0 +1,8 @@
import { LaunchApi, UserApi } from './typeDefs'

export interface Context {
dataSources: {
userAPI: UserApi
launchAPI: LaunchApi
}
}
3 changes: 2 additions & 1 deletion examples/apollo-fullstack/src/fullstack-typegen.ts
@@ -1,6 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */

import * as t from './typeDefs'
import { Context } from './context'

declare global {
interface NexusGen extends NexusGenTypes {}
Expand Down Expand Up @@ -221,7 +222,7 @@ export type NexusGenFeaturesConfig = {
}

export interface NexusGenTypes {
context: t.Context
context: Context
inputTypes: NexusGenInputs
rootTypes: NexusGenRootTypes
inputTypeShapes: NexusGenInputs & NexusGenEnums & NexusGenScalars
Expand Down
9 changes: 6 additions & 3 deletions examples/apollo-fullstack/src/index.ts
Expand Up @@ -15,16 +15,19 @@ const schema = makeSchema({
types,
outputs: {
schema: path.join(__dirname, '../fullstack-schema.graphql'),
typegen: path.join(__dirname.replace(/\/dist$/, '/src'), '../src/fullstack-typegen.ts'),
typegen: path.join(__dirname, 'fullstack-typegen.ts'),
},
typegenAutoConfig: {
sources: [
{
source: path.join(__dirname.replace(/\/dist$/, '/src'), './typeDefs.ts'),
source: path.join(__dirname, 'typeDefs.ts'),
alias: 't',
},
],
contextType: 't.Context',
},
contextType: {
module: path.join(__dirname, 'context.ts'),
export: 'Context',
},
prettierConfig: require.resolve('../../../.prettierrc'),
})
Expand Down
7 changes: 0 additions & 7 deletions examples/apollo-fullstack/src/typeDefs.ts
Expand Up @@ -65,10 +65,3 @@ export interface Utils {
}): T[]
createStore(): {}
}

export interface Context {
dataSources: {
userAPI: UserApi
launchAPI: LaunchApi
}
}
2 changes: 1 addition & 1 deletion examples/ghost/config.development.json
Expand Up @@ -9,7 +9,7 @@
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "",
"password": "root",
"database": "ghost_nexus"
}
}
Expand Down
9 changes: 7 additions & 2 deletions examples/ghost/scripts/dumpTables.ts
Expand Up @@ -5,16 +5,21 @@ import fs from 'fs-extra'
import _ from 'lodash'
import Knex from 'knex'

const config = require('../config.development.json')

const knex = Knex({
...require('../config.development.json').database,
...config.database,
})

const GENERATED_DIR = path.join(__dirname, '..', 'src', 'generated')
const OUT_TYPES = path.join(GENERATED_DIR, 'ghost-db-types.ts')
const OUT_TABLES = path.join(GENERATED_DIR, 'ghost-db-tables.ts')

async function generateTypes() {
const rows = await knex.select('*').from('information_schema.tables').where('table_schema', 'ghost_nexus')
const rows = await knex
.select('*')
.from('information_schema.tables')
.where('table_schema', config.database.connection.database)
const tableNames = _.map(rows, 'TABLE_NAME').sort()
const connectionString = dbConnectionString()
await knex.destroy()
Expand Down
103 changes: 88 additions & 15 deletions examples/ghost/src/generated/ghost-nexus.ts
@@ -1,22 +1,27 @@
/**
* This file was generated by Nexus Schema
* Do not make changes to this file directly
*/
/** This file was generated by Nexus Schema Do not make changes to this file directly */

import * as ctx from '../data-sources/Context'
import * as db from './ghost-db-types'
import { Context } from './../data-sources/Context'
import { FieldAuthorizeResolver } from '@nexus/schema/dist/plugins/fieldAuthorizePlugin'
import { core } from '@nexus/schema'
declare global {
interface NexusGenCustomInputMethods<TypeName extends string> {
/**
* A date string, such as 2007-12-03, compliant with the `full-date` format outlined in section 5.6 of the
* RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
*/
date<FieldName extends string>(
fieldName: FieldName,
opts?: core.ScalarInputFieldConfig<core.GetGen3<'inputTypes', TypeName, FieldName>>
opts?: core.CommonInputFieldConfig<TypeName, FieldName>
): void // "Date";
}
}
declare global {
interface NexusGenCustomOutputMethods<TypeName extends string> {
/**
* A date string, such as 2007-12-03, compliant with the `full-date` format outlined in section 5.6 of the
* RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
*/
date<FieldName extends string>(
fieldName: FieldName,
...opts: core.ScalarOutSpread<TypeName, FieldName>
Expand Down Expand Up @@ -178,16 +183,17 @@ export type NexusGenAbstractsUsingStrategyResolveType = never

export type NexusGenFeaturesConfig = {
abstractTypeStrategies: {
isTypeOf: true
resolveType: false
isTypeOf: false
resolveType: true
__typename: false
}
}

export interface NexusGenTypes {
context: ctx.Context
context: Context
inputTypes: NexusGenInputs
rootTypes: NexusGenRootTypes
inputTypeShapes: NexusGenInputs & NexusGenEnums & NexusGenScalars
argTypes: NexusGenArgTypes
fieldTypes: NexusGenFieldTypes
fieldTypeNames: NexusGenFieldTypeNames
Expand Down Expand Up @@ -218,14 +224,81 @@ declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
/**
* Authorization for an individual field. Returning "true"
* or "Promise<true>" means the field can be accessed.
* Returning "false" or "Promise<false>" will respond
* with a "Not Authorized" error for the field.
* Returning or throwing an error will also prevent the
* resolver from executing.
* Authorization for an individual field. Returning "true" or "Promise<true>" means the field can be
* accessed. Returning "false" or "Promise<false>" will respond with a "Not Authorized" error for the
* field. Returning or throwing an error will also prevent the resolver from executing.
*/
authorize?: FieldAuthorizeResolver<TypeName, FieldName>
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
required?: boolean
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
required?: boolean
}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {
/**
* Whether the type can be null
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value. If list is true, we assume the type is a
* list. If list is an array, we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
*
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
*
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
required?: boolean
}
}
9 changes: 4 additions & 5 deletions examples/ghost/src/ghost-schema.ts
Expand Up @@ -10,12 +10,7 @@ export const schema = makeSchema({
},
plugins: [fieldAuthorizePlugin()],
typegenAutoConfig: {
contextType: 'ctx.Context',
sources: [
{
alias: 'ctx',
source: path.join(__dirname, 'data-sources', 'Context.ts'),
},
{
alias: 'db',
source: path.join(__dirname, 'generated', 'ghost-db-types.ts'),
Expand All @@ -26,5 +21,9 @@ export const schema = makeSchema({
Date: 'Date',
},
},
contextType: {
module: path.join(__dirname, 'data-sources', 'Context.ts'),
export: 'Context',
},
prettierConfig: require.resolve('../../../.prettierrc'),
})
2 changes: 1 addition & 1 deletion examples/kitchen-sink/src/kitchen-sink-definitions.ts
Expand Up @@ -83,7 +83,7 @@ export const UnusedInterface = interfaceType({
definition(t) {
t.boolean('ok')
},
rootTyping: { name: 'UnusedInterfaceTypeDef', path: __filename },
rootTyping: { module: __filename, export: 'UnusedInterfaceTypeDef' },
})

export const Baz = interfaceType({
Expand Down
1 change: 1 addition & 0 deletions examples/kitchen-sink/src/kitchen-sink.gen.ts
Expand Up @@ -115,6 +115,7 @@ export interface NexusGenObjects {
SomeNode: {
// root type
data?: NexusGenRootTypes['SomeNode'] | null // SomeNode
id?: string | null // ID
}
TestObj: {
// root type
Expand Down
7 changes: 5 additions & 2 deletions examples/star-wars/src/schema.ts
Expand Up @@ -22,11 +22,14 @@ export const schema = makeSchema({
typegenAutoConfig: {
sources: [
{
source: path.join(__dirname.replace(/\/dist$/, '/src'), './types/backingTypes.ts'),
source: path.join(__dirname, 'types', 'backingTypes.ts'),
alias: 'swapi',
},
],
contextType: 'swapi.ContextType',
},
contextType: {
module: path.join(__dirname, 'types', 'context.ts'),
export: 'ContextType',
},
prettierConfig: require.resolve('../../../.prettierrc'),
features: {
Expand Down
3 changes: 2 additions & 1 deletion examples/star-wars/src/star-wars-typegen.ts
@@ -1,6 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */

import * as swapi from './types/backingTypes'
import { ContextType } from './types/context'

declare global {
interface NexusGen extends NexusGenTypes {}
Expand Down Expand Up @@ -170,7 +171,7 @@ export type NexusGenFeaturesConfig = {
}

export interface NexusGenTypes {
context: swapi.ContextType
context: ContextType
inputTypes: NexusGenInputs
rootTypes: NexusGenRootTypes
inputTypeShapes: NexusGenInputs & NexusGenEnums & NexusGenScalars
Expand Down
26 changes: 12 additions & 14 deletions examples/star-wars/src/types/backingTypes.ts
@@ -1,26 +1,24 @@
import { NexusGenEnums } from "../star-wars-typegen";
import { NexusGenEnums } from '../star-wars-typegen'

/**
* These are Flow types which correspond to the schema.
* They represent the shape of the data visited during field resolution.
* These are Flow types which correspond to the schema. They represent the shape of the data visited during
* field resolution.
*/
export interface CharacterFields {
id: string;
name: string;
friends: string[];
appears_in: NexusGenEnums["Episode"][];
id: string
name: string
friends: string[]
appears_in: NexusGenEnums['Episode'][]
}

export interface Human extends CharacterFields {
type: "Human";
home_planet?: string;
type: 'Human'
home_planet?: string
}

export interface Droid extends CharacterFields {
type: "Droid";
primary_function: string;
type: 'Droid'
primary_function: string
}

export type Character = Human | Droid;

export interface ContextType {}
export type Character = Human | Droid
1 change: 1 addition & 0 deletions examples/star-wars/src/types/context.ts
@@ -0,0 +1 @@
export interface ContextType {}
5 changes: 4 additions & 1 deletion examples/ts-ast-reader/src/schema.ts
Expand Up @@ -23,12 +23,15 @@ export const schema = makeSchema({
onlyTypes: [],
},
],
contextType: 't.ContextType',
backingTypeMap: {
Token: 'ts.Token<any>',
},
// debug: true,
},
contextType: {
module: path.join(__dirname, './types/context.ts'),
export: 'ContextType',
},
prettierConfig: require.resolve('../../../.prettierrc'),
nonNullDefaults: {
output: true,
Expand Down
4 changes: 2 additions & 2 deletions examples/ts-ast-reader/src/ts-ast-reader-typegen.ts
@@ -1,7 +1,7 @@
/** This file was generated by Nexus Schema Do not make changes to this file directly */

import * as t from './types/index'
import ts from 'typescript'
import { ContextType } from './types/context'

declare global {
interface NexusGen extends NexusGenTypes {}
Expand Down Expand Up @@ -2875,7 +2875,7 @@ export type NexusGenFeaturesConfig = {
}

export interface NexusGenTypes {
context: t.ContextType
context: ContextType
inputTypes: NexusGenInputs
rootTypes: NexusGenRootTypes
inputTypeShapes: NexusGenInputs & NexusGenEnums & NexusGenScalars
Expand Down
5 changes: 5 additions & 0 deletions examples/ts-ast-reader/src/types/context.ts
@@ -0,0 +1,5 @@
import * as ts from 'typescript'

export interface ContextType {
source: ts.SourceFile
}

0 comments on commit 642be8b

Please sign in to comment.