Skip to content

Commit

Permalink
fix: app.reset and make code more portable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Jul 28, 2020
1 parent 4bdee8e commit 493cbf8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/lib/add-to-context-extractor/extractor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ it('extracts from returned object of referenced primitive value', () => {
`)
})

it('can access all default context types', () => {
const allTypesExported = DEFAULT_CONTEXT_TYPES.typeImports.every(i => {
it('all types extracted from the default context data are importable', () => {
const allTypesExported = DEFAULT_CONTEXT_TYPES.typeImports.every((i) => {
const project = new tsm.Project({
addFilesFromTsConfig: false,
skipFileDependencyResolution: true
skipFileDependencyResolution: true,
})

const sourceFile = project.addSourceFileAtPath(i.modulePath + '.d.ts')
const sourceFile = project.addSourceFileAtPath(i.modulePath + '.ts')

return sourceFile.getExportedDeclarations().has(i.name)
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib/add-to-context-extractor/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hardWriteFile } from '../fs'
import * as Layout from '../layout'
import { rootLogger } from '../nexus-logger'
import { createTSProject } from '../tsc'
import { Exception } from '../utils'
import { Exception, prettyImportPath } from '../utils'
import { ContribType, extractContextTypes, ExtractedContextTypes } from './extractor'

const log = rootLogger.child('addToContextExtractor')
Expand All @@ -22,7 +22,7 @@ export const DEFAULT_CONTEXT_TYPES: ExtractedContextTypes = {
typeImports: [
{
name: 'ContextAdderLens',
modulePath: require.resolve('../../../dist/runtime/schema/schema').split('.')[0],
modulePath: prettyImportPath(require.resolve('../../runtime/schema/schema')),
isExported: true,
isNode: false,
},
Expand Down
11 changes: 9 additions & 2 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,21 @@ export function noop() {}
* foo/bar -> foo/bar
* ```
* ```
* foo/bar.js -> foo/bar
* ```
* ```
* foo/bar/index.js -> foo/bar
* ```
*/
export function prettyImportPath(id: string): string {
const { dir, name } = Path.parse(id)
const { dir, name, ext } = Path.parse(id)

if (name === 'index') return dir

if (ext) {
return id.replace(ext, '')
}

return id
}

Expand Down Expand Up @@ -541,4 +548,4 @@ export function indent(str: string, len: number, char: string = ' ') {
.split('\n')
.map((s) => char.repeat(len) + s)
.join('\n')
}
}
22 changes: 19 additions & 3 deletions src/runtime/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import * as GraphQL from 'graphql'
import * as HTTP from 'http'
import 'jest-extended'
import * as Lo from 'lodash'
import { inspect } from 'util'
import { setReflectionStage, unsetReflectionStage } from '../lib/reflection'
import * as App from './app'
import * as Lifecycle from './lifecycle'

let app: App.PrivateApp

function dump(x: any) {
return inspect(x, { depth: null })
}

beforeEach(() => {
app = App.create() as App.PrivateApp
// fix once there is a singleton logger
Expand All @@ -23,13 +28,19 @@ describe('reset', () => {
server: { path: '/bar' },
schema: { connections: { foo: {} } },
})
app.schema.objectType({ name: 'Foo', definition(t) { t.string('ok') } })
app.schema.objectType({
name: 'Foo',
definition(t) {
t.string('ok')
},
})
app.on.start(() => {})
app.assemble()
app.reset()
expect(app.settings.current.server.path).toEqual(app.settings.original.server.path)
expect(app.settings.current.schema).toEqual(app.settings.original.schema)
expect(app.private.state).toEqual(originalAppState)
// must dump because functions are not equal-able
expect(dump(app.private.state)).toEqual(dump(originalAppState))
expect(app.private.state.components.lifecycle).toEqual(Lifecycle.createLazyState())
})

Expand All @@ -48,7 +59,12 @@ describe('assemble', () => {

beforeEach(() => {
// avoid schema check error
app.schema.objectType({ name: 'Foo', definition(t) { t.string('ok') } })
app.schema.objectType({
name: 'Foo',
definition(t) {
t.string('ok')
},
})
})

it('multiple calls is a noop', () => {
Expand Down
47 changes: 29 additions & 18 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ export function create(): App {
server: serverComponent.public,
on: lifecycleComponent.public,
reset() {
// todo once we have log filtering, make this debug level
rootLogger.trace('resetting state')
rootLogger.debug('resetting state')
schemaComponent.private.reset()
serverComponent.private.reset()
settingsComponent.private.reset()
lifecycleComponent.private.reset()
state.assembled = null
state.plugins = []
state.running = false
dogfood()
},
async start() {
if (Reflection.isReflection()) return
Expand Down Expand Up @@ -207,25 +207,36 @@ export function create(): App {
}

/**
* Setup default log filter
* Dogfood the public API to change things.
*/
app.settings.change({
logger: {
filter: 'app:*, nexus:*@info+, *@warn+',
pretty: { timeDiff: false },
},
})
function dogfood() {
/**
* Setup default log filter
*/
app.settings.change({
logger: {
filter: 'app:*, nexus:*@info+, *@warn+',
pretty: { timeDiff: false },
},
})

/**
* Setup default scalar types
*/
app.schema.importType(builtinScalars.DateTime, 'date')
app.schema.importType(builtinScalars.Json, 'json')

/**
* Add `req` and `res` to the context by default
*/
app.schema.addToContext((params) => params)
}

/**
* Setup default scalar types
*/
app.schema.importType(builtinScalars.DateTime, 'date')
app.schema.importType(builtinScalars.Json, 'json')
// HACK dogfood function called eagerly here once and
// then within reset method. We should have a better
// reset system.

/**
* Add `req` and `res` to the context by default
*/
app.schema.addToContext(params => params)
dogfood()

return {
...app,
Expand Down

0 comments on commit 493cbf8

Please sign in to comment.