Skip to content
Draft
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
36 changes: 35 additions & 1 deletion site/gatsby-site/server/scalars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLScalarType, Kind } from "graphql";
import { GraphQLScalarType, Kind, ValueNode } from "graphql";
import { ObjectId } from "mongodb";

export const ObjectIdScalar = new GraphQLScalarType({
Expand All @@ -23,4 +23,38 @@

return null;
},
});

function parseLiteralValue(ast: ValueNode): any {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value;
case Kind.INT:
return parseInt(ast.value, 10);
case Kind.FLOAT:
return parseFloat(ast.value);
case Kind.LIST:
return ast.values.map(parseLiteralValue);
case Kind.OBJECT:
return Object.fromEntries(ast.fields.map(f => [f.name.value, parseLiteralValue(f.value)]));
case Kind.NULL:
return null;
default:
return null;
}
}

Check warning on line 46 in site/gatsby-site/server/scalars.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/scalars.ts#L28-L46

Added lines #L28 - L46 were not covered by tests

export const AttributeValueScalar = new GraphQLScalarType({
name: 'AttributeValue',
description: 'Value of classification attribute as typed data',
serialize(value: unknown) {
return value;
},
parseValue(value: unknown) {
return value;

Check warning on line 55 in site/gatsby-site/server/scalars.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/scalars.ts#L55

Added line #L55 was not covered by tests
},
parseLiteral(ast) {
return parseLiteralValue(ast);

Check warning on line 58 in site/gatsby-site/server/scalars.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/scalars.ts#L58

Added line #L58 was not covered by tests
},
});
29 changes: 27 additions & 2 deletions site/gatsby-site/server/tests/fixtures/classifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const classification1: DBClassification = {
attributes: [
{
short_name: "Harm Distribution Basis",
value_json: "[]"
value_json: "[\"sexual orientation or gender identity\"]"
},
{
short_name: "Sector of Deployment",
Expand All @@ -132,7 +132,7 @@ const classification2: DBClassification = {
attributes: [
{
short_name: "Harm Distribution Basis",
value_json: "[]"
value_json: "[\"veteran status\"]"
},
{
short_name: "Sector of Deployment",
Expand Down Expand Up @@ -272,6 +272,7 @@ const fixture: Fixture<Classification, any, ClassificationInsertType> = {
attributes {
short_name
value_json
value
}
namespace
notes
Expand Down Expand Up @@ -317,6 +318,18 @@ const fixture: Fixture<Classification, any, ClassificationInsertType> = {
namespace: "CSETv1",
reports: [{ report_number: 1 }],
incidents: [{ incident_id: 1 }],
attributes: [
{
short_name: "Harm Distribution Basis",
value: ["sexual orientation or gender identity"],
value_json: "[\"sexual orientation or gender identity\"]",
} as any,
{
short_name: "Sector of Deployment",
value: [],
value_json: "[]",
} as any,
]
},
},
testPluralFilter: {
Expand All @@ -331,6 +344,18 @@ const fixture: Fixture<Classification, any, ClassificationInsertType> = {
namespace: "CSETv1",
reports: [{ report_number: 1 }],
incidents: [{ incident_id: 1 }],
attributes: [
{
short_name: "Harm Distribution Basis",
value: ["sexual orientation or gender identity"],
value_json: "[\"sexual orientation or gender identity\"]",
} as any,
{
short_name: "Sector of Deployment",
value: [],
value_json: "[]",
} as any,
]
}
]
},
Expand Down
17 changes: 15 additions & 2 deletions site/gatsby-site/server/types/classification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLObjectType, GraphQLString, GraphQLBoolean, GraphQLList, GraphQLInt, GraphQLNonNull, GraphQLID } from 'graphql';
import { ObjectIdScalar } from '../scalars';
import { ObjectIdScalar, AttributeValueScalar } from '../scalars';
import { IncidentType } from './incidents';
import { getListRelationshipConfig } from '../utils';
import { ReportType } from './report';
Expand All @@ -8,10 +8,23 @@
name: 'Attribute',
fields: {
short_name: { type: GraphQLString },
value_json: { type: GraphQLString }
value_json: { type: GraphQLString },
value: {
type: AttributeValueScalar,
resolve: (source: any) => {
try {
return JSON.parse(source.value_json);
} catch (e) {
return source.value_json;
}

Check warning on line 19 in site/gatsby-site/server/types/classification.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/types/classification.ts#L18-L19

Added lines #L18 - L19 were not covered by tests
}
}
}
});

//@ts-ignore
AttributeType.getFields().value.dependencies = ['value_json'];

export const ClassificationType = new GraphQLObjectType({
name: 'Classification',
fields: {
Expand Down