Skip to content

Commit

Permalink
fix(json-field): switch from json to jsonb (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Apr 25, 2019
1 parent 6def8b9 commit 14aaa83
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/2-complex-example/src/modules/user/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum StringEnum {

@Model()
@Unique(['firstName', 'stringEnumField'])
export class User extends BaseModelUUID {
export class User extends BaseModel {
@StringField({ maxLength: 30 })
firstName?: string;

Expand Down
7 changes: 7 additions & 0 deletions examples/2-complex-example/tools/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ async function seedDatabase() {
const email = `${firstName
.substr(0, 1)
.toLowerCase()}${lastName.toLowerCase()}-${random}@fakeemail.com`;
const jsonField = {
bar: 'hello',
baz: [{}, { one: 3 }],
bool: false,
foo: 1
};

try {
const user = await binding.mutation.createUser(
{
data: {
email,
firstName,
jsonField,
lastName,
stringEnumField: 'FOO'
}
Expand Down
6 changes: 4 additions & 2 deletions src/decorators/JSONField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import GraphQLJSONObject from 'graphql-type-json';
// tslint:disable-next-line:no-var-requires
const { GraphQLJSONObject } = require('graphql-type-json');

import { Field } from 'type-graphql';
import { Column } from 'typeorm';

Expand All @@ -17,7 +19,7 @@ export function JSONField(args: JSONFieldOptions = {}): any {
...nullableOption
}),
Column({
type: 'json',
type: 'jsonb',
...nullableOption
}) as MethodDecoratorFactory
];
Expand Down
7 changes: 5 additions & 2 deletions src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ export class SchemaGenerator {
// will be re-written. If you need to change this file, update models or add
// new TypeGraphQL objects
import { ArgsType, Field as TypeGraphQLField, Float, ID, InputType as TypeGraphQLInputType, Int } from 'type-graphql';
import { registerEnumType } from 'type-graphql';
import GraphQLJSONObject from 'graphql-type-json';
import { registerEnumType } from 'type-graphql';
// tslint:disable-next-line:no-var-requires
const { GraphQLJSONObject } = require('graphql-type-json');
import { BaseWhereInput, PaginationArgs } from '${warthogImportPath}';
${entityListToImports(entities).join('')}
`;
Expand Down
32 changes: 20 additions & 12 deletions src/schema/TypeORMConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import { UniqueMetadata } from 'typeorm/metadata/UniqueMetadata';
import { getMetadataStorage } from '../metadata';

import GraphQLJSONObject from 'graphql-type-json';
// tslint:disable-next-line:no-var-requires
const { GraphQLJSONObject } = require('graphql-type-json');

const SYSTEM_FIELDS = [
'createdAt',
Expand Down Expand Up @@ -115,7 +116,7 @@ export function entityToCreateInput(entity: EntityMetadata): string {
// we need to know what the graphql type is and what the tsType is
// for enums

if (column.enum || column.type === 'json') {
if (column.enum || column.type === 'json' || column.type === 'jsonb') {
fieldTemplates += `
@TypeGraphQLField(type => ${graphQLDataType}, ${nullable})
${column.propertyName}${tsRequired}: ${tsType};
Expand Down Expand Up @@ -154,7 +155,7 @@ export function entityToUpdateInput(entity: EntityMetadata): string {
const graphQLDataType = columnTypeToGraphQLDataType(column);
const tsType = columnToTypeScriptType(column);

if (column.enum || column.type === 'json') {
if (column.enum || column.type === 'json' || column.type === 'jsonb') {
fieldTemplates += `
@TypeGraphQLField(type => ${graphQLDataType}, { nullable: true })
${column.propertyName}?: ${tsType};
Expand Down Expand Up @@ -270,7 +271,7 @@ export function entityToWhereInput(entity: EntityMetadata): string {
@TypeGraphQLField({ nullable: true })
${column.propertyName}_lte?: ${tsType};
`;
} else if (column.type !== 'json') {
} else if (column.type !== 'json' && column.type !== 'jsonb') {
// @@@ dcaddigan not sure what it means to search by JSONObjects
// future release?

Expand Down Expand Up @@ -375,11 +376,17 @@ export function columnToTypeScriptType(column: ColumnMetadata): string {

export function columnTypeToGraphQLDataType(column: ColumnMetadata): string {
const graphQLType = columnToGraphQLType(column);
const typeMap: any = {
JSON: 'GraphQLJSONObject'
};

return typeMap[graphQLType.name] || graphQLType.name;
// Sometimes we want to return the full blow GraphQL data type, but sometimes we want to return
// the more readable name. Ex:
// GraphQLInt -> Int
// GraphQLJSONObject -> GraphQLJSONObject
switch (graphQLType) {
case GraphQLJSONObject:
return 'GraphQLJSONObject';
default:
return graphQLType.name;
}
}
export function columnToGraphQLType(column: ColumnMetadata): GraphQLScalarType | GraphQLEnumType {
// Check to see if this column is an enum and return that
Expand All @@ -392,10 +399,6 @@ export function columnToGraphQLType(column: ColumnMetadata): GraphQLScalarType |
// Some types have a name attribute
const type = (column.type as any).name ? (column.type as any).name : column.type;

if (type instanceof GraphQLScalarType) {
return type;
}

switch (type) {
case String:
case 'String':
Expand Down Expand Up @@ -436,8 +439,13 @@ export function columnToGraphQLType(column: ColumnMetadata): GraphQLScalarType |
case 'timestamp':
return GraphQLISODateTime;
case 'json':
case 'jsonb':
return GraphQLJSONObject;
default:
if (type instanceof GraphQLScalarType) {
return type;
}

throw new Error(`convertToGraphQLType: Unexpected type: ${type}`);
}
}

0 comments on commit 14aaa83

Please sign in to comment.