Skip to content

Commit c7ab5fa

Browse files
committed
First test, some flow type fixes
1 parent c1bdf51 commit c7ab5fa

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

src/__tests__/globalId-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from 'chai';
2+
import {
3+
base64,
4+
unbase64,
5+
toGlobalId,
6+
fromGlobalId,
7+
} from '../globalId';
8+
9+
describe('globalId', () => {
10+
it('should have correct method base64()', () => {
11+
expect(base64('123')).to.equal('MTIz');
12+
expect(base64('lksdnfkksdknsdc:123')).to.equal('bGtzZG5ma2tzZGtuc2RjOjEyMw==');
13+
});
14+
15+
it('should have correct method unbase64()', () => {
16+
expect(unbase64('MTIz')).to.equal('123');
17+
expect(unbase64('bGtzZG5ma2tzZGtuc2RjOjEyMw==')).to.equal('lksdnfkksdknsdc:123');
18+
});
19+
20+
it('should have correct method toGlobalId()', () => {
21+
expect(toGlobalId('User', '789')).to.equal('VXNlcjo3ODk=');
22+
expect(toGlobalId('Article', 22)).to.equal('QXJ0aWNsZToyMg==');
23+
});
24+
25+
it('should have correct method fromGlobalId()', () => {
26+
expect(fromGlobalId('VXNlcjo3ODk=')).to.deep.equal({ type: 'User', id: '789' });
27+
expect(fromGlobalId('QXJ0aWNsZToyMg=='))
28+
.to.deep.equal({ type: 'Article', id: '22' });
29+
});
30+
});

src/definition.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1+
/* @flow */
2+
13
// RE-EXPORT graphql-compose definitions
24
import type {
35
ResolveParams as _ResolveParams,
6+
ResolverMWArgsFn as _ResolverMWArgsFn,
7+
ResolverMWArgs as _ResolverMWArgs,
8+
ResolverMWResolveFn as _ResolverMWResolveFn,
9+
ResolverMWResolve as _ResolverMWResolve,
10+
ResolverMWOutputTypeFn as _ResolverMWOutputTypeFn,
11+
ResolverMWOutputType as _ResolverMWOutputType,
412
} from 'graphql-compose/lib/definition.js';
513

14+
export type ResolverMWArgsFn = _ResolverMWArgsFn;
15+
export type ResolverMWArgs = _ResolverMWArgs;
16+
17+
export type ResolverMWResolveFn = _ResolverMWResolveFn;
18+
export type ResolverMWResolve = _ResolverMWResolve;
19+
20+
export type ResolverMWOutputTypeFn = _ResolverMWOutputTypeFn;
21+
export type ResolverMWOutputType = _ResolverMWOutputType;
22+
23+
24+
import type {
25+
TypeComposer as _TypeComposer,
26+
} from 'graphql-compose';
27+
628
export type ResolveParams = _ResolveParams;
29+
export type TypeComposer = _TypeComposer;
30+
export type TypeComposerMap = {[typeName: string]: TypeComposer};
731

832

933
// INTERNAL TYPES

src/globalId.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import {
3+
import type {
44
ResolvedGlobalId,
55
Base64String,
66
} from './definition.js';

src/mutationMiddleware.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
import { ResolverMiddleware, TypeComposer } from 'graphql-compose';
55
import { GraphQLID, GraphQLString, GraphQLObjectType } from 'graphql';
66
import { toGlobalId } from './globalId';
7+
import type {
8+
ResolverMWArgsFn,
9+
ResolverMWArgs,
10+
ResolverMWResolveFn,
11+
ResolverMWResolve,
12+
ResolverMWOutputTypeFn,
13+
ResolverMWOutputType,
14+
} from './definition';
715

8-
export class MutationMiddleware extends ResolverMiddleware {
16+
17+
export default class MutationMiddleware extends ResolverMiddleware {
918
// constructor(typeComposer, opts = {}) {
1019
// super(typeComposer, opts);
1120
// }
1221

13-
args = (next) => (args) => {
22+
args:ResolverMWArgs = (next: ResolverMWArgsFn) => (args) => {
1423
const nextArgs = next(args);
1524

1625
if (!nextArgs.clientMutationId) {
@@ -25,7 +34,7 @@ export class MutationMiddleware extends ResolverMiddleware {
2534
};
2635

2736

28-
resolve = (next) => (resolveParams) => {
37+
resolve:ResolverMWResolve = (next: ResolverMWResolveFn) => (resolveParams) => {
2938
let clientMutationId;
3039

3140
if (resolveParams && resolveParams.args && resolveParams.args.input
@@ -48,7 +57,7 @@ export class MutationMiddleware extends ResolverMiddleware {
4857
};
4958

5059

51-
outputType = (next) => (outputType) => {
60+
outputType:ResolverMWOutputType = (next: ResolverMWOutputTypeFn) => (outputType) => {
5261
const nextOutputType = next(outputType);
5362

5463
if (!(nextOutputType instanceof GraphQLObjectType)) {

src/nodeFieldConfig.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import {
77
} from 'graphql';
88
import { fromGlobalId } from './globalId';
99
import NodeInterface from './nodeInterface';
10+
import type { TypeComposerMap } from './definition.js';
1011

1112
// this fieldConfig must be set to RootQuery.node field
12-
export function getNodeFieldConfig(typeComposerMap) {
13+
export function getNodeFieldConfig(typeComposerMap: TypeComposerMap) {
1314
return {
1415
description: 'Fetches an object that has globally unique ID among all types',
1516
type: NodeInterface,
@@ -19,7 +20,10 @@ export function getNodeFieldConfig(typeComposerMap) {
1920
description: 'The globally unique ID among all types',
2021
},
2122
},
22-
resolve: (source, args, context, info) => {
23+
resolve: (source: mixed, args: {[argName: string]: mixed}, context: mixed, info: mixed) => {
24+
if (!args.id || !(typeof args.id === 'string')) {
25+
return null;
26+
}
2327
const { type, id } = fromGlobalId(args.id);
2428

2529
const typeComposer = typeComposerMap[type];

0 commit comments

Comments
 (0)