Skip to content

Commit

Permalink
feat(compat): enable lower case Json/Uuid like v3 (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Feb 27, 2018
1 parent 4cd8208 commit 20a084f
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 29 deletions.
1 change: 0 additions & 1 deletion packages/graphile-build-pg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"debug": ">=2 <3",
"graphile-build": "4.0.0-alpha.1",
"graphql-iso-date": "^3.2.0",
"graphql-type-json": "^0.1.4",
"jsonwebtoken": "^8.1.1",
"lodash": ">=4 <5",
"lru-cache": "4.1.1",
Expand Down
82 changes: 82 additions & 0 deletions packages/graphile-build-pg/src/GraphQLJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This file is based on
// https://github.com/taion/graphql-type-json/blob/6e45ae4ee0a60f8f3565c8c980a82c7d9b98d3f5/src/index.js
//
// It only exists here (rather than using `graphql-type-json` directly) because
// we need to export Json along with JSON.
//
/*
The MIT License (MIT)
Copyright (c) 2016 Jimmy Jia
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import { GraphQLScalarType } from "graphql";
import { Kind } from "graphql/language";

function identity(value) {
return value;
}

function parseLiteral(ast) {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value;
case Kind.INT:
case Kind.FLOAT:
return parseFloat(ast.value);
case Kind.OBJECT: {
const value = Object.create(null);
ast.fields.forEach(field => {
value[field.name.value] = parseLiteral(field.value);
});

return value;
}
case Kind.LIST:
return ast.values.map(parseLiteral);
case Kind.NULL:
return null;
default:
return undefined;
}
}

export const GraphQLJSON = new GraphQLScalarType({
name: "JSON",
description:
"The `JSON` scalar type represents JSON values as specified by " +
"[ECMA-404](http://www.ecma-international.org/" +
"publications/files/ECMA-ST/ECMA-404.pdf).",
serialize: identity,
parseValue: identity,
parseLiteral,
});

export const GraphQLJson = new GraphQLScalarType({
name: "Json",
description:
"The `Json` scalar type represents JSON values as specified by " +
"[ECMA-404](http://www.ecma-international.org/" +
"publications/files/ECMA-ST/ECMA-404.pdf).",
serialize: identity,
parseValue: identity,
parseLiteral,
});
17 changes: 10 additions & 7 deletions packages/graphile-build-pg/src/plugins/PgTypesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
import { Kind } from "graphql/language";
import { types as pgTypes } from "pg";

import GraphQLJSON from "graphql-type-json";
import { GraphQLJSON, GraphQLJson } from "../GraphQLJSON";

import rawParseInterval from "postgres-interval";
import LRU from "lru-cache";

function indent(str) {
return " " + str.replace(/\n/g, "\n ");
Expand All @@ -36,8 +39,6 @@ const stringType = (name, description) =>
return ast.value;
},
});
import rawParseInterval from "postgres-interval";
import LRU from "lru-cache";
/*
const {
GraphQLDate,
Expand Down Expand Up @@ -97,7 +98,7 @@ const pgRangeParser = {

export default (function PgTypesPlugin(
builder,
{ pgExtendedTypes = true, pgInflection: inflection }
{ pgExtendedTypes = true, pgInflection: inflection, pgLegacyJsonUuid = false }
) {
// XXX: most of this should be in an "init" hook, not a "build" hook
builder.hook("build", build => {
Expand Down Expand Up @@ -325,16 +326,18 @@ export default (function PgTypesPlugin(
"The exact time of day, does not include the date. May or may not have a timezone offset."
);
const SimpleJSON = stringType(
"JSON",
pgLegacyJsonUuid ? "Json" : "JSON",
"A JavaScript object encoded in the JSON format as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)."
);
const SimpleUUID = stringType(
"UUID",
pgLegacyJsonUuid ? "Uuid" : "UUID",
"A universally unique identifier as defined by [RFC 4122](https://tools.ietf.org/html/rfc4122)."
);

// pgExtendedTypes might change what types we use for things
const JSONType = pgExtendedTypes ? GraphQLJSON : SimpleJSON;
const JSONType = pgExtendedTypes
? pgLegacyJsonUuid ? GraphQLJson : GraphQLJSON
: SimpleJSON;
const UUIDType = SimpleUUID; // GraphQLUUID
const DateType = SimpleDate; // GraphQLDate
const DateTimeType = SimpleDatetime; // GraphQLDateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12294,7 +12294,7 @@ input WrappedUrlInput {
"
`;

exports[`prints a schema without legacy relations 1`] = `
exports[`prints a schema without new relations and with legacy type names 1`] = `
"enum AnEnum {
_ASTERISK_BAR_
_ASTERISK_BAZ_ASTERISK_
Expand Down Expand Up @@ -12422,7 +12422,7 @@ type CompoundType {
b: String
c: Color
computedField: Int
d: UUID
d: Uuid
e: EnumCaps
f: EnumWithEmptyString
fooBar: Int
Expand Down Expand Up @@ -13035,7 +13035,7 @@ type IntSetQueryEdge {
\\"\\"\\"
A JavaScript object encoded in the JSON format as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
\\"\\"\\"
scalar JSON
scalar Json

\\"\\"\\"All input for the \`jsonbIdentityMutation\` mutation.\\"\\"\\"
input JsonbIdentityMutationInput {
Expand All @@ -13044,7 +13044,7 @@ input JsonbIdentityMutationInput {
payload verbatim. May be used to track mutations by the client.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json
}

\\"\\"\\"The output of our \`jsonbIdentityMutation\` mutation.\\"\\"\\"
Expand All @@ -13054,7 +13054,7 @@ type JsonbIdentityMutationPayload {
unchanged and unused. May be used by a client to track mutations.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
Expand All @@ -13064,7 +13064,7 @@ type JsonbIdentityMutationPayload {

\\"\\"\\"All input for the \`jsonbIdentityMutationPlpgsql\` mutation.\\"\\"\\"
input JsonbIdentityMutationPlpgsqlInput {
_theJson: JSON!
_theJson: Json!

\\"\\"\\"
An arbitrary string value with no semantic meaning. Will be included in the
Expand All @@ -13080,7 +13080,7 @@ type JsonbIdentityMutationPlpgsqlPayload {
unchanged and unused. May be used by a client to track mutations.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
Expand All @@ -13090,7 +13090,7 @@ type JsonbIdentityMutationPlpgsqlPayload {

\\"\\"\\"All input for the \`jsonbIdentityMutationPlpgsqlWithDefault\` mutation.\\"\\"\\"
input JsonbIdentityMutationPlpgsqlWithDefaultInput {
_theJson: JSON
_theJson: Json

\\"\\"\\"
An arbitrary string value with no semantic meaning. Will be included in the
Expand All @@ -13106,7 +13106,7 @@ type JsonbIdentityMutationPlpgsqlWithDefaultPayload {
unchanged and unused. May be used by a client to track mutations.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
Expand All @@ -13121,7 +13121,7 @@ input JsonIdentityMutationInput {
payload verbatim. May be used to track mutations by the client.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json
}

\\"\\"\\"The output of our \`jsonIdentityMutation\` mutation.\\"\\"\\"
Expand All @@ -13131,7 +13131,7 @@ type JsonIdentityMutationPayload {
unchanged and unused. May be used by a client to track mutations.
\\"\\"\\"
clientMutationId: String
json: JSON
json: Json

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
Expand Down Expand Up @@ -14112,8 +14112,8 @@ type Query implements Node {
y: Int
z: Int
): IntSetQueryConnection!
jsonIdentity(json: JSON): JSON
jsonbIdentity(json: JSON): JSON
jsonIdentity(json: Json): Json
jsonbIdentity(json: Json): Json

\\"\\"\\"Reads a single \`LeftArm\` using its globally unique \`ID\`.\\"\\"\\"
leftArm(
Expand Down Expand Up @@ -14179,7 +14179,7 @@ type Query implements Node {
\\"\\"\\"
offset: Int
): PeopleConnection!
typesQuery(a: BigInt!, b: Boolean!, c: String!, d: [Int]!, e: JSON!, f: FloatRangeInput!): Boolean
typesQuery(a: BigInt!, b: Boolean!, c: String!, d: [Int]!, e: Json!, f: FloatRangeInput!): Boolean
}

\\"\\"\\"All input for the \`tableMutation\` mutation.\\"\\"\\"
Expand Down Expand Up @@ -14249,7 +14249,7 @@ input TypesMutationInput {
\\"\\"\\"
clientMutationId: String
d: [Int]!
e: JSON!
e: Json!
f: FloatRangeInput!
}

Expand Down Expand Up @@ -14547,7 +14547,7 @@ type UpdatePersonSecretPayload {
\\"\\"\\"
A universally unique identifier as defined by [RFC 4122](https://tools.ietf.org/html/rfc4122).
\\"\\"\\"
scalar UUID
scalar Uuid

type WrappedUrl {
url: NotNullUrl!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ const testFixtures = [
}),
},
{
name: "prints a schema without legacy relations", // will be default in v5
name: "prints a schema without new relations and with legacy type names",
createSchema: client =>
createPostGraphQLSchema(client, "c", {
legacyRelations: "only",
enableTags: false,
legacyJsonUuid: true,
}),
},
];
Expand Down
3 changes: 3 additions & 0 deletions packages/postgraphile-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type PostGraphQLOptions = {
setWriteCacheCallback?: (fn: () => Promise<void>) => void,
legacyRelations?: "only" | "deprecated",
badlyBehavedFunctions?: boolean,
legacyJsonUuid?: boolean,
};

type PgConfig = Client | Pool | string;
Expand Down Expand Up @@ -108,6 +109,7 @@ const getPostGraphQLBuilder = async (
setWriteCacheCallback,
legacyRelations = "deprecated", // TODO: Change to 'omit' in v5
badlyBehavedFunctions = false,
legacyJsonUuid = false,
} = options;

if (
Expand Down Expand Up @@ -218,6 +220,7 @@ const getPostGraphQLBuilder = async (
pgViewUniqueKey: viewUniqueKey,
pgEnableTags: enableTags,
pgLegacyRelations: legacyRelations,
pgLegacyJsonUuid: legacyJsonUuid,
persistentMemoizeWithKey,
pgForbidSetofFunctionsToReturnNull: !badlyBehavedFunctions,
},
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2311,10 +2311,6 @@ graphql-iso-date@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/graphql-iso-date/-/graphql-iso-date-3.5.0.tgz#55a1be0efa8d28c1453afd2eb5ce1d052189a513"

graphql-type-json@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.1.4.tgz#89f13f5d32ce08c9a76c79fdf9c1968384d81a4e"

"graphql@>=0.9 <0.14":
version "0.13.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.13.1.tgz#9b3db3d8e40d1827e4172404bfdd2e4e17a58b55"
Expand Down

0 comments on commit 20a084f

Please sign in to comment.