Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1] GraphQL can't find custom fields on type "ImageSharp" (exif/metadata related) #7015

Closed
Patapinh0 opened this issue Aug 4, 2018 · 1 comment

Comments

@Patapinh0
Copy link

Hey everyone !

I am trying to reproduce the example given in this issue to load images EXIF through GraphQL.
Problem : GraphiQL gives me an error, telling me that it

"Cannot query field "fields" on type "ImageSharp"."

I've been messing around with it all morning, with no success. GraphQL actually see the images in the folder, but can't find any custom fields (exif) related to the nodes.

Being a quite a bit newbish when it comes to React, Gatsby and GraphQL, I was hoping that someone here could give me a hint on what seems to/could be wrong.

First time posting an issue on Github too, so if there's any additional information I forgot to include, just say the word and I'll include them asap.

Thanks a lot in advance <3

Environment (if relevant)

System:
OS: Windows 7
CPU: x64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
Binaries:
npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD
npmPackages:
gatsby: ^1.9.273 => 1.9.273
gatsby-image: ^1.0.54 => 1.0.54
gatsby-link: ^1.6.45 => 1.6.45
gatsby-plugin-page-transitions: ^1.0.7 => 1.0.7
gatsby-plugin-react-helmet: ^2.0.11 => 2.0.11
gatsby-plugin-react-next: ^1.0.11 => 1.0.11
gatsby-plugin-sass: ^1.0.26 => 1.0.26
gatsby-plugin-sharp: ^1.6.48 => 1.6.48
gatsby-source-filesystem: ^1.5.39 => 1.5.39
gatsby-transformer-sharp: ^1.6.27 => 1.6.27

File contents (if changed)

gatsby-config.js:


module.exports = {
  siteMetadata: {
    title: 'Gatsby Default Starter',
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    "gatsby-plugin-react-next",
    "gatsby-plugin-page-transitions",
    "gatsby-plugin-sass",
    {
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/src/`,
        name: "src",
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/src/images/categorie-photo/`,
        name: "images",
        plugins: [
          "gatsby-plugin-sharp"
        ]
      },
    },
    "gatsby-plugin-sharp",
    "gatsby-transformer-sharp",

  ],
}

package.json:

{
  "name": "gatsby-starter-default",
  "description": "Gatsby default starter",
  "version": "1.0.0",
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
  "dependencies": {
    "bootstrap": "^4.1.2",
    "fast-exif": "^1.0.1",
    "gatsby": "^1.9.273",
    "gatsby-image": "^1.0.54",
    "gatsby-link": "^1.6.45",
    "gatsby-plugin-page-transitions": "^1.0.7",
    "gatsby-plugin-react-helmet": "^2.0.11",
    "gatsby-plugin-react-next": "^1.0.11",
    "gatsby-plugin-sass": "^1.0.26",
    "gatsby-plugin-sharp": "^1.6.48",
    "gatsby-source-filesystem": "^1.5.39",
    "gatsby-transformer-sharp": "^1.6.27",
    "google-map-react": "^1.0.5",
    "informed": "^1.6.0",
    "lodash": "^4.17.10",
    "prop-types": "^15.6.2",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-helmet": "^5.2.0",
    "react-image-lightbox": "^5.0.0",
    "react-image-masonry": "^0.3.7",
    "react-popper": "^1.0.0",
    "react-scroll-up": "^1.3.3",
    "react-sticky-footer": "0.1.0-rc3",
    "react-svg": "^4.1.8",
    "react-transition-group": "^2.4.0",
    "reactstrap": "^6.3.0",
    "typeface-aleo": "0.0.44"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write 'src/**/*.js'",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "prettier": "^1.13.7",
    "react-masonry-css": "^1.0.11"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/gatsbyjs/gatsby-starter-default"
  }
}

gatsby-node.js:


const fastExif = require('fast-exif');
const get = require('lodash/get');

exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
  const { createNodeField } = boundActionCreators;

if(node.internal.type === 'ImageSharp' && node.id.includes("/src/images/categorie-photo")) {
      const absolutePath = node.id.split(' ')[0];
      fastExif.read(absolutePath)
        .then((exifData) => {
          const title        = get( exifData, [ 'image', 'ImageDescription' ], null );
          const location     = get( exifData, [ 'image', 'DocumentName' ], null );
          const copyright = get( exifData, [ 'exif', 'Copyright' ], null );
          const categoryData = get( exifData, [ 'exif', 'ImageHistory' ], null );
          const categories   = categoryData === null ? [ 'uncategorized' ] : categoryData.split( ',' );
          const iso          = get( exifData, [ 'exif', 'ISO' ], null );
          const model        = get( exifData, [ 'exif', 'LensModel' ], null );
          const fstop        = get( exifData, [ 'exif', 'FNumber' ], null );
          const focalLength  = get( exifData, [ 'exif', 'FocalLength' ], null );

              createNodeField({
                node,
                name: 'exif',
                value: {title, location, copyright, categories, technical: {iso, model, fstop, focalLength}}
              });
        })

        .catch((err) => console.error(err));
  }
}

GraphQL request :


  query PhotosQuery {

    pictures: allImageSharp(filter: { id: { regex: "//images/categorie-photo//" } }) {
       
      edges {
        
          node {
            
            id
            internal {
              contentDigest
              type
              owner
              
            }
            original {
              width
              height
              src
            },
            sizes(maxWidth: 1600) {
              src,
              srcSet,
              sizes
            },
            resolutions(width: 200) {
            src,
            srcSet,
            width,
            height,
            aspectRatio,
            base64
            }
            fields {
              exif {
                title
                location
                categories
                copyright
                technical {
                  iso
                  model
                  fstop
                  focalLength
                }
              }
            }

            
    
  	}
      }
   }
 }
@KyleAMathews
Copy link
Contributor

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants