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 028eee1
Showing 1 changed file with 31 additions and 38 deletions.
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 028eee1

Please sign in to comment.