Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter empty selections sets in FilterToSchema (#826) #827

Merged
merged 3 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Add null to return type of directive visitors in the TypeScript definition.
* Make sure mergeSchemas keeps Enum descriptions and deprecation status. [PR 898](https://github.com/apollographql/graphql-tools/pull/898/)
* Add `inheritResolversFromInterfaces` option to `mergeSchemas` [PR #812](https://github.com/apollographql/graphql-tools/pull/812)
* Added filtering of empty selection sets in FilterToSchema [#827](https://github.com/apollographql/graphql-tools/pull/827)

### v3.0.5

Expand Down
101 changes: 101 additions & 0 deletions src/test/testTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
graphql,
Kind,
SelectionSetNode,
print,
parse,
} from 'graphql';
import { makeExecutableSchema } from '../makeExecutableSchema';
import { propertySchema, bookingSchema } from './testingSchemas';
Expand All @@ -17,6 +19,7 @@ import {
FilterTypes,
WrapQuery,
ExtractField,
FilterToSchema,
} from '../transforms';

describe('transforms', () => {
Expand Down Expand Up @@ -143,6 +146,104 @@ describe('transforms', () => {
});
});

describe('filter to schema', () => {
let filter: FilterToSchema;
before(() => {
filter = new FilterToSchema(bookingSchema);
});

it('should remove empty selection sets on objects', async () => {
const query = parse(`
query customerQuery($id: ID!) {
customerById(id: $id) {
id
name
address {
planet
}
}
}
`);
const filteredQuery = filter.transformRequest({
document: query,
variables: {
id: 'c1'
}
});

const expected = parse(`
query customerQuery($id: ID!) {
customerById(id: $id) {
id
name
}
}
`);
expect(print(filteredQuery.document)).to.equal(print(expected));
});

it('should also remove variables when removing empty selection sets', async () => {
const query = parse(`
query customerQuery($id: ID!, $limit: Int) {
customerById(id: $id) {
id
name
bookings(limit: $limit) {
paid
}
}
}
`);
const filteredQuery = filter.transformRequest({
document: query,
variables: {
id: 'c1',
limit: 10
}
});

const expected = parse(`
query customerQuery($id: ID!) {
customerById(id: $id) {
id
name
}
}
`);
expect(print(filteredQuery.document)).to.equal(print(expected));
});

it('should remove empty selection sets on wrapped objects (non-nullable/lists)', async () => {
const query = parse(`
query bookingQuery($id: ID!) {
bookingById(id: $id) {
id
propertyId
customer {
favoriteFood
}
}
}
`);
const filteredQuery = filter.transformRequest({
document: query,
variables: {
id: 'b1'
}
});

const expected = parse(`
query bookingQuery($id: ID!) {
bookingById(id: $id) {
id
propertyId
}
}
`);
expect(print(filteredQuery.document)).to.equal(print(expected));
});
});

describe('filter type', () => {
let schema: GraphQLSchema;
before(() => {
Expand Down
35 changes: 28 additions & 7 deletions src/transforms/FilterToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ function filterDocumentToSchema(
): DocumentNode {
const operations: Array<
OperationDefinitionNode
> = document.definitions.filter(
def => def.kind === Kind.OPERATION_DEFINITION,
) as Array<OperationDefinitionNode>;
> = document.definitions.filter(
def => def.kind === Kind.OPERATION_DEFINITION,
) as Array<OperationDefinitionNode>;
const fragments: Array<FragmentDefinitionNode> = document.definitions.filter(
def => def.kind === Kind.FRAGMENT_DEFINITION,
) as Array<FragmentDefinitionNode>;
Expand Down Expand Up @@ -95,8 +95,9 @@ function filterDocumentToSchema(
targetSchema,
type,
validFragmentsWithType,
operation.selectionSet,
operation.selectionSet
);

usedFragments = union(usedFragments, operationUsedFragments);

const {
Expand Down Expand Up @@ -164,7 +165,7 @@ function collectFragmentVariables(
type,
validFragmentsWithType,
fragment.selectionSet,
);
);
usedFragments = union(usedFragments, fragmentUsedFragments);
usedVariables = union(usedVariables, fragmentUsedVariables);

Expand Down Expand Up @@ -241,8 +242,28 @@ function filterSelectionSet(
typeStack.push(TypeNameMetaFieldDef.type);
}
},
leave() {
typeStack.pop();
leave(node: FieldNode): null | undefined | FieldNode {
const currentType = typeStack.pop();
const resolvedType = resolveType(currentType);
if (
resolvedType instanceof GraphQLObjectType ||
resolvedType instanceof GraphQLInterfaceType
) {
const selections = node.selectionSet && node.selectionSet.selections || null;
if (!selections || selections.length === 0) {
// need to remove any added variables. Is there a better way to do this?
visit(node, {
[Kind.VARIABLE](variableNode: VariableNode) {
const index = usedVariables.indexOf(variableNode.name.value);
if (index !== -1) {
usedVariables.splice(index, 1);
}
}
}
);
return null;
}
}
},
},
[Kind.FRAGMENT_SPREAD](node: FragmentSpreadNode): null | undefined {
Expand Down