# Remove field from schema

**URL:** <https://community.apollographql.com/t/remove-field-from-schema/9288>\
**Category:** Server\
**Created:** [July 15, 2025, 12:21pm UTC](https://community.apollographql.com/t/remove-field-from-schema/9288 "2025-07-15T12:21:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![santsalc](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/santsalc/32/6265_2.png) [@santsalc](https://community.apollographql.com/u/santsalc)\
**Post date:** [July 15, 2025, 12:21pm UTC](https://community.apollographql.com/t/remove-field-from-schema/9288/1 "2025-07-15T12:21:57Z")

</div>

Hi! Is there a way to remove a field from the schema based on an environment variable? I’m working with a whitelabel GraphQL API and need to exclude a specific field during application startup. Would that be possible?

I have two clients and one server for each one

---

<div class="post-metadata">

**Author:** ![michaeldouglasdev](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/michaeldouglasdev/32/4911_2.png) [@michaeldouglasdev](https://community.apollographql.com/u/michaeldouglasdev)\
**Post date:** [July 15, 2025, 12:43pm UTC](https://community.apollographql.com/t/remove-field-from-schema/9288/2 "2025-07-15T12:43:44Z")

</div>

It’s definitely possible to conditionally exclude fields using a custom directive, but the implementation differs slightly depending on whether you’re using a monolithic GraphQL server or an GraphQL Federation architecture. Since you didn’t specify your setup, I’ll cover both cases.

### Monolithic GraphQL Server

In a monolithic setup, you can create a custom directive (e.g., `@exclude`) to remove fields based on an environment variable. Use `@graphql-tools/schema` to transform the schema and return null for fields that should be excluded.

```auto
directive @exclude(for: Client!) on FIELD_DEFINITION

enum Client {
  CLIENT_1
  CLIENT_2
}

type Foo {
  bar: String!
  baz: String! @exclude(for: CLIENT_1)
}

type Query {
  foo: Foo!
}

```

### GraphQL Federation

In a Federation setup, the process is similar, but there’s a catch: the Router uses the `_service { sdl }` query to compose the `supergraph` schema, not the `__schema` introspection. If you apply a directive transformation using `buildSubgraphSchema`, the transformed schema (with fields removed) is reflected in the subgraph’s introspection (`__schema`), but the schema sent to the router (`_service { sdl }`) won’t include these changes.

Additionally, `makeExecutableSchema` doesn’t recognize Federation directives like `@link`, `@key`, etc., which causes errors. To fix this, you need to:

1. Explicitly declare Federation directives in your schema.
2. Apply the directive transformation before passing the schema to `buildSubgraphSchema`.

I’m not sure, but maybe you can solve this problem in Federation using `@tag` directive and `Managed Federation`

If you are using Federation and have one server for each client, consequently, one router for each, you could use the `@inaccessible` directive, it’s much easier than the previous one. But if you need to exclude a specific field based on environment variable during the startup, this directive will not help you.

EDIT: I’m not sure, but maybe you can use the @exclude directive to add the @inaccessible directive to the field if condition is true instead of return null

---

<div class="post-metadata">

**Author:** ![santsalc](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/santsalc/32/6265_2.png) [@santsalc](https://community.apollographql.com/u/santsalc)\
**Post date:** [July 15, 2025, 1:55pm UTC](https://community.apollographql.com/t/remove-field-from-schema/9288/3 "2025-07-15T13:55:17Z")

</div>

That answer fit perfectly into what I needed. Really appreciate the clarity and precision. Thank you!
