Skip to content

Commit

Permalink
+ track output
Browse files Browse the repository at this point in the history
... for github action  to include it the package
+ expect String Kind
+ rejection test
+ version
  • Loading branch information
Mr D committed Jul 2, 2020
1 parent d2ded7e commit e474a16
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
**/.vscode/*
**/node_modules/*
**/coverage/*
**/cjs/*
*.tgz
*.zip
*.tar.gz
Expand Down
3 changes: 3 additions & 0 deletions cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare var _default: GraphQLScalarType;
export default _default;
import { GraphQLScalarType } from "graphql";
27 changes: 27 additions & 0 deletions cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const language_1 = require("graphql/language");
const json5_1 = __importDefault(require("json5"));
/** */
exports.default = new graphql_1.GraphQLScalarType({
name: "JSON5",
description: "`JSON5` scalar type, see [JSON5](https://spec.json5.org).",
serialize: json5_1.default.stringify,
parseValue: json5_1.default.parse,
/**
* @param {import("graphql").ASTNode | any} ast
*/
parseLiteral: (ast, variables) => {
switch (ast.kind) {
case language_1.Kind.VARIABLE:
return ((variables && json5_1.default.parse(variables[ast.name.value])) || undefined);
default: {
return json5_1.default.parse(ast.value);
}
}
},
});
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLScalarType } from "graphql";
import { Kind } from "graphql/language";
import { GraphQLScalarType, Kind, GraphQLError } from "graphql";
import JSON5 from "json5";
/** */
export default new GraphQLScalarType({
Expand All @@ -8,17 +7,20 @@ export default new GraphQLScalarType({
serialize: JSON5.stringify,
parseValue: JSON5.parse,
/**
* @param {import("graphql").ASTNode | any} ast
* @param {import("graphql").ASTNode} ast
*/
parseLiteral: (ast, variables) => {
switch (ast.kind) {
case Kind.VARIABLE:
return (
(variables && JSON5.parse(variables[ast.name.value])) || undefined
);
default: {
case Kind.STRING:
return JSON5.parse(ast.value);
}
default:
throw new GraphQLError(
`Expected '${Kind.STRING}' but got '${ast.kind}'`,
);
}
},
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@d10221/graphql-scalar-json5",
"version": "0.1.0-7",
"version": "0.1.0-8",
"description": "GraphQL JSON5 scalar type",
"main": "cjs/index.js",
"module": "index.js",
Expand Down
19 changes: 18 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { graphql, GraphQLObjectType, GraphQLSchema } from "graphql";
import {
graphql,
GraphQLObjectType,
GraphQLSchema,
Kind,
GraphQLError,
} from "graphql";
import { makeExecutableSchema } from "graphql-tools";
import JSON5 from "../";
import json5 from "json5";
Expand Down Expand Up @@ -68,4 +74,15 @@ describe("JSON", () => {
data: { value: json5.stringify({ x: "y" }) },
});
});
it("rejects what json5 can't parse", async () => {
const { errors } = await graphql(
schema1,
/* GraphQL */ `
query {
value(params: {})
}
`,
);
expect(errors[0]).toBeInstanceOf(GraphQLError);
});
});

0 comments on commit e474a16

Please sign in to comment.