diff --git a/generateModule.js b/generateModule.js index 4510f2d2..5198b602 100755 --- a/generateModule.js +++ b/generateModule.js @@ -9,6 +9,7 @@ const getModuleInfos = require('./parse-graphql/getModuleInfos'); const getModuleNames = require('./parse-graphql/getModuleNames'); const getFederatedEntities = require('./parse-graphql/getFederatedEntities'); const getInterfaces = require('./parse-graphql/getInterfaces'); +const getScalars = require('./parse-graphql/getScalars'); // const checkIfGitStateClean = require('./helpers/checkIfGitStateClean'); const saveRenderedTemplate = require('./helpers/saveRenderedTemplate'); @@ -405,6 +406,29 @@ const execute = (appPrefix = '@app', generatedPrefix = '@generated', modulesPath } }); } + + const scalars = getScalars(schemaString); + + const createScalarResolvers = () => { + if (scalars && scalars.length) { + shelljs.mkdir('-p', `${projectMainPath}/src/${graphqlFileRootPath}/scalars/`); + } + scalars.forEach((scalarName) => { + const templateName = './templates/scalarResolver.handlebars'; + const context = { + scalarName, + moduleName: name, + generatedPrefix, + }; + const filePath = `${projectMainPath}/src/${graphqlFileRootPath}/scalars/`; + const fileName = `${scalarName}.ts`; + const keepIfExists = true; + + saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists); + }); + }; + createScalarResolvers(); + const moduleName = name; const createModuleResolvers = () => { const templateName = './templates/moduleResolvers.handlebars'; @@ -415,6 +439,7 @@ const execute = (appPrefix = '@app', generatedPrefix = '@generated', modulesPath typeResolvers, graphqlFileRootPath, appPrefix, + scalars, }; const filePath = `${projectMainPath}/generated/graphql/`; const fileName = `${moduleName}Resolvers.ts`; diff --git a/helpers/saveRenderedTemplate.js b/helpers/saveRenderedTemplate.js index 5fb680a1..31b22c19 100644 --- a/helpers/saveRenderedTemplate.js +++ b/helpers/saveRenderedTemplate.js @@ -4,8 +4,13 @@ const path = require('path'); Handlebars.registerHelper('toUpperCase', function(str) { return str.replace(/^\w/, c => c.toUpperCase()); +}) + +Handlebars.registerHelper('toLowerCase', function(str) { + return str.replace(/^\w/, c => c.toLowerCase()); }); + module.exports = function saveRenderedTemplate (templateName, context, filePath, fileName, keepIfExists = false) { const combinedPath = path.join(filePath, fileName); if (keepIfExists && fs.existsSync(combinedPath)) { diff --git a/parse-graphql/getScalars.js b/parse-graphql/getScalars.js new file mode 100644 index 00000000..2ea83242 --- /dev/null +++ b/parse-graphql/getScalars.js @@ -0,0 +1,8 @@ +const gql = require('graphql-tag'); +module.exports = (graphqlString) => { + const graphqlAST = gql` + ${graphqlString} + `; + + return graphqlAST.definitions.filter((d) => d.kind === 'ScalarTypeDefinition').map((f) => f.name.value); +}; diff --git a/parse-graphql/getScalars.spec.js b/parse-graphql/getScalars.spec.js new file mode 100644 index 00000000..924d9133 --- /dev/null +++ b/parse-graphql/getScalars.spec.js @@ -0,0 +1,27 @@ +const getScalars = require('./getScalars'); +const gql = (a) => a[0]; + +test('get the non extended types with apollo key annotation', () => { + const schemaString = gql` + type TodoItem @key(fields: "id") { + id: ID! + list: List + } + + extend type List { + id: ID! + todos: [TodoItem!]! + incompleteCount: Int! + } + + type InMemory { + id: ID! + } + + scalar MyOwn + `; + + const res = getScalars(schemaString); + + expect(res).toEqual(['MyOwn']); +}); diff --git a/templates/moduleResolvers.handlebars b/templates/moduleResolvers.handlebars index 0e8a8002..610aacea 100644 --- a/templates/moduleResolvers.handlebars +++ b/templates/moduleResolvers.handlebars @@ -1,6 +1,7 @@ import {Resolvers} from "./types"; {{#each queries}}import { {{name}}Query } from "{{../appPrefix}}/{{../graphqlFileRootPath}}queries/{{name}}Query";{{/each}} {{#each mutations}}import { {{name}}Mutation } from "{{../appPrefix}}/{{../graphqlFileRootPath}}mutations/{{name}}Mutation";{{/each}} +{{#each scalars}}import { {{this}} } from "{{../appPrefix}}/{{../graphqlFileRootPath}}scalars/{{this}}";{{/each}} {{#each typeResolvers}}{{#each fieldName}}import { {{../typeName}}{{capitalizedName}} } from "{{../../appPrefix}}/{{../../graphqlFileRootPath}}/types/{{../typeName}}{{capitalizedName}}";{{/each}}{{/each}} // Warning, this file is autogenerated, please don't change it manually. @@ -11,4 +12,7 @@ export const {{moduleName}}Resolvers: Resolvers = { {{typeName}}: { {{#each fieldName}}{{name}}: {{../typeName}}{{capitalizedName}},{{/each}} }, {{/each}} {{#if queries.length}}Query: { {{#each queries}}{{name}}: {{name}}Query,{{/each}} },{{/if}} {{#if mutations.length}}Mutation: { {{#each mutations}}{{name}}: {{name}}Mutation,{{/each}} },{{/if}} +{{#if scalars.length}}// Scalars:{{/if}} + {{#each scalars}}{{this}},{{/each}} + }; diff --git a/templates/scalarResolver.handlebars b/templates/scalarResolver.handlebars new file mode 100644 index 00000000..dbfd0020 --- /dev/null +++ b/templates/scalarResolver.handlebars @@ -0,0 +1,8 @@ +import { GraphQLScalarType } from "graphql"; + +const {{toLowerCase scalarName}}Serialize = (value) => value || null; + +export const {{scalarName}}: GraphQLScalarType = new GraphQLScalarType({ +name: "{{scalarName}}", +serialize: {{toLowerCase scalarName}}Serialize, +});