Skip to content

Commit

Permalink
Add support for __typename in @examples values
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jul 18, 2019
1 parent 5208e5b commit 72f59ca
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/fake_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getDirectiveValues,
GraphQLSchema,
GraphQLObjectType,
GraphQLAbstractType,
GraphQLOutputType,
GraphQLInputObjectType,
GraphQLList,
Expand Down Expand Up @@ -105,24 +104,14 @@ export function fakeSchema(schema: GraphQLSchema) {
if (type instanceof GraphQLList)
return arrayResolver(getResolver(type.ofType, field));

if (isAbstractType(type))
return abstractTypeResolver(type);

return fieldResolver(type, field);
}


function abstractTypeResolver(type:GraphQLAbstractType) {
const possibleTypes = schema.getPossibleTypes(type);
return () => ({__typename: getRandomItem(possibleTypes)});
}

function fieldResolver(type:GraphQLOutputType, field) {
const directiveToArgs = {
const { fake, examples } = {
...getFakeDirectives(type),
...getFakeDirectives(field),
};
const {fake, examples} = directiveToArgs;

if (isLeafType(type)) {
if (examples)
Expand All @@ -133,13 +122,15 @@ export function fakeSchema(schema: GraphQLSchema) {
return () => fakeLeafValue(type);
} else {
// TODO: error on fake directive
if (examples) {
return () => ({
...getRandomItem(examples.values),
$example: true,
});
}
return () => ({});
const possibleTypes = isAbstractType(type)
? schema.getPossibleTypes(type)
: [type];

return () => ({
__typename: getRandomItem(possibleTypes),
...(examples ? getRandomItem(examples.values) : {}),
$example: true,
});
}
}

Expand Down

0 comments on commit 72f59ca

Please sign in to comment.