Skip to main content

Inferring Input Filters

This documentation isn’t up to date with the latest schema customization changes.

Outdated areas are:

  • inferObjectStructureFromNodes does not exist anymore
  • input fields are generated differently. Gatsby previously inferred values one by one, now graphql-compose handles this

You can help by making a PR to update this documentation.

Input Filters vs gqlType

In gqlTypes, we inferred a Gatsby Node’s main fields. These allow us to query a node’s children, parent and object fields. But these are only useful once a top level GraphQL Query has returned results. In order to query by those fields, we must create GraphQL objects for input filters. E.g, querying for all markdownRemark nodes that have 4 paragraphs.

The arguments (wordcount: {paragraphs: {eq: 4}}) to the query are known as Input filters. In graphql-js, they are the GraphQLInputObjectType. This section covers how these Input filters are inferred.

Inferring input filters from example node values

The first step is to generate an input field for each type of field on the redux nodes. For example, we might want to query markdown nodes by their front matter author:

This step is handled by inferInputObjectStrctureFromNodes. First, we generate an example Value (see gqlTypes). For each field on the example value (e.g. author), we create a GraphQLInputObjectType with an appropriate name. The fields for Input Objects are predicates that depend on the value’s typeof result. E.g. for a String, we need to be able to query by eq, regex etc. If the value is an object itself, then we recurse, building its fields as above.

If the key is a foreign key reference (ends in ___NODE), then we find the field’s linked Type first, and progress as above (for more on how foreign keys are implemented, see gqlType). After this step, we will end up with an Input Object type such as .

Inferring input filters from plugin fields

Plugins themselves have the opportunity to create custom fields that apply to ALL nodes of a particular type, as opposed to having to explicitly add the field on every node creation. An example would be markdownRemark which adds a wordcount field to each node automatically. This section deals with the generation of input filters so that we can query by these fields as well. E.g:

Plugins add custom fields by implementing the setFieldsOnGraphQLNodeType API. They must return a full GraphQLObjectType, complete with resolve function. Once this API has been run, the fields are passed to inferInputObjectStructureFromFields, which will generate input filters for the new fields. The result would look something like:

As usual, the input filter fields (eq, lt, gt, etc) are based on the type of the field (Int in this case), which is defined by the plugin.

Merged result

Now that we’ve generated input fields from the redux nodes and from custom plugin fields, we merge them together. E.g

Edit this page on GitHub