Skip to content

Commit

Permalink
Code coverage fixes.
Browse files Browse the repository at this point in the history
Since we're planning to bump the graphql-tools major version to 3.0.0, I
think it's fine to hard-deprecate passing positional arguments to
delegateToSchema, by throwing an exception rather than warning/continuing.
There's no reason to keep using the old style, especially now that the
fragmentReplacements argument has been replaced by transforms.
  • Loading branch information
benjamn committed Apr 23, 2018
1 parent fa9af99 commit adb1a7f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
54 changes: 9 additions & 45 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,36 @@ import {
execute,
validate,
VariableDefinitionNode,
print,
GraphQLResolveInfo,
InlineFragmentNode,
GraphQLSchema,
} from 'graphql';

import {
Operation,
Request,
IDelegateToSchemaOptions,
Transform,
} from '../Interfaces';

import {
applyRequestTransforms,
applyResultTransforms,
} from '../transforms/transforms';

import AddArgumentsAsVariables from '../transforms/AddArgumentsAsVariables';
import FilterToSchema from '../transforms/FilterToSchema';
import AddTypenameToAbstract from '../transforms/AddTypenameToAbstract';
import CheckResultAndHandleErrors from '../transforms/CheckResultAndHandleErrors';
import ReplaceFieldWithFragment from '../transforms/ReplaceFieldWithFragment';

export default async function delegateToSchema(
export default function delegateToSchema(
options: IDelegateToSchemaOptions | GraphQLSchema,
...args: Array<any>
): Promise<any>;
export default async function delegateToSchema(
options: IDelegateToSchemaOptions | GraphQLSchema,
fragmentReplacements: {
[typeName: string]: { [fieldName: string]: InlineFragmentNode };
},
operation: 'query' | 'mutation' | 'subscription',
fieldName: string,
args: { [key: string]: any },
context: { [key: string]: any },
info: GraphQLResolveInfo,
transforms?: Array<Transform>,
...args: any[],
): Promise<any> {
if (options instanceof GraphQLSchema) {
const schema = options;
console.warn(
'Argument list is a deprecated. Pass object of parameters ' +
'to delegate to schema',
throw new Error(
'Passing positional arguments to delegateToSchema is a deprecated. ' +
'Please pass named parameters instead.'
);
const fragments: Array<{ field: string; fragment: string }> = [];
Object.keys(fragmentReplacements).forEach(typeName => {
const typeFragments = fragmentReplacements[typeName];
Object.keys(typeFragments).forEach(field => {
fragments.push({ field, fragment: print(typeFragments[field]) });
});
});
const newOptions: IDelegateToSchemaOptions = {
schema,
operation,
fieldName,
args,
context,
info,
transforms: [
new ReplaceFieldWithFragment(schema, fragments),
...(transforms || []),
],
};
return delegateToSchemaImplementation(newOptions);
} else {
return delegateToSchemaImplementation(options);
}
return delegateToSchemaImplementation(options);
}

async function delegateToSchemaImplementation(
Expand Down
44 changes: 38 additions & 6 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,36 @@ if (process.env.GRAPHQL_VERSION === '^0.11') {
`;
}

// Miscellaneous typeDefs that exercise uncommon branches for the sake of
// code coverage.
const codeCoverageTypeDefs = `
interface SyntaxNode {
type: String
}
type Statement implements SyntaxNode {
type: String
}
type Expression implements SyntaxNode {
type: String
}
union ASTNode = Statement | Expression
enum Direction {
NORTH
SOUTH
EAST
WEST
}
input WalkingPlan {
steps: Int
direction: Direction
}
`;

testCombinations.forEach(async combination => {
describe('merging ' + combination.name, () => {
let mergedSchema: GraphQLSchema,
Expand All @@ -329,23 +359,25 @@ testCombinations.forEach(async combination => {
linkSchema,
loneExtend,
localSubscriptionSchema,
codeCoverageTypeDefs,
],
resolvers: {
Property: {
bookings: {
fragment: '... on Property { id }',
resolve(parent, args, context, info) {
return info.mergeInfo.delegateToSchema({
schema: bookingSchema,
operation: 'query',
fieldName: 'bookingsByPropertyId',
args: {
// Use the old mergeInfo.delegate API just this once, to make
// sure it continues to work.
return info.mergeInfo.delegate(
'query',
'bookingsByPropertyId',
{
propertyId: parent.id,
limit: args.limit ? args.limit : null,
},
context,
info,
});
);
},
},
},
Expand Down

0 comments on commit adb1a7f

Please sign in to comment.