Gatsby: Schema object creation with underscore in field name always returns field value null

Created on 3 Jul 2020  路  3Comments  路  Source: gatsbyjs/gatsby

Description

Creating a schema object with fields containing underscore (see x_qlik_stability below) does not fetch the data. The field can be queried for, but the value is always null.

Steps to reproduce

I have this in gatsby-node.js

exports.createSchemaCustomization = ({ actions, schema }) => {
  const typeDefs = [
    schema.buildObjectType({
      name: 'tagsJson',
      fields: {
        title: 'String',
        name: 'String',
        version: 'String',
        description: 'String',
      },
    }),
    schema.buildObjectType({
      name: 'RestJson',
      fields: {
        x_qlik_stability: 'String',
        tags: 'tagsJson',
      },
      interfaces: ['Node'],
      extensions: {
        infer: false,
      },
    }),
  ];
  actions.createTypes(typeDefs);
};

Expected result

It is expected that the actual x_qlik_stability field value is fetched.

Actual result

The x_qlik_stability field value is always null.

Environment

System:
    OS: macOS 10.15.4
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.4.0 - /var/folders/tp/g7hx5p9s3tg4kpqhf66zcxvn_swy3m/T/yarn--1593758726969-0.017458157550566344/node
    Yarn: 1.22.4 - /var/folders/tp/g7hx5p9s3tg4kpqhf66zcxvn_swy3m/T/yarn--1593758726969-0.017458157550566344/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 83.0.4103.116
    Firefox: 77.0.1
    Safari: 13.1
  npmPackages:
    gatsby: 2.23.17 => 2.23.17
    gatsby-cli: 2.12.55 => 2.12.55
    gatsby-image: 2.4.7 => 2.4.7
    gatsby-link: 2.4.11 => 2.4.11
    gatsby-plugin-google-tagmanager: 2.3.5 => 2.3.5
    gatsby-plugin-manifest: 2.4.12 => 2.4.12
    gatsby-plugin-material-ui: 2.1.9 => 2.1.9
    gatsby-plugin-mdx: 1.2.15 => 1.2.15
    gatsby-plugin-offline: 3.2.16 => 3.2.16
    gatsby-plugin-react-helmet: 3.3.6 => 3.3.6
    gatsby-plugin-sass: 2.3.4 => 2.3.4
    gatsby-plugin-sharp: 2.6.10 => 2.6.10
    gatsby-plugin-sitemap: 2.4.4 => 2.4.4
    gatsby-plugin-styled-components: 3.3.4 => 3.3.4
    gatsby-react-router-scroll: 3.0.10 => 3.0.10
    gatsby-remark-autolink-headers: 2.3.4 => 2.3.4
    gatsby-remark-copy-linked-files: 2.3.3 => 2.3.3
    gatsby-remark-embed-snippet: 4.3.7 => 4.3.7
    gatsby-remark-images: 3.3.13 => 3.3.13
    gatsby-remark-prismjs: 3.5.4 => 3.5.4
    gatsby-source-filesystem: 2.3.11 => 2.3.11
    gatsby-transformer-json: 2.4.7 => 2.4.7
    gatsby-transformer-remark: 2.8.17 => 2.8.17
    gatsby-transformer-sharp: 2.5.5 => 2.5.5
needs reproduction bug

Most helpful comment

You can write a resolver for the field to handle names like that.

    schema.buildObjectType({
      name: 'RestJson',
      fields: {
        // you can also rename it to qlikStability if you want
        x_qlik_stability: {
          type: 'String',
          resolve: (parent) => {
             return parent['x-qlik-stability']
          }
        }
        tags: 'tagsJson',
      },
      interfaces: ['Node'],
      extensions: {
        infer: false,
      },
    }),

I hope that helps! I'll close this issue, feel free to open it if you have more questions.

All 3 comments

Hi!

I've tried reproducing a bug locally, but I can get fields with underscores in them. Could you provide (at least partially) the .json file that you are using for that data?

Thanks! 馃挏

Hi @freiksenet!

When preparing a .json to provide, I realised that it is partially PEBCAK and another issue.
This is a part of the json:

{
    "openapi": "3.0.0",
    "x-qlik-stability": "stable",
    "x-qlik-visibility": "public",
    "info": {
        "version": "1.2.3",
        "title": "API keys",
        "description": "Description."
    },
    "tags": {
        "name": "api-keys",
        "description": "Description.",
        "title": "API keys",
        "version": "1.2.3"
    }
}

The field name is actually x-qlik-stability and not x_qlik_stability. When querying for them I use x_qlik_stability.. but that I cannot use when creating the schema. I also can't use x-qlik-stability. If using "x-qlik-stability" it gives me Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "x-qlik-stability" does not.

GAH.. do you possibly have any tips on what to do here? Guess this issue can be closed since the underscore probably works just fine.

You can write a resolver for the field to handle names like that.

    schema.buildObjectType({
      name: 'RestJson',
      fields: {
        // you can also rename it to qlikStability if you want
        x_qlik_stability: {
          type: 'String',
          resolve: (parent) => {
             return parent['x-qlik-stability']
          }
        }
        tags: 'tagsJson',
      },
      interfaces: ['Node'],
      extensions: {
        infer: false,
      },
    }),

I hope that helps! I'll close this issue, feel free to open it if you have more questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andykais picture andykais  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments

signalwerk picture signalwerk  路  3Comments

benstr picture benstr  路  3Comments

magicly picture magicly  路  3Comments