Skip to content

Commit

Permalink
feat: scalars functionality (#727)
Browse files Browse the repository at this point in the history
Pozdro @dweller23
  • Loading branch information
lgandecki authored Jan 26, 2021
1 parent 88530be commit 3f5afa2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
25 changes: 25 additions & 0 deletions generateModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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';
Expand All @@ -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`;
Expand Down
5 changes: 5 additions & 0 deletions helpers/saveRenderedTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions parse-graphql/getScalars.js
Original file line number Diff line number Diff line change
@@ -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);
};
27 changes: 27 additions & 0 deletions parse-graphql/getScalars.spec.js
Original file line number Diff line number Diff line change
@@ -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']);
});
4 changes: 4 additions & 0 deletions templates/moduleResolvers.handlebars
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}}

};
8 changes: 8 additions & 0 deletions templates/scalarResolver.handlebars
Original file line number Diff line number Diff line change
@@ -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,
});

0 comments on commit 3f5afa2

Please sign in to comment.