question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema Customization for gatsby-transformer-csv & gatsby-transformer-json

See original GitHub issue

Description

Neither gatsby-transformer-csv nor gatsby-transformer-json honor Schema Customization.

Also, there is inconsistent inference of fields for these 2 transformers.

gatsby-csv-issue

Steps to reproduce

  1. Install the Gatsby starter.
  2. Install & configure both gatsby-transformer-csv & gatsby-transformer-json
  3. Add one .csv and one .json file under /src/data/csv/ and /src/data/json respectively. Include at least 1 field with a 4-digit number in each file.
// CSV file
name,year,code
Test1,2019,9999
Test2,2017,1600
Test3,2016,7654
// JSON file
[
  {
    "name": "Test1",
    "year": 2019,
    "code": 9999
  },
  {
    "name": "Test2",
    "year": 2017,
    "code": 1600
  },
  {
    "name": "Test3",
    "year": 2016,
    "code": 7654
  }
]
  1. npm run develop

At this point there are issues with the 4-digit field in GraphQL being assigned different field types (See below)

  1. In gatsby-node.js add a createSchemaCustomization definition to declare the CSV & JSON types created to use type Int! for the 4-digit number field.
// gatsy-node.js
module.exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions
  const typeDefs = `
    type csvTestCsv implements Node @dontInfer {
      name: String!
      date: Date! @dateformat
      code: Int!
    }
    type jsonTestJson implements Node @dontInfer {
      name: String!
      date: Date! @dateformat
      code: Int!
      }
  `
  createTypes(typeDefs)
}
  1. npm run develop again

No change in results.

Expected result

  1. I would expect the 2 transformers to infer the same data as the same type.
  2. I would not expect a 4-digit number to be inferred as type Date.
  3. I would expect both transformers to honor the schema customization.

Actual result

After Step 4 the 4-digit field in GraphQL is being assigned different field types from the two transformers:

  • The CSV is assigned the type Date.
    • Note that if a 5-digit number is used, it is assigned the type String.
  • The JSON transformer assigns the type Number.

Build message:

warn There are conflicting field types in your data.

If you have explicitly defined a type for those fields, you can safely ignore this warning message.
Otherwise, Gatsby will omit those fields from the GraphQL schema.

If you know all field types in advance, the best strategy is to explicitly define them with the `createTypes` action, and skip inference with the `@dontInfer` directive.
See https://www.gatsbyjs.org/docs/actions/#createTypes
SitePage.context.code:
 - type: date
   value: '9999'
 - type: number
   value: 9999

error  Variable "$code" of type "Int!" used in position expecting type "Date"  graphql/template-strings

Environment

System: OS: macOS 10.14.6 CPU: (4) x64 Intel® Core™ i5-4670 CPU @ 3.40GHz Shell: 3.2.57 - /bin/bash Binaries: Node: 10.15.3 - /usr/local/bin/node npm: 6.4.1 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/local/bin/python Browsers: Chrome: 80.0.3987.163 Firefox: 75.0 Safari: 13.1 npmPackages: gatsby: ^2.20.12 => 2.20.12 gatsby-image: ^2.3.1 => 2.3.1 gatsby-plugin-manifest: ^2.3.3 => 2.3.3 gatsby-plugin-offline: ^3.1.2 => 3.1.2 gatsby-plugin-react-helmet: ^3.2.2 => 3.2.2 gatsby-plugin-sharp: ^2.5.3 => 2.5.3 gatsby-source-filesystem: ^2.2.2 => 2.2.2 gatsby-transformer-csv: ^2.2.1 => 2.2.1 gatsby-transformer-json: ^2.3.1 => 2.3.1 gatsby-transformer-sharp: ^2.4.3 => 2.4.3 npmGlobalPackages: gatsby-cli: 2.5.12

Test Repo: https://github.com/melpers/gatsby-csv-test

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
d4rekanguokcommented, Apr 17, 2020

Np! In case you missed it, a field’s type name is listed in the right sidebar in the graphql explorer:

image
1reaction
d4rekanguokcommented, Apr 17, 2020

Hi @melpers, in createSchemaCustomization hook, type name has to be in title case. Also your data doesn’t have date, but year:

// gatsy-node.js
module.exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions
  const typeDefs = `
-   type csvTestCsv implements Node @dontInfer {
+   type CsvTestCsv implements Node @dontInfer {
      name: String!
-     date: Date! @dateformat
+     year: Date! @dateformat
      code: Int!
    }

-   type jsonTestJson implements Node @dontInfer {
+   type JsonTestJson implements Node @dontInfer {
      name: String!
-     date: Date! @dateformat
+     year: Date! @dateformat
      code: Int!
      }
  `
  createTypes(typeDefs)
}

It works properly for me after fixing these. Perhaps docs need to make this more clear?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the GraphQL Schema - Gatsby
This guide is aimed at plugin authors, users trying to fix GraphQL schemas created by automatic type inference, developers optimizing builds for larger...
Read more >
Gatsby Graphql schema customization for beginners
Learn how to replace data in the existing Graphql fields, add new ones and define relationships between objects.
Read more >
Modify Gatsby's GraphQL data types using ... - Paul Scanlon
Modify Gatsby's GraphQL data types using createSchemaCustomization · node. This is the new node type sourced from the NASA API and already exists ......
Read more >
Gatsby Data Relationships With Foreign-Key Fields
This tutorial walks through how to customize the GraphQL layer to create ... Secondly, inside the options for our gatsby-transformer-json we ...
Read more >
Customizing the GraphQL schema of a Gatsby project with ...
This example uses json files with gatsby-transformer-json . I have tried with gatsby-transformer-yaml too with the same results. Is it possible ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found