Skip to content

Commit 94c15ff

Browse files
committed
use flow-dynamic to check resolver,and link them to Flow
1 parent d13a8ef commit 94c15ff

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"coveralls": "2.11.9",
5959
"eslint": "2.10.2",
6060
"flow-bin": "0.25.0",
61+
"flow-dynamic": "^0.0.9",
62+
"flow-graphql": "^0.6.4",
6163
"graphql": "0.6.0",
6264
"isparta": "4.0.0",
6365
"mocha": "2.5.3",

src/mutation/__tests__/mutation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ var simpleRootValueMutation = mutationWithClientMutationId({
6565
type: GraphQLInt
6666
}
6767
},
68-
mutateAndGetPayload: (params, context, {rootValue}) => (rootValue)
68+
// :any to ignore type check in test.
69+
mutateAndGetPayload: (params, context, {rootValue}) => ((rootValue:any))
6970
});
7071

7172
var mutation = new GraphQLObjectType({

src/mutation/mutation.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import type {
2222
GraphQLResolveInfo
2323
} from 'graphql';
2424

25+
import {
26+
graph,
27+
pro
28+
} from 'flow-dynamic';
29+
const {argsCheck} = graph;
30+
2531
type mutationFn = (object: Object, ctx: mixed, info: GraphQLResolveInfo) =>
2632
( Object | Promise<Object> );
2733

@@ -86,12 +92,16 @@ export function mutationWithClientMutationId(
8692
args: {
8793
input: {type: new GraphQLNonNull(inputType)}
8894
},
89-
resolve: (_, {input}, context, info) => {
90-
return Promise.resolve(mutateAndGetPayload(input, context, info))
91-
.then(payload => {
92-
payload.clientMutationId = input.clientMutationId;
93-
return payload;
94-
});
95-
}
95+
resolve: argsCheck(
96+
args => ({
97+
input: pro.isObject(args.input)
98+
}),
99+
(_, {input}, context, info) => {
100+
return Promise.resolve(mutateAndGetPayload(input, context, info))
101+
.then(payload => {
102+
payload.clientMutationId = input.clientMutationId;
103+
return payload;
104+
});
105+
})
96106
};
97107
}

src/node/__tests__/plural.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ var queryType = new GraphQLObjectType({
4848
description: 'Map from a username to the user',
4949
inputType: GraphQLString,
5050
outputType: userType,
51-
// $FlowFixMe : rootValue Graphql(mixed) -> relay(object)
52-
resolveSingleInput: (username, context, {rootValue: {lang}}) => ({
51+
// rootValue Graphql(mixed) -> relay(object)
52+
// :any to ignore type check in test.
53+
resolveSingleInput: (username, context, {rootValue}) => ({
5354
username: username,
54-
url: 'www.facebook.com/' + username + '?lang=' + lang
55+
url: 'www.facebook.com/' + username + '?lang=' + (rootValue:any).lang
5556
})
5657
})
5758
})

src/node/node.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import {
2525
unbase64
2626
} from '../utils/base64.js';
2727

28+
import {
29+
graph,
30+
pro
31+
} from 'flow-dynamic';
32+
const {argsCheck, sourceCheck} = graph;
33+
2834
type GraphQLNodeDefinitions = {
2935
nodeInterface: GraphQLInterfaceType,
3036
nodeField: GraphQLFieldConfig
@@ -58,6 +64,8 @@ export function nodeDefinitions(
5864
resolveType: typeResolver
5965
});
6066

67+
68+
6169
var nodeField = {
6270
name: 'node',
6371
description: 'Fetches an object given its ID',
@@ -68,7 +76,11 @@ export function nodeDefinitions(
6876
description: 'The ID of an object'
6977
}
7078
},
71-
resolve: (obj, {id}, context, info) => idFetcher(id, context, info),
79+
// type NodeArgs = {id:string};
80+
resolve: argsCheck(
81+
args => ({id: pro.isString(args.id) }),
82+
(obj, args, context, info) => idFetcher(args.id, context, info)
83+
)
7284
};
7385

7486
return {nodeInterface, nodeField};
@@ -114,9 +126,12 @@ export function globalIdField(
114126
name: 'id',
115127
description: 'The ID of an object',
116128
type: new GraphQLNonNull(GraphQLID),
117-
resolve: (obj, args, context, info) => toGlobalId(
118-
typeName || info.parentType.name,
119-
idFetcher ? idFetcher(obj, context, info) : obj.id
129+
resolve: sourceCheck(
130+
obj => pro.isObject(obj),
131+
(obj, args, context, info) => toGlobalId(
132+
typeName || info.parentType.name,
133+
idFetcher ? idFetcher(obj, context, info) : obj.id
134+
)
120135
)
121136
};
122137
}

src/node/plural.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import type {
2323
GraphQLInputObjectType
2424
} from 'graphql';
2525

26+
import {
27+
graph,
28+
pro
29+
} from 'flow-dynamic';
30+
const {argsCheck} = graph;
31+
2632
type PluralIdentifyingRootFieldConfig = {
2733
argName: string,
2834
inputType: GraphQLInputType,
@@ -47,14 +53,16 @@ export function pluralIdentifyingRootField(
4753
description: config.description,
4854
type: new GraphQLList(config.outputType),
4955
args: inputArgs,
50-
resolve: (obj, args, context, info) => {
51-
var inputs = args[config.argName];
52-
return Promise.all(inputs.map(
53-
input => Promise.resolve(
54-
config.resolveSingleInput(input, context, info)
55-
)
56-
));
57-
}
56+
resolve: argsCheck( args => pro.isObject(args),
57+
(obj, args, context, info) => {
58+
var inputs = args[config.argName];
59+
return Promise.all(inputs.map(
60+
input => Promise.resolve(
61+
config.resolveSingleInput(input, context, info)
62+
)
63+
));
64+
}
65+
)
5866
};
5967
}
6068

0 commit comments

Comments
 (0)