Gatsby: [gatsby-transformer-json] add graphql-type-json to allow better handling of JSON files

Created on 1 Feb 2020  路  5Comments  路  Source: gatsbyjs/gatsby

Summary

I'm currently trying to add my package.json file to GraphQL. I already created a plugin which sources single files instead of directories with helpers from gatsby-source-filesystem. But this plugin does not allow me to get all from the key dependencies since GraphQL is explicit. After searching, I found a type plugin for graphql which adds this cap.

https://github.com/taion/graphql-type-json

It would be great if this plugin could be added to gatsby-transformer-json or if we could extend the documentation so that someone can add it by himself.

Here is an Apollo tutorial: https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/#using-a-package

I tried to add it with createResolvers but still get some errors (I know it tells we what exactly is wrong but I don't know what exactly it means). I'm just starting with GraphQL in deep and don't really know how to add custom scalars. Would appreciate any help and would create a PR if I get a working version for my portfolio as soon as I get free time for it.

ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the createResolvers lifecycle:

Cannot find ObjectTypeComposer with name JSON

Basic example

If the proposal involves a new or changed API, include a basic code example. Omit this section if it's not applicable.

Motivation

This would enable easier transformation of JSON files and handling like when using require syntax.

Most helpful comment

@muuvmuuv

  • the content of your package.json file should be available out of the box with:
{
  sitePlugin(name: {eq: "default-site-plugin"}) {
    packageJson {
      dependencies {
        name
        version
      }
    }
  }
}
  • Gatsby (via graphql-compose) already includes a JSON scalar. Did that not work for you?

All 5 comments

If the only purpose is to query all dependencies from the local package.json, would it be simpler to write a custom plugin that transform the dependencies field into an array instead? Then you can query it like so:

query {
  packageJson {
    allDependencies {
      name
      version
    }
  }
}

That plugin could looks like this: https://gist.github.com/d4rekanguok/cc54bf49f46246b19ced4a03292a0012

Thank you! Yes this is the only purpose ATM but I still think this would add great benefits.

@muuvmuuv

  • the content of your package.json file should be available out of the box with:
{
  sitePlugin(name: {eq: "default-site-plugin"}) {
    packageJson {
      dependencies {
        name
        version
      }
    }
  }
}
  • Gatsby (via graphql-compose) already includes a JSON scalar. Did that not work for you?

@stefanprobst that is even easier! Didn't know that!

Thanks @stefanprobst 鉂わ笍

@muuvmuuv Looks like this is resolved for the moment. Closing this issue for now. Thanks!

Was this page helpful?
0 / 5 - 0 ratings