GraphQL in Gatsbyjs - Only Return Data Which Contains a Particular Field GraphQL in Gatsbyjs - Only Return Data Which Contains a Particular Field json json

GraphQL in Gatsbyjs - Only Return Data Which Contains a Particular Field


It looks like there isn't an $exists operator in GraphQL. Instead what you can do is add some logic in the resolver to check if a field is not null. I found two older questions related to yours:
GraphQL query and check the returned data
GraphQL query: only include field if not null


if sections & menuSections are string or arrays of string, maybe you can filter for null:

{  "menuSections": "..."}
// query{  allDataJson(filter: {    menuSections: {      ne: null    }  }) {    nodes {      menuSections    }  }}

If they are object, you can still filter for null, but it has to be applied to one of the field inside that object. If your objects don't have a common field, this won't work:

{  "menuSections": {    "menuSectionField": "..."  }}
// query{  allDataJson(filter: {    menuSections: {      menuSectionField: {        ne: null      }    }  }) {    nodes {      menuSections    }  }}

If they are array of objects, you can do the same thing but with elemMatch:

{  "menuSections": [    { "menuSectionField": "..." },    { "other": "..." }  ]}
// queryallDataJson(filter: {    menuSections: {      elemMatch: {        menuSectionField: {          ne: null        }      }    }  }) { ... }

Worst case worst, I think you might be able to define some sort of custom types that ensure existence of menuSections so you can query allDataWithMenuSections etc., but if filter works it's much simpler.