Skip to content

Commit

Permalink
fix(graphql): enum types get cached (#35)
Browse files Browse the repository at this point in the history
* fix(graphql): enum types get cached (#35)

* lint fixes

* added integration and unit test
  • Loading branch information
artem-barmin authored and calebmer committed May 5, 2016
1 parent 8e23854 commit 6eb854f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
8 changes: 6 additions & 2 deletions examples/forum/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ set search_path = forum_example, forum_example_utils;
-------------------------------------------------------------------------------
-- Basic Tables

create type entity_status as enum('new','updated','deleted');

create table person (
id serial not null primary key,
given_name varchar(64) not null,
family_name varchar(64),
about text,
created_at timestamp,
updated_at timestamp
updated_at timestamp,
status entity_status
);

comment on table person is 'A user of the forum.';
Expand All @@ -45,7 +48,8 @@ create table post (
topic post_topic,
body text,
created_at timestamp,
updated_at timestamp
updated_at timestamp,
status entity_status
);

comment on table post is 'A forum post written by a user.';
Expand Down
38 changes: 21 additions & 17 deletions src/graphql/getColumnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,24 @@ const postgresToGraphQLTypes = new Map([
])

/**
* Gets a GraphQL type for a PostgreSQL type.
*
* @param {Column} column
* @returns {GraphQLType}
*/
* Gets a GraphQL type for a PostgreSQL type.
*
* @param {Column} column
* @returns {GraphQLType}
*/
const getColumnEnumType = memoize(enum_ => {
return new GraphQLEnumType({
name: upperFirst(camelCase(enum_.name)),
description: enum_.description,
values: fromPairs(
enum_.variants
.map(variant => [toUpper(snakeCase(variant)), { value: variant }])
),
})
}, enum_ => {
return enum_.name
})

const getColumnGraphqlType = memoize(column => {
const wrapType = type => (column.isNullable ? type : new GraphQLNonNull(type))
const internalType = postgresToGraphQLTypes.get(column.type)
Expand All @@ -107,19 +120,10 @@ const getColumnGraphqlType = memoize(column => {
if (internalType)
return wrapType(internalType)

const enum_ = column.getEnum()

// If the column has an enum type, we need to create a `GraphQLEnumType`.
if (enum_) {
return wrapType(new GraphQLEnumType({
name: upperFirst(camelCase(enum_.name)),
description: enum_.description,
values: fromPairs(
enum_.variants
.map(variant => [toUpper(snakeCase(variant)), { value: variant }])
),
}))
}
const enum_ = column.getEnum()
if (enum_)
return wrapType(getColumnEnumType(enum_))

// Otherwise, just return `GraphQLString`.
return wrapType(GraphQLString)
Expand Down
6 changes: 6 additions & 0 deletions tests/graphql/getColumnType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ describe('getColumnType', () => {
expect(enumType.name).toEqual('TestEnum')
})

it('will return same reference for enum with same name', () => {
const enumFirst = getEnum()
const enumSecond = getEnum()
expect(enumFirst).toBe(enumSecond)
})

it('will correctly format variants', () => {
const enumType = getEnum()
expect(enumType.getValues()).toEqual([
Expand Down

0 comments on commit 6eb854f

Please sign in to comment.