Gatsby - runQuery inside createResolver using group

Viewed 376

I would like to use createResolvers to run a query using group, but I don't think this is possible at the moment. According to Accessing Gatsby’s data store from field resolvers, it seems runQuery currently accepts filter and sort query arguments, but not group. I was hopeful that runQuery would support group (based on https://github.com/gatsbyjs/gatsby/issues/15453), but I can't find any examples of grouping using runQuery

If possible, how can I use createResolvers to run a query using group for the following graphql query?

  query {
    allFile(filter: { internal: { mediaType: { eq: "text/markdown" } } }) {
      group(field: sourceInstanceName) {
        fieldValue
        totalCount
      }
    }
  }

a

For context, I have 2 folders (coding & recipes) that I am sourcing markdown files from. Here is my gatsby-config.js:

const path = require('path')

module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `coding`,
        path: path.resolve(__dirname, `src/contents/coding`),
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `recipes`,
        path: path.resolve(__dirname, `src/contents/recipes`),
      },
    },
    {
      resolve: `gatsby-transformer-remark`,
    },
  ],
}

Currently, I am able to use runQuery to run queries using filter. Here is my gatsby-node.js:

exports.createResolvers = ({ createResolvers }) => {
  createResolvers({
    Query: {
      recipes: {
        type: `[File!]!`,
        resolve(source, args, context, info) {
          return context.nodeModel.runQuery({
            query: {
              filter: {
                internal: { mediaType: { eq: 'text/markdown' } },
                sourceInstanceName: { eq: 'recipes' },
              },
            },
            type: `File`,
            firstOnly: false,
          })
        },
      },
      coding: {
        type: `[File!]!`,
        resolve(source, args, context, info) {
          return context.nodeModel.runQuery({
            query: {
              filter: {
                internal: { mediaType: { eq: 'text/markdown' } },
                sourceInstanceName: { eq: 'coding' },
              },
            },
            type: `File`,
            firstOnly: false,
          })
        },
      },
    },
  })
}

b

But now I want to use createResolver to runQuery using group. Is this possible? If so, how?

I've produced a minimal repo at https://github.com/kimbaudi/gatsby-group-query

Currently, the home page (src/pages/index.jsx) is displaying the folders (coding & recipes) and the count of markdown files in that folder:

home page

using the following page query:


export const query = graphql`
  query {
    allFile(filter: { internal: { mediaType: { eq: "text/markdown" } } }) {
      group(field: sourceInstanceName) {
        fieldValue
        totalCount
      }
    }
  }
`

and I would like to create a resolver that groups and use it in place of the above page query.

0 Answers
Related