Remove GraphQL field based on another field in Gatsby page query

2 replies
Last updated: Aug 9, 2021
How do you remove a GraphQL field based on another field, on a Gatsby page query?

https://stackoverflow.com/questions/68714301/how-to-remove-graphql-fields-according-to-other-fields-in-gatsby
AI Update

Looking at the Stack Overflow question you linked, this is actually about Gatsby's schema customization capabilities rather than something specific to Sanity. The question is asking how to conditionally remove fields from GraphQL queries based on the value of another field.

In Gatsby, you can't directly filter out fields in a page query based on field values, but you have a few approaches:

Using createResolvers for Schema Customization

The most powerful approach is to use Gatsby's createResolvers API in your gatsby-node.js to create custom resolvers that conditionally return data. Here's an example:

exports.createResolvers = ({ createResolvers }) => {
  const resolvers = {
    SanityYourType: {
      conditionalField: {
        type: "String",
        resolve: (source) => {
          // Only return the field if some condition is met
          if (source.someOtherField === "specificValue") {
            return source.conditionalField
          }
          return null
        }
      }
    }
  }
  createResolvers(resolvers)
}

Filtering in Your Component

A simpler approach is to query all fields and filter them in your React component:

const MyComponent = ({ data }) => {
  const filteredData = data.allSanityYourType.nodes.filter(node => {
    return node.someField === "condition"
  }).map(node => {
    // Return only the fields you want
    const { unwantedField, ...rest } = node
    return rest
  })
  
  return <div>{/* render filtered data */}</div>
}

Creating Custom Fields

You can also use createResolvers to create entirely new computed fields that combine logic from multiple fields:

exports.createResolvers = ({ createResolvers }) => {
  const resolvers = {
    SanityYourType: {
      computedField: {
        type: "JSON",
        resolve: (source) => {
          // Create a new field based on conditions
          const result = {}
          if (source.fieldA) result.fieldA = source.fieldA
          if (source.condition === true) result.fieldB = source.fieldB
          return result
        }
      }
    }
  }
  createResolvers(resolvers)
}

If you're using gatsby-source-sanity, these schema customization techniques work seamlessly with the data sourced from Sanity. The plugin creates GraphQL types based on your Sanity schema, and you can extend or modify them using Gatsby's schema customization APIs.

The key limitation to remember: GraphQL queries in Gatsby are static and run at build time, so you can't make them truly dynamic based on runtime values. Your best bet is either schema customization with createResolvers or post-query filtering in your components.

Show original thread
2 replies
Thanks for your reply! Query Filters seem to only allow to filter fields based on the values of those same fields. I want to remove the URL field based on the sibling “public” field. If the video is not public, remove the URL. Otherwise, include it.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?