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: 6 additions & 6 deletions lib/codegen/fromcto/graphql/graphqlvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class GraphQLVisitor {
* @private
*/
visitEnumValueDeclaration(enumValueDeclaration, parameters) {
parameters.fileWriter.writeLine(1, enumValueDeclaration.getName());
parameters.fileWriter.writeLine(1, this.toGraphQLName(enumValueDeclaration.getName()));
return null;
}

Expand All @@ -183,10 +183,10 @@ class GraphQLVisitor {
const keyType = mapDeclaration.getKey().getType();
const valueType = mapDeclaration.getValue().getType();

let key = ModelUtil.isPrimitiveType(keyType) ? this.toGraphQLType(keyType) : keyType;
let value = ModelUtil.isPrimitiveType(valueType) ? this.toGraphQLType(valueType) : valueType;
let key = ModelUtil.isPrimitiveType(keyType) ? this.toGraphQLType(keyType) : this.toGraphQLName(keyType);
let value = ModelUtil.isPrimitiveType(valueType) ? this.toGraphQLType(valueType) : this.toGraphQLName(valueType);

parameters.fileWriter.writeLine(0, `type ${mapDeclaration.getName()} {`);
parameters.fileWriter.writeLine(0, `type ${this.toGraphQLName(mapDeclaration.getName())} {`);
parameters.fileWriter.writeLine(1, `key: ${key}`);
parameters.fileWriter.writeLine(1, `value: ${value}`);
parameters.fileWriter.writeLine(0, '}');
Expand All @@ -212,7 +212,7 @@ class GraphQLVisitor {
type = `${type}!`;
}

parameters.fileWriter.writeLine(1, `${relationship.getName()}: ${type} # ${relationship.getType()}`);
parameters.fileWriter.writeLine(1, `${this.toGraphQLName(relationship.getName())}: ${type} # ${relationship.getType()}`);
return null;
}

Expand Down Expand Up @@ -331,7 +331,7 @@ class GraphQLVisitor {
*/
toGraphQLName(name) {
// $ is unfortunately a restricted character in GraphQL!
return name.replace('$', '_').replace(/\./g, '_');
return name.replace(/\$/g, '_').replace(/\./g, '_');
}
}

Expand Down
102 changes: 102 additions & 0 deletions test/codegen/fromcto/graphql/graphqlvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const chai = require('chai');
chai.should();
const sinon = require('sinon');
const { buildSchema } = require('graphql');

const GraphQLVisitor = require('../../../../lib/codegen/fromcto/graphql/graphqlvisitor.js');

Expand Down Expand Up @@ -424,4 +425,105 @@ ref: String
arrayRef: String) on OBJECT | FIELD_DEFINITION`]);
});
});

describe('names with $', () => {
// $ is not legal in GraphQL names, whatever kind of element it is in
const MODEL_WITH_DOLLARS = `
namespace test@1.0.0

enum Le$ve$l {
o LO$W$X
}

concept Va$lue {
o String x
}

map Look$Up$Map {
o String
o Va$lue
}
Comment thread
aaryanpawar16 marked this conversation as resolved.
Resolved

asset It$em identified by id {
o String id
o String some$field$name
o Le$ve$l lev
o Look$Up$Map lookup
--> It$em par$ent$x optional
}
`;
let previousMapType;
before(() => {
previousMapType = process.env.ENABLE_MAP_TYPE;
process.env.ENABLE_MAP_TYPE = 'true';
});
after(() => {
if (previousMapType === undefined) {
delete process.env.ENABLE_MAP_TYPE;
} else {
process.env.ENABLE_MAP_TYPE = previousMapType;
}
});

/**
* Generates the GraphQL for a model.
* @param {string} cto the Concerto model
* @returns {string[]} the lines written by the visitor
*/
const generate = (cto) => {
const lines = [];
const fileWriter = {
openFile: () => {},
closeFile: () => {},
writeLine: (indent, line) => lines.push(line),
writeBeforeLine: (indent, line) => lines.unshift(line),
};
const modelManager = new ModelManager();
modelManager.addCTOModel(cto);
graphQLVisitor.visit(modelManager, { fileWriter });
return lines;
};

it('should replace every $ so that the generated GraphQL is valid', () => {
const lines = generate(MODEL_WITH_DOLLARS);

const sdl = lines.join('\n');
// comments (e.g. the type of a relationship) keep the Concerto name
lines.map((line) => line.split('#')[0]).join('\n').should.not.contain('$');
sdl.should.contain('enum Le_ve_l {');
sdl.should.contain('LO_W_X');
sdl.should.contain('type Look_Up_Map {');
sdl.should.contain('type It_em ');
sdl.should.contain('some_field_name: String!');
sdl.should.contain('par_ent_x: ID # It$em');
// a map whose value is a declared type refers to it by its escaped name
sdl.should.contain('type Va_lue {');
sdl.should.contain('value: Va_lue');
// graphql-js is the reference parser for GraphQL SDL
(() => buildSchema(sdl)).should.not.throw();
});

it('should replace $ in the name of a non-primitive map key', () => {
const lines = generate(`
namespace test@1.0.0

scalar Ke$y extends String

map Ke$yMap {
o Ke$y
o String
}

concept Holder {
o Ke$yMap m
}
`);

lines.should.include('type Ke_yMap {');
lines.map((line) => line.trim()).should.include('key: Ke_y');
lines.join('\n').should.not.contain('$');
// The SDL is not validated with graphql-js here: scalar declarations are
// not written yet, so a scalar key is an unknown type (see #248).
});
});
});
Loading