Schema Customization for gatsby-transformer-csv & gatsby-transformer-json
See original GitHub issueDescription
Neither gatsby-transformer-csv nor gatsby-transformer-json honor Schema Customization.
Also, there is inconsistent inference of fields for these 2 transformers.
Steps to reproduce
- Install the Gatsby starter.
- Install & configure both gatsby-transformer-csv & gatsby-transformer-json
- 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
}
]
- npm run develop
At this point there are issues with the 4-digit field in GraphQL being assigned different field types (See below)
- 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)
}
- npm run develop again
No change in results.
Expected result
- I would expect the 2 transformers to infer the same data as the same type.
- I would not expect a 4-digit number to be inferred as type Date.
- 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Np! In case you missed it, a field’s type name is listed in the right sidebar in the graphql explorer:
Hi @melpers, in
createSchemaCustomization
hook, type name has to be in title case. Also your data doesn’t havedate
, butyear
:It works properly for me after fixing these. Perhaps docs need to make this more clear?