malcolm-kee / gatsby-source-mysql

Source plugin for pulling data into Gatsby from MySQL database
https://www.npmjs.com/package/gatsby-source-mysql
18 stars 10 forks source link

graphQL schema and NULL values #11

Closed korukugashi closed 5 years ago

korukugashi commented 5 years ago

Hi,

I am facing an issue with the management of null values: when a SQL query sometimes returns the null value for a column, Gatsby returns a compilation error indicating that it does not find the GraphQL field matching to this column

Unknown field 'new_sma_id' on type 'MysqlNewProduct!'. Source: document family file: GraphQL request

I would expect Gatsby to return this field with a null value, otherwise I would have to modify all my SQL queries to add IFNULL(mycolumn, "")in the SELECT statement to make sure that if we encounter a null value the field is still present. In addition in some cases we may need both values: NULL and empty string.

Following gatsbyjs/gatsby#6800, I tried to defined a type on my query (see schema customization), but I didn't achieve to match my type with the query.

Do you have any idea? Thanks

malcolm-kee commented 5 years ago

Is your project open source?

korukugashi commented 5 years ago

Unfortunately not.

Below is an example that reproduce the issue: example.zip

There is some test data in InsertExampleData.sql to import into a database

The example reproduces the problem well when all values are null, but not when a row is not null unlike my project, I will look for the difference

malcolm-kee commented 5 years ago

Assuming new_sma_id is a String, try adding the following in your gatsby-node.js


exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = [
    `type MysqlNewProduct implements Node {
      new_sma_id: String
    }`,
    // add other type definition here that have the same null value scenario
  ];

  createTypes(typeDefs);
}

Let me know if this solve your problem, else let me know what's the message you're seeing.

korukugashi commented 5 years ago

Great, it seems to work, thanks! I don't understand why that wasn't the case earlier, maybe it was clean gatsby that was required..

In my project I have to define in the Type all the columns even those that are never null, however in the example project it works well by defining only the columns that are null... I will look for the differences.

malcolm-kee commented 5 years ago

I had a typo previously, it should be in gatsby-node.js, not gatsby-config.js.