Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
Support express middleware when ejecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Mar 15, 2019
1 parent 67aacf8 commit 8ca4194
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/minimal/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"rootDir": "./src",
"rootDir": ".",
"outDir": "dist",
"sourceMap": true,
"lib": ["es2015", "esnext.asynciterable", "es2017", "dom"],
Expand Down
4 changes: 2 additions & 2 deletions packages/yoga/src/cli/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function useEntryPoint(
const indexFile = renderIndexFile(ejectFilePath)
const indexFilePath = path.join(path.dirname(ejectFilePath), 'index.ts')

outputFile(indexFile, indexFilePath, config.options, info)
outputFile(indexFilePath, indexFile, config.options, info)
}

export function writeEjectFiles(
Expand Down Expand Up @@ -123,8 +123,8 @@ function transpileModule(
}

function outputFile(
fileContent: string,
filePath: string,
fileContent: string,
compilerOptions: ts.CompilerOptions,
info: ConfigWithInfo,
) {
Expand Down
69 changes: 31 additions & 38 deletions packages/yoga/src/cli/commands/build/renderers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getRelativePath } from '.'
import { EOL } from 'os'
import * as path from 'path'
import { ConfigWithInfo } from '../../../types'
import { getRelativePath } from '.'
import { findFileByExtension } from '../../../helpers'
import { EOL } from 'os'
import { ConfigWithInfo } from '../../../types'

export function renderIndexFile(ejectFilePath: string) {
return `
Expand All @@ -27,26 +27,14 @@ export function renderPrismaEjectFile(filePath: string, info: ConfigWithInfo) {
return `
import * as path from 'path'
import { ApolloServer, makePrismaSchema, express, yogaEject } from 'yoga'
import datamodelInfo from '${getRelativePath(
fileDir,
info.datamodelInfoDir!,
)}'
import { prisma } from '${getRelativePath(fileDir, info.prismaClientDir!)}'
${
info.yogaConfig.contextPath
? `import context from '${getRelativePath(
fileDir,
info.yogaConfig.contextPath,
)}'`
: ''
}
import * as types from '${getRelativePath(
fileDir,
info.yogaConfig.resolversPath,
)}'
${renderImportIf('* as types', fileDir, info.yogaConfig.resolversPath)}
${renderImportIf('context', fileDir, info.yogaConfig.contextPath)}
${renderImportIf('expressMiddleware', fileDir, info.yogaConfig.expressPath)}
${renderImportIf('datamodelInfo', fileDir, info.datamodelInfoDir)}
${renderImportIf('{ prisma }', fileDir, info.prismaClientDir)}
export default yogaEject({
server() {
async server() {
const schema = makePrismaSchema({
types,
prisma: {
Expand Down Expand Up @@ -106,18 +94,19 @@ export function renderPrismaEjectFile(filePath: string, info: ConfigWithInfo) {
})
const app = express()
${info.yogaConfig.expressPath ? 'await expressMiddleware(app)' : ''}
apolloServer.applyMiddleware({ app, path: '/' })
return app
},
startServer(app) {
async startServer(app) {
return app.listen({ port: 4000 }, () => {
console.log(
\`🚀 Server ready at http://localhost:4000/\`,
)
})
},
stopServer(http) {
async stopServer(http) {
http.close()
}
})
Expand All @@ -130,21 +119,12 @@ export function renderSimpleIndexFile(filePath: string, info: ConfigWithInfo) {
return `\
import * as path from 'path'
import { ApolloServer, makeSchema, express, yogaEject } from 'yoga'
${
info.yogaConfig.contextPath
? `import context from '${getRelativePath(
fileDir,
info.yogaConfig.contextPath,
)}'`
: ''
}
import * as types from '${getRelativePath(
fileDir,
info.yogaConfig.resolversPath,
)}'
${renderImportIf('* as types', fileDir, info.yogaConfig.resolversPath)}
${renderImportIf('context', fileDir, info.yogaConfig.contextPath)}
${renderImportIf('expressMiddleware', fileDir, info.yogaConfig.expressPath)}
export default yogaEject({
server() {
async server() {
const schema = makeSchema({
types,
outputs: {
Expand Down Expand Up @@ -183,18 +163,19 @@ export default yogaEject({
})
const app = express()
${info.yogaConfig.expressPath ? 'await expressMiddleware(app)' : ''}
apolloServer.applyMiddleware({ app, path: '/' })
return app
},
startServer(app) {
async startServer(app) {
return app.listen({ port: 4000 }, () => {
console.log(
\`🚀 Server ready at http://localhost:4000/\`,
)
})
},
stopServer(http) {
async stopServer(http) {
http.close()
}
})
Expand Down Expand Up @@ -228,3 +209,15 @@ ${resolversFile
.join(EOL)}
`
}

export function renderImportIf(
importName: string,
sourceDir: string,
targetPath: string | undefined,
) {
if (!targetPath) {
return ''
}

return `import ${importName} from '${getRelativePath(sourceDir, targetPath)}'`
}

0 comments on commit 8ca4194

Please sign in to comment.