Skip to content

Commit

Permalink
enable typescript services and sdl
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Reidy committed May 5, 2020
1 parent 529f72a commit e566c56
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
14 changes: 8 additions & 6 deletions packages/cli/src/commands/generate/sdl/sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const sdlFromSchemaModel = async (name) => {
}
}

export const files = async ({ name, crud }) => {
export const files = async ({ name, crud, typescript }) => {
const {
query,
createInput,
Expand All @@ -115,8 +115,9 @@ export const files = async ({ name, crud }) => {
enums,
} = await sdlFromSchemaModel(pascalcase(pluralize.singular(name)))

const extension = typescript ? 'ts' : 'js'
const template = generateTemplate(
path.join('sdl', 'templates', 'sdl.js.template'),
path.join('sdl', 'templates', `sdl.${extension}.template`),
{
name,
crud,
Expand All @@ -130,11 +131,11 @@ export const files = async ({ name, crud }) => {

const outputPath = path.join(
getPaths().api.graphql,
`${camelcase(pluralize(name))}.sdl.js`
`${camelcase(pluralize(name))}.sdl.${extension}`
)
return {
[outputPath]: template,
...(await serviceFiles({ name, crud, relations })),
...(await serviceFiles({ name, crud, relations, typescript })),
}
}

Expand All @@ -144,15 +145,16 @@ export const builder = {
services: { type: 'boolean', default: true },
crud: { type: 'boolean', default: false },
force: { type: 'boolean', default: false },
typescript: { type: 'boolean', default: false },
}
// TODO: Add --dry-run command
export const handler = async ({ model, crud, force }) => {
export const handler = async ({ model, crud, force, typescript }) => {
const tasks = new Listr(
[
{
title: 'Generating SDL files...',
task: async () => {
const f = await files({ name: model, crud })
const f = await files({ name: model, crud, typescript })
return writeFilesTask(f, { overwriteExisting: force })
},
},
Expand Down
28 changes: 28 additions & 0 deletions packages/cli/src/commands/generate/sdl/templates/sdl.ts.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const schema = gql`
type ${singularPascalName} {
${query}
}
<% if (enums.length > 0) {%>
<% enums.forEach((enumDef, idx)=> {%> enum ${enums[idx].name} {<% enums[idx].values.forEach((enumDefValue, idk)=> { %>
${enums[idx].values[idk].name}<% }) %>
}
<%}) %><% } %>
type Query {
${pluralCamelName}: [${singularPascalName}!]!<% if (crud) { %>
${singularCamelName}(id: ${idType}!): ${singularPascalName}!<% } %>
}

input Create${singularPascalName}Input {
${createInput}
}

input Update${singularPascalName}Input {
${updateInput}
}<% if (crud) { %>

type Mutation {
create${singularPascalName}(input: Create${singularPascalName}Input!): ${singularPascalName}!
update${singularPascalName}(id: ${idType}!, input: Update${singularPascalName}Input!): ${singularPascalName}!
delete${singularPascalName}(id: ${idType}!): ${singularPascalName}!
}<% } %>
`
9 changes: 6 additions & 3 deletions packages/cli/src/commands/generate/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import {

export const files = async ({ name, relations, ...rest }) => {
const componentName = camelcase(pluralize(name))
const extension = rest.typescript ? 'ts' : 'js'
const serviceFile = templateForComponentFile({
name,
componentName: componentName,
extension: `.${extension}`,
apiPathSection: 'services',
generator: 'service',
templatePath: 'service.js.template',
templatePath: `service.${extension}.template`,
templateVars: { relations: relations || [], ...rest },
})
const testFile = templateForComponentFile({
name,
componentName: componentName,
extension: '.test.js',
extension: `.test.${extension}`,
apiPathSection: 'services',
generator: 'service',
templatePath: 'test.js.template',
templatePath: `test.${extension}.template`,
templateVars: { relations: relations || [], ...rest },
})

Expand All @@ -42,6 +44,7 @@ export const files = async ({ name, relations, ...rest }) => {
export const builder = {
crud: { type: 'boolean', default: false, desc: 'Create CRUD functions' },
force: { type: 'boolean', default: false },
typescript: { type: 'boolean', default: false },
}

export const { command, desc, handler } = createYargsForComponentGeneration({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { db } from 'src/lib/db'

export const ${pluralCamelName} = () => {
return db.${singularCamelName}.findMany()
}<% if (crud) { %>

export const ${singularCamelName} = ({ id }) => {
return db.${singularCamelName}.findOne({
where: { id },
})
}

export const create${singularPascalName} = ({ input }) => {
return db.${singularCamelName}.create({
data: input,
})
}

export const update${singularPascalName} = ({ id, input }) => {
return db.${singularCamelName}.update({
data: input,
where: { id },
})
}

export const delete${singularPascalName} = ({ id }) => {
return db.${singularCamelName}.delete({
where: { id },
})
}<% } %><% if (relations.length) { %>

export const ${singularPascalName} = {<% relations.forEach(relation => { %>
${relation}: (_obj, { root }) => db.${singularCamelName}.findOne({ where: { id: root.id } }).${relation}(),<% }) %>
}<% } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ${pluralCamelName} } from './${pluralCamelName}'

describe('${pluralCamelName}', () => {
it('returns true', () => {
expect(true).toBe(true)
})
})
6 changes: 5 additions & 1 deletion packages/dev-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const purgeRequireCache = (): void => {

const requireLambdaFunctions = (path: string): { [path: string]: any } => {
// @ts-ignore ; requireDir is outdated.
return requireDir(path, { recurse: false, extensions: ['.js', '.ts'] })
return requireDir(path, {
recurse: false,
extensions: ['.js', '.ts'],
filter: (path: string) => path.match(/\.test\.[jt]s/),
})
}

const app = express()
Expand Down

0 comments on commit e566c56

Please sign in to comment.