imgix / gatsby

A simple yet powerful integration between Gatsby and imgix
BSD 2-Clause "Simplified" License
30 stars 6 forks source link

Is it possible to use customized GraphQL fields? #273

Open YusukeSano opened 1 year ago

YusukeSano commented 1 year ago

Before you submit:

Question I have added a field for image URL using Gatsby createResolvers API.

// gatsby-node.js
exports.createResolvers = ({ createResolvers }) => {
  createResolvers({
    Post: {
      imageUrl: {
        type: 'String!',
        resolve(source) {
          const getImageUrl = (text) => {
            // The process of extracting the image URL from text
          }
          return getImageUrl(source.text)
        },
      },
    },
  })
}

I can retrieve the value of the field using the following query:

export const query = graphql`
  {
    allPost {
      nodes {
        text # Text containing the image URL
        imageUrl # https://...
      }
    }
  }
`

I have configured the field, but when I execute the query, an error occurs.

// gatsby-config.js
module.exports = {
  //...
  plugins: [
    //... other plugins
    {
      resolve: `@imgix/gatsby`,
      options: {
        defaultImgixParams: { auto: ['compress', 'format'] },
        fields: [
          {
            nodeType: 'Post',
            fieldName: 'imgixImage',
            rawURLKey: 'imageUrl',
          },
        ],
      },
    },
  ],
};

Error:

Error when resolving URL value for node type Post. This probably means that the rawURLKey function in gatsby-config.js is incorrectly set. Please read this project's README for detailed instructions on how to set this correctly.

Is it possible to use customized GraphQL fields?