Skip to content

Commit

Permalink
Fix removing root types
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnwalraven committed Jul 8, 2021
1 parent 01a3675 commit dd5c55b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildSchema } from 'graphql';
import { buildSchema, assertValidSchema } from 'graphql';
import { removeInaccessibleElements } from '../removeInaccessibleElements';

describe('removeInaccessibleElements', () => {
Expand Down Expand Up @@ -154,4 +154,100 @@ describe('removeInaccessibleElements', () => {
`Field Query.fooField returns an @inaccessible type without being marked @inaccessible itself`,
);
});

it(`removes @inaccessible query root type`, () => {
let schema = buildSchema(`
directive @core(feature: String!) repeatable on SCHEMA
directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION
schema
@core(feature: "https://specs.apollo.dev/core/v0.1")
@core(feature: "https://specs.apollo.dev/inaccessible/v0.1")
{
query: Query
}
type Query @inaccessible {
fooField: Foo
}
type Foo {
someField: String
}
`);

schema = removeInaccessibleElements(schema);

expect(schema.getQueryType()).toBeUndefined();
expect(schema.getType('Query')).toBeUndefined();

expect(() => assertValidSchema(schema)).toThrow();
});

it(`removes @inaccessible mutation root type`, () => {
let schema = buildSchema(`
directive @core(feature: String!) repeatable on SCHEMA
directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION
schema
@core(feature: "https://specs.apollo.dev/core/v0.1")
@core(feature: "https://specs.apollo.dev/inaccessible/v0.1")
{
query: Query
mutation: Mutation
}
type Query {
fooField: Foo
}
type Mutation @inaccessible {
fooField: Foo
}
type Foo {
someField: String
}
`);

schema = removeInaccessibleElements(schema);

expect(schema.getMutationType()).toBeUndefined();
expect(schema.getType('Mutation')).toBeUndefined();
});

it(`removes @inaccessible subscription root type`, () => {
let schema = buildSchema(`
directive @core(feature: String!) repeatable on SCHEMA
directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION
schema
@core(feature: "https://specs.apollo.dev/core/v0.1")
@core(feature: "https://specs.apollo.dev/inaccessible/v0.1")
{
query: Query
subscription: Subscription
}
type Query {
fooField: Foo
}
type Subscription @inaccessible {
fooField: Foo
}
type Foo {
someField: String
}
`);

schema = removeInaccessibleElements(schema);

expect(schema.getSubscriptionType()).toBeUndefined();
expect(schema.getType('Subscription')).toBeUndefined();
});
});
32 changes: 32 additions & 0 deletions query-planner-js/src/composedSchema/removeInaccessibleElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function removeInaccessibleElements(
}),
);

removeRootTypesIfNeeded();

return transformSchema(schema, (type) => {
// Remove the type.
if (typesToRemove.has(type)) return null;
Expand Down Expand Up @@ -71,6 +73,36 @@ export function removeInaccessibleElements(
}
});

function removeRootTypesIfNeeded() {
let schemaConfig = schema.toConfig();
let hasRemovedRootType = false;

const queryType = schema.getQueryType();

if (queryType && typesToRemove.has(queryType)) {
schemaConfig.query = undefined;
hasRemovedRootType = true;
}

const mutationType = schema.getMutationType();

if (mutationType && typesToRemove.has(mutationType)) {
schemaConfig.mutation = undefined;
hasRemovedRootType = true;
}

const subscriptionType = schema.getSubscriptionType();

if (subscriptionType && typesToRemove.has(subscriptionType)) {
schemaConfig.subscription = undefined;
hasRemovedRootType = true;
}

if (hasRemovedRootType) {
schema = new GraphQLSchema(schemaConfig);
}
}

function removeInaccessibleFields(
type: GraphQLCompositeType,
fieldMapConfig: GraphQLFieldConfigMap<any, any>,
Expand Down
3 changes: 3 additions & 0 deletions query-planner-js/src/composedSchema/toAPISchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
assertValidSchema,
GraphQLDirective,
GraphQLNamedType,
GraphQLSchema,
Expand Down Expand Up @@ -40,6 +41,8 @@ export function toAPISchema(schema: GraphQLSchema): GraphQLSchema {

schema.__apiSchema = apiSchema;

assertValidSchema(schema);

return apiSchema;

// TODO: Implement the IsExported algorithm from the Core Schema spec.
Expand Down

0 comments on commit dd5c55b

Please sign in to comment.