Skip to content

Commit

Permalink
feat(map): Codegen for GraphQL Target (#66)
Browse files Browse the repository at this point in the history
* feat(map): add graphqlvisitor

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): update snapshot

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): adds graphqlvisitor

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): restores sandbox

Signed-off-by: Jonathan Casey <[email protected]>

---------

Signed-off-by: Jonathan Casey <[email protected]>
  • Loading branch information
jonathan-casey committed Nov 3, 2023
1 parent bf1743a commit da49245
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/codegen/fromcto/graphql/graphqlvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const util = require('util');
const { ModelUtil } = require('@accordproject/concerto-core');

/**
* Convert the contents of a ModelManager to GraphQL types, based on
Expand Down Expand Up @@ -52,7 +53,7 @@ class GraphQLVisitor {
} else if (thing.isField?.()) {
return this.visitField(thing, parameters);
} else if (thing.isMapDeclaration?.()) {
return;
return this.visitMapDeclaration(thing, parameters);
} else if (thing.isRelationship?.()) {
return this.visitRelationship(thing, parameters);
} else if (thing.isEnumValue?.()) {
Expand Down Expand Up @@ -171,6 +172,28 @@ class GraphQLVisitor {
return null;
}

/**
* Visitor design pattern
* @param {MapDeclaration} mapDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {
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;

parameters.fileWriter.writeLine(0, `type ${mapDeclaration.getName()} {`);
parameters.fileWriter.writeLine(1, `key: ${key}`);
parameters.fileWriter.writeLine(1, `value: ${value}`);
parameters.fileWriter.writeLine(0, '}');

return null;
}

/**
* Visitor design pattern
* @param {Relationship} relationship - the object being visited
Expand Down
48 changes: 48 additions & 0 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,30 @@ type Address {
dictionary5: Map5!
dictionary6: Map6!
}
type Map1 {
key: String
value: String
}
type Map2 {
key: String
value: DateTime
}
type Map3 {
key: String
value: SSN
}
type Map4 {
key: String
value: Concept
}
type Map5 {
key: SSN
value: String
}
type Map6 {
key: SSN
value: Employee
}
type Company {
name: String!
headquarters: Address!
Expand Down Expand Up @@ -5758,6 +5782,30 @@ type Address {
dictionary5: Map5!
dictionary6: Map6!
}
type Map1 {
key: String
value: String
}
type Map2 {
key: String
value: DateTime
}
type Map3 {
key: String
value: SSN
}
type Map4 {
key: String
value: Concept
}
type Map5 {
key: SSN
value: String
}
type Map6 {
key: SSN
value: Employee
}
type Company {
name: String!
headquarters: Address!
Expand Down
35 changes: 35 additions & 0 deletions test/codegen/fromcto/graphql/graphqlvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ const GraphQLVisitor = require('../../../../lib/codegen/fromcto/graphql/graphqlv
const ModelFile = require('@accordproject/concerto-core').ModelFile;
const ModelManager = require('@accordproject/concerto-core').ModelManager;
const ClassDeclaration = require('@accordproject/concerto-core').ClassDeclaration;
const MapDeclaration = require('@accordproject/concerto-core').MapDeclaration;
const EnumValueDeclaration = require('@accordproject/concerto-core').EnumValueDeclaration;
const Field = require('@accordproject/concerto-core').Field;
const RelationshipDeclaration = require('@accordproject/concerto-core').RelationshipDeclaration;
const FileWriter = require('@accordproject/concerto-util').FileWriter;
const { ModelUtil } = require('@accordproject/concerto-core');

let sandbox = sinon.createSandbox();

const MODEL_WITH_DECORATORS = `
namespace test
Expand Down Expand Up @@ -339,6 +343,37 @@ describe('GraphQLVisitor', function () {
});
});

describe('visitMapDeclaration', () => {
it('should write a type for a Map', () => {
let param = {
fileWriter: mockFileWriter
};

sandbox.stub(ModelUtil, 'isPrimitiveType').callsFake(() => {
return true;
});

const mockMapDeclaration = sinon.createStubInstance(MapDeclaration);
const getKeyType = sinon.stub();
const getValueType = sinon.stub();

getKeyType.returns('String');
getValueType.returns('String');
mockMapDeclaration.getName.returns('map1');
mockMapDeclaration.getKey.returns({ getType: getKeyType });
mockMapDeclaration.getValue.returns({ getType: getValueType });

graphQLVisitor.visitMapDeclaration(mockMapDeclaration, param);

param.fileWriter.writeLine.withArgs(0, 'type map1 {').calledOnce.should.be.ok;
param.fileWriter.writeLine.withArgs(1, 'key: String').calledOnce.should.be.ok;
param.fileWriter.writeLine.withArgs(1, 'value: String').calledOnce.should.be.ok;
param.fileWriter.writeLine.withArgs(0, '}').calledOnce.should.be.ok;

sandbox.restore();
});
});

describe('visitRelationship', () => {
it('should write a line for a relationship', () => {
let param = {
Expand Down

0 comments on commit da49245

Please sign in to comment.