Skip to content

Commit

Permalink
Adds support for removing a field on a particular type
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Oct 2, 2018
1 parent 8a78dcc commit 007c3f5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/test/testTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ReplaceFieldWithFragment,
FilterToSchema,
} from '../transforms';
import RemoveFieldsOnType from '../transforms/RemoveFieldsOnType';

describe('transforms', () => {
describe('rename type', () => {
Expand Down Expand Up @@ -309,6 +310,34 @@ describe('transforms', () => {
});
});

describe('removing fields from a type', () => {
it('removes a field from an existing type', async () => {
const newSchema = transformSchema(propertySchema, [
new RemoveFieldsOnType('Property', ['name', 'location'])
]);
const result = await graphql(
newSchema,
`
query {
propertyById(id: "p1") {
id
name
location
}
}
`,
);
expect(result.errors).not.to.be.empty;
expect(result.errors.length).to.equal(2);
expect(result.errors[0].message).to.equal(
'Cannot query field "name" on type "Property".'
);
expect(result.errors[1].message).to.equal(
'Cannot query field "location" on type "Property".'
);
});
});

describe('tree operations', () => {
let data: any;
let subSchema: GraphQLSchema;
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/AddArgumentsAsVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function typeToAst(type: GraphQLInputType): TypeNode {
type: innerType,
};
} else {
throw new Error('Incorrent inner non-null type');
throw new Error('Incorrect inner non-null type');
}
} else if (type instanceof GraphQLList) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/ConvertEnumValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLSchema, GraphQLEnumType } from 'graphql';
import { Transform } from '../transforms/transforms';
import { visitSchema, VisitSchemaKind } from '../transforms/visitSchema';

// Transformation used to modifiy `GraphQLEnumType` values in a schema.
// Transformation used to modify `GraphQLEnumType` values in a schema.
export default class ConvertEnumValues implements Transform {
// Maps current enum values to their new values.
// e.g. { Color: { 'RED': '#EA3232' } }
Expand Down
52 changes: 52 additions & 0 deletions src/transforms/RemoveFieldsOnType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
GraphQLObjectType,
GraphQLSchema,
GraphQLNamedType,
} from 'graphql';
import { Transform } from './transforms';
import { visitSchema, VisitSchemaKind } from './visitSchema';
import { fieldToFieldConfig, createResolveType } from '../stitching/schemaRecreation';

export default class RemoveFieldsOnType implements Transform {
private typeName: string;
private fields: Array<string>;

constructor(typeName: string, fields: Array<string> ) {
this.typeName = typeName;
this.fields = fields;
}

public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema {
return visitSchema(originalSchema, {
[VisitSchemaKind.OBJECT_TYPE]: (type: GraphQLObjectType) => {
// If it's not the object you're looking for, just skip it
if (type.name !== this.typeName) {
return type;
}
// It's the class you're after, so re-create the type
// and remove the fields that you want to remove
const resolveType = createResolveType(
(name: string, originalType: GraphQLNamedType): GraphQLNamedType =>
originalType,
);
const newFields = {};
const oldFields = type.getFields();
Object.keys(oldFields).forEach(fieldName => {
if (!this.fields.includes(fieldName)) {
const field = oldFields[fieldName];
newFields[fieldName] = fieldToFieldConfig(field, resolveType, true);
}
});
return new GraphQLObjectType({
name: type.name,
description: type.description,
astNode: type.astNode,
// extensionASTNodes: type.extensionASTNodes,
// interfaces: type.getInterfaces(),
// isTypeOf: type.isTypeOf,
fields: newFields
});
},
});
}
}

0 comments on commit 007c3f5

Please sign in to comment.