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.

[gatsby-transformer-json] access all json file in subdirectories within a folder

See original GitHub issue

Description

I would like to access all transformed JSON within a folder, where each json file is located within a subdirectory and is named project.json. However, this only allows access via allProjectNameJson. I will not know the projectName (folder name) at runtime to add it to my query.

Steps to reproduce

CodeSandbox I built this sandbox with the required gatsby-config.js entries for gatsby-transformer-json and the path to ${dir}/src/data. Unfortunately, graphQl isn’t updating and doesn’t see the json files in their project directories.

Expected result

I can query allProjectsJson to access project1’s project.json and project2’s project.json.

Actual result

I can only query allProject1Json or allProject2Json.

Environment

  System:
    OS: Linux 4.4 Ubuntu 18.04.2 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Shell: 4.4.19 - /bin/bash
  Binaries:
    Node: 10.16.3 - /usr/bin/node
    Yarn: 1.21.1 - /usr/bin/yarn
    npm: 6.9.0 - /usr/bin/npm
  Languages:
    Python: 2.7.15+ - /usr/bin/python
  Browsers:
    Chrome: 76.0.3809.132
    Firefox: 68.0.2
  npmPackages:
    gatsby: ^2.18.21 => 2.18.21
    gatsby-image: ^2.2.38 => 2.2.38
    gatsby-plugin-netlify-cms: ^4.1.37 => 4.1.37
    gatsby-plugin-netlify-cms-paths: ^1.3.0 => 1.3.0
    gatsby-plugin-postcss: ^2.1.19 => 2.1.19
    gatsby-plugin-sharp: ^2.3.13 => 2.3.13
    gatsby-source-filesystem: ^2.1.46 => 2.1.46
    gatsby-transformer-json: ^2.2.25 => 2.2.25
    gatsby-transformer-sharp: ^2.3.12 => 2.3.12
  npmGlobalPackages:
    gatsby-cli: 2.8.27

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
TylerBarnescommented, Jan 21, 2020

Ahh, I see what you mean 😃 luckily gatsby-transformer-json has a plugin option that will help get you unstuck!

It’s the typeName option. You should be able to check for a field on each JSON node, and use that as the type name. Something like this:

    {
      resolve: `gatsby-transformer-json`,
      options: {
        typeName: ({ node, object, isArray }) =>
          object.project ? `Project` : `Json`,
      },
    },

That way anything with the field project defined will show up as allProject { ... }, where any other json files will show up as allJson { ... }

{
  allProject {
    nodes {
      project
    }
  }
}
{
  "data": {
    "allProject": {
      "nodes": [
        {
          "project": "project1"
        },
        {
          "project": "project2"
        }
      ]
    }
  }
}
2reactions
cyrilfcommented, Mar 3, 2020

I’m experiencing the same issue as @caseykey As a workaround I used JsonFiles instead of Json

So in my case it’s:

{
  resolve: `gatsby-transformer-json`,
  options: {
    typeName: ({ object }) => object.typeName || "JsonFiles"
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL Query access all transformed json files within a ...
I have my projects split into individual json files within respective folders containing the project.json file and an images folder. Here is ...
Read more >
Loop over json files inside subdirectories - Ask Ubuntu
1 · Hi, I then get ls: cannot access /data/Mydata/*//*. · are you sure this data/Mydata directory is under / ? is it...
Read more >
gatsby-transformer-json
gatsby-transformer-json. Parses raw JSON strings into JavaScript objects e.g. from JSON files. Supports arrays of objects and single objects.
Read more >
Recursively read json files - Alteryx Community
Hi All, I have a directory that contains several subdirectories with JSON files in them. Setting up the "Directory" tool is pretty easy....
Read more >
Sourcing local .json files with Gatsby - Paul Scanlon
Hi all, I recently had a discussion with Rahul on Twitter regarding building a Gatsby blog using only locally sourced .json files…
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