datopian / data-explorer-graphql

GraphQL based Data Explorer
https://datopian.github.io/data-explorer-graphql
MIT License
5 stars 2 forks source link

Hyphens are disallowed by GraphQL spec #49

Closed anuveyatsu closed 3 years ago

anuveyatsu commented 3 years ago

When a field name has a hyphen, it breaks the app since it is disallowed by GraphQL spec.

Example: https://datopian.github.io/data-explorer-graphql/?path=/story/templates--power-system-right-now

Tasks

Analysis

Resolver enables us to resolve a value of a type or a field in a schema. A simple example can be like this:

const typeDefs = gql`
  type User { 
       id
       username
   }
`

const resolver = {
    User: {
      username: (parent, args, context, info) =>{
            //perform custom operation
            return "myname" // this will always return "myname" no matter the query
      }
  }
}

For every field to be resolve we create a resolver function (like the one created for username above) that takes in parent (if a type is reference in another type. I will show an example below), args: (for argument passed in like transmisonlines({order_by:{}}), order_by is an arg), context: (graphql and app context to be share among all resolvers) and info: (contains resolver properties).

Resolver to fix the Graphql spec would be possible if the graphql query is created like this from the server side:

const typeDefs = gql`
  type powersystemrightnow { 
    Minutes1UTC : datetime
    Minutes1DK : datetime
    CO2Emission: number
    ProductionGe100MW: number
    ProductionLt100MW: number
    SolarPower: number: number
    OffshoreWindPower: number
    OnshoreWindPower: number
    Exchange_DK1-DK2: number
    extrafield: extrafield
   }

 type extrafield {
    extrafield: number
  }
`
const resolver = {
  extrafield: (parent, args, context, info) => {
     // since extrafield is a child node to powersystemright now
    // it has access to all of it fields and their values
   return parent["Exchange_DK1-DK2"]
 }
}

This method above is okay but might involve some changes in the frontend since we are automatically generating graphql query from the schema and input fields using graphql-query-builder

NOTE if the above type is created manually containing field like Exchange_DK1-DK2 it will show error on the server side also.

Proposed solution

Since this a spec error base on the field name convention, I assume the field is generated from hasura converting postgress table to graphql. if so, we can customize the field name (e.g Exchange_DK1-DK2) to something else. This hasura blog shows how to do that: https://hasura.io/blog/customising-alias-graphql-fields-with-hasura/#:~:text=Server-,Side,-Customisation%20with%20Hasura

anuveyatsu commented 3 years ago

@steveoni thanks for analysis. I've implemented fix here: https://github.com/datopian/data-explorer-graphql/commit/9f6319ab72b141240c3770b09eb6ef8cf59cccd7

I also did some update in Hasura server so now it works:

https://datopian.github.io/data-explorer-graphql/?path=/story/templates--power-system-right-now

anuveyatsu commented 3 years ago

Closing this as FIXED. Note that you need to handle hyphens in Hasura server as well as described in the analysis of this issue.