Skip to content

Commit

Permalink
feat(TypeMapper): allow to override built-in JSON, Date and `Buff…
Browse files Browse the repository at this point in the history
…er` types
  • Loading branch information
nodkz committed Apr 25, 2018
1 parent 45eb4c1 commit 78c9000
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/TypeMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
// isOutputType,
// isInputType,
isNamedType,
isScalarType,
valueFromAST,
} from './graphql';
import type {
Expand Down Expand Up @@ -124,23 +125,26 @@ export class TypeMapper<TContext> {
['Int', GraphQLInt],
['Boolean', GraphQLBoolean],
['ID', GraphQLID],
// graphql-compose basic types
['JSON', GraphQLJSON],
['Json', GraphQLJSON],
['Date', GraphQLDate],
['Buffer', GraphQLBuffer],
]);

get(name: string): ?GraphQLNamedType {
const basicScalar = this.basicScalars.get(name);
if (basicScalar) return basicScalar;

if (!this.schemaComposer.has(name)) {
return null;
if (name === 'JSON' || name === 'Json') {
this.schemaComposer.set(name, GraphQLJSON);
} else if (name === 'Date') {
this.schemaComposer.set(name, GraphQLDate);
} else if (name === 'Buffer') {
this.schemaComposer.set(name, GraphQLBuffer);
} else {
return null;
}
}

const schemaType = this.schemaComposer.get(name);
if (isNamedType(schemaType)) {
if (isNamedType(schemaType) || isScalarType(schemaType)) {
return schemaType;
}
return schemaType.getType();
Expand Down
9 changes: 7 additions & 2 deletions src/TypeStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { isFunction } from './utils/is';
import { TypeComposer } from './TypeComposer';
import { InputTypeComposer } from './InputTypeComposer';
import { EnumTypeComposer } from './EnumTypeComposer';
import type { GraphQLNamedType } from './graphql';
import type { GraphQLNamedType, GraphQLScalarType } from './graphql';

type K = string;
type V<TContext> = TypeComposer<TContext> | InputTypeComposer | EnumTypeComposer | GraphQLNamedType;
type V<TContext> =
| TypeComposer<TContext>
| InputTypeComposer
| EnumTypeComposer
| GraphQLNamedType
| GraphQLScalarType;

// TypeStorage has all methods from Map class
export class TypeStorage<TContext> {
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/TypeMapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GraphQLList,
GraphQLNonNull,
GraphQLEnumType,
GraphQLScalarType,
} from '../graphql';
import {
schemaComposer,
Expand Down Expand Up @@ -52,6 +53,29 @@ describe('TypeMapper', () => {
expect(TypeMapper.get('Buffer')).toBe(GraphQLBuffer);
});

it('should allow to override basic graphql-compose types', () => {
const CustomJSON = new GraphQLScalarType({
name: 'CustomJSON',
serialize: () => {},
});
const CustomDate = new GraphQLScalarType({
name: 'CustomDate',
serialize: () => {},
});
const CustomBuffer = new GraphQLScalarType({
name: 'CustomBuffer',
serialize: () => {},
});
schemaComposer.set('JSON', CustomJSON);
schemaComposer.set('Json', CustomJSON);
schemaComposer.set('Date', CustomDate);
schemaComposer.set('Buffer', CustomBuffer);
expect(TypeMapper.get('JSON')).toBe(CustomJSON);
expect(TypeMapper.get('Json')).toBe(CustomJSON);
expect(TypeMapper.get('Date')).toBe(CustomDate);
expect(TypeMapper.get('Buffer')).toBe(CustomBuffer);
});

it('should create object type from template string', () => {
const type: GraphQLObjectType = (TypeMapper.createType(
graphqlVersion < 12
Expand Down

0 comments on commit 78c9000

Please sign in to comment.