Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions reverse_engineering/mappers/directiveName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {Object} params
* @param {String} params.name
* @returns {string} The directive name with "@" prefix
*/
function getDirectiveName({ name }) {
return `@${name}`;
}

module.exports = {
getDirectiveName,
};
5 changes: 3 additions & 2 deletions reverse_engineering/mappers/directiveUsage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { astNodeKind } = require('../constants/graphqlAST');
const { DIRECTIVE_FORMAT, ARGUMENT_VALUE_FORMAT } = require('../constants/properties');
const { getDirectiveName } = require('./directiveName');

/**
* @import { DirectiveNode, ArgumentNode, ValueNode } from "graphql"
Expand All @@ -16,7 +17,7 @@ function mapDirectivesUsage({ directives = [] }) {
return directives.map(directive => {
return {
directiveFormat: DIRECTIVE_FORMAT.structured,
directiveName: directive.name.value,
directiveName: getDirectiveName({ name: directive.name.value }),
argumentValueFormat: ARGUMENT_VALUE_FORMAT.raw,
rawArgumentValues: getRawArguments({ argumentNodes: directive.arguments }),
};
Expand All @@ -39,7 +40,7 @@ function getRawArguments({ argumentNodes = [] }) {
* @returns {string} The string representation of the value
*/
function getArgumentValue(value) {
switch (value.astNodeKind) {
switch (value.kind) {
case astNodeKind.INT:
case astNodeKind.FLOAT:
return value.value;
Expand Down
35 changes: 35 additions & 0 deletions reverse_engineering/mappers/typeDefinitions/customScalar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @import { ScalarTypeDefinitionNode } from "graphql"
* @import { CustomScalarDefinition } from "../../types/types"
*/

const { mapDirectivesUsage } = require('../directiveUsage');

/**
* Maps the custom scalar type definitions
* @param {Object} params
* @param {ScalarTypeDefinitionNode[]} params.customScalars - The custom scalars
* @returns {CustomScalarDefinition[]} The mapped custom scalar type definitions
*/
function getCustomScalarTypeDefinitions({ customScalars = [] }) {
return customScalars.map(scalar => mapCustomScalar({ scalar }));
}

/**
* Maps a single custom scalar definition
* @param {Object} params
* @param {ScalarTypeDefinitionNode} params.scalar - The scalar to map
* @returns {CustomScalarDefinition} The mapped custom scalar definition
*/
function mapCustomScalar({ scalar }) {
return {
type: 'scalar',
name: scalar.name.value,
description: scalar.description?.value || '',
typeDirectives: mapDirectivesUsage({ directives: scalar.directives }),
};
}

module.exports = {
getCustomScalarTypeDefinitions,
};
4 changes: 3 additions & 1 deletion reverse_engineering/mappers/typeDefinitions/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @import { DirectiveDefinition } from "../../types/types"
*/

const { getDirectiveName } = require('../directiveName');

const locationMap = {
'SCHEMA': 'schema',
'QUERY': 'query',
Expand Down Expand Up @@ -48,7 +50,7 @@ function mapDirective({ directive }) {

return {
type: 'directive',
name: directive.name.value,
name: getDirectiveName({ name: directive.name.value }),
description: directive.description?.value || '',
arguments: [], // TODO: implement argument mapping
directiveLocations: locations,
Expand Down
16 changes: 13 additions & 3 deletions reverse_engineering/mappers/typeDefinitions/typeDefinitions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { astNodeKind } = require('../../constants/graphqlAST');
const { findNodesByKind } = require('../../helpers/findNodesByKind');
const { sortByName } = require('../../helpers/sortByName');
const { getCustomScalarTypeDefinitions } = require('./customScalar');
const { getDirectiveTypeDefinitions } = require('./directive');

/**
* @import { DirectiveDefinition, FieldsOrder } from "../../types/types"
* @import { FieldsOrder, DirectiveDefinition, CustomScalarDefinition } from "../../types/types"
*/

/**
Expand All @@ -18,8 +19,11 @@ function getTypeDefinitions({ typeDefinitions, fieldsOrder }) {
const directives = getDirectiveTypeDefinitions({
directives: findNodesByKind({ nodes: typeDefinitions, kind: astNodeKind.DIRECTIVE_DEFINITION }),
});
const customScalars = getCustomScalarTypeDefinitions({
customScalars: findNodesByKind({ nodes: typeDefinitions, kind: astNodeKind.SCALAR_TYPE_DEFINITION }),
});

const definitions = getTypeDefinitionsStructure({ fieldsOrder, directives });
const definitions = getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars });

return definitions;
}
Expand All @@ -29,15 +33,21 @@ function getTypeDefinitions({ typeDefinitions, fieldsOrder }) {
* @param {Object} params
* @param {FieldsOrder} params.fieldsOrder - The fields order
* @param {DirectiveDefinition[]} params.directives - The directive definitions
* @param {CustomScalarDefinition[]} params.customScalars - The custom scalar definitions
* @returns {Object} The type definitions structure
*/
function getTypeDefinitionsStructure({ fieldsOrder, directives }) {
function getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars }) {
const definitions = {
['Directives']: getDefinitionCategoryStructure({
fieldsOrder,
subtype: 'directive',
properties: directives,
}),
['Scalars']: getDefinitionCategoryStructure({
fieldsOrder,
subtype: 'scalar',
properties: customScalars,
}),
};

return {
Expand Down
7 changes: 7 additions & 0 deletions reverse_engineering/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,12 @@ export type DirectiveDefinition = {
directiveLocations: DirectiveLocations;
}

export type CustomScalarDefinition = {
type: 'scalar';
name: string;
description?: string;
typeDirectives?: DirectiveUsage[];
}

export type TestConnectionCallback = (err: Error | null) => void;
export type DisconnectCallback = TestConnectionCallback;
28 changes: 14 additions & 14 deletions test/reverse_engineering/mappers/directiveUsage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('mapDirectivesUsage', () => {
const expected = [
{
directiveFormat: 'Structured',
directiveName: 'deprecated',
directiveName: '@deprecated',
argumentValueFormat: 'Raw',
rawArgumentValues: '',
},
Expand All @@ -34,23 +34,23 @@ describe('mapDirectivesUsage', () => {
arguments: [
{
name: { value: 'intArg' },
value: { astNodeKind: astNodeKind.INT, value: '42' },
value: { kind: astNodeKind.INT, value: '42' },
},
{
name: { value: 'stringArg' },
value: { astNodeKind: astNodeKind.STRING, value: 'hello' },
value: { kind: astNodeKind.STRING, value: 'hello' },
},
{
name: { value: 'boolArg' },
value: { astNodeKind: astNodeKind.BOOLEAN, value: true },
value: { kind: astNodeKind.BOOLEAN, value: true },
},
],
};

const expected = [
{
directiveFormat: 'Structured',
directiveName: 'test',
directiveName: '@test',
argumentValueFormat: 'Raw',
rawArgumentValues: 'intArg: 42, stringArg: "hello", boolArg: true',
},
Expand All @@ -67,21 +67,21 @@ describe('mapDirectivesUsage', () => {
{
name: { value: 'listArg' },
value: {
astNodeKind: astNodeKind.LIST,
kind: astNodeKind.LIST,
values: [
{ astNodeKind: astNodeKind.INT, value: '1' },
{ astNodeKind: astNodeKind.INT, value: '2' },
{ kind: astNodeKind.INT, value: '1' },
{ kind: astNodeKind.INT, value: '2' },
],
},
},
{
name: { value: 'objectArg' },
value: {
astNodeKind: astNodeKind.OBJECT,
kind: astNodeKind.OBJECT,
fields: [
{
name: { value: 'field1' },
value: { astNodeKind: astNodeKind.STRING, value: 'value1' },
value: { kind: astNodeKind.STRING, value: 'value1' },
},
],
},
Expand All @@ -92,7 +92,7 @@ describe('mapDirectivesUsage', () => {
const expected = [
{
directiveFormat: 'Structured',
directiveName: 'complex',
directiveName: '@complex',
argumentValueFormat: 'Raw',
rawArgumentValues: 'listArg: [1, 2], objectArg: {field1: "value1"}',
},
Expand All @@ -108,19 +108,19 @@ describe('mapDirectivesUsage', () => {
arguments: [
{
name: { value: 'varArg' },
value: { astNodeKind: astNodeKind.VARIABLE, name: { value: 'var' } },
value: { kind: astNodeKind.VARIABLE, name: { value: 'var' } },
},
{
name: { value: 'enumArg' },
value: { astNodeKind: astNodeKind.ENUM, value: 'ENUM_VALUE' },
value: { kind: astNodeKind.ENUM, value: 'ENUM_VALUE' },
},
],
};

const expected = [
{
directiveFormat: 'Structured',
directiveName: 'test',
directiveName: '@test',
argumentValueFormat: 'Raw',
rawArgumentValues: 'varArg: $var, enumArg: ENUM_VALUE',
},
Expand Down
Loading