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

feat(gatsby): Refine typing on createPages’ graphql function #14575

Merged
merged 2 commits into from
Jul 2, 2019

Conversation

ryanditjia
Copy link
Contributor

Description

This PR expands the typing on createPages’ graphql function:

  • Accepts TData as generic
  • Accepts query: string as first param
  • Accepts variables?: Record<string, unknown> as second param
  • Returns errors?: any and data?: TData

Below is example code that will be properly typed with this PR merged.

gatsby-node.ts

import { GatsbyNode } from 'gatsby'
import { loadPagesQuery, loadPagesQueryVariables } from './src/types/__generated__/loadPagesQuery'

export const gatsbyNode: GatsbyNode = {
  createPages: async ({ graphql, actions }) => {
    const { createPage } = actions

    const oneThousandPosts = await graphql<loadPagesQuery, loadPagesQueryVariables>(
      `
        query loadPagesQuery($limit: Int!) {
          allMarkdownRemark(limit: $limit) {
            edges {
              node {
                frontmatter {
                  slug
                }
              }
            }
          }
        }
      `,
      { limit: 1000 }, // this will be typed
    )

    if (oneThousandPosts.errors) {
      throw oneThousandPosts.errors
    }

    oneThousandPosts.data.allMarkdownRemark.edges.forEach(edge => {
      createPage({
        path: `/blog/${edge.node.frontmatter.slug}/`,
        ... rest of code
      })
    })
  },
}

@ryanditjia ryanditjia requested a review from a team as a code owner June 6, 2019 03:08
@ryanditjia
Copy link
Contributor Author

ryanditjia commented Jun 6, 2019

errors?: any could use better typing. I couldn’t track the piece of Gatsby code that’s producing this errors object, hence I’m not sure of the correct typing.

An assumption:
errors?: Array<Error>

@freiksenet freiksenet merged commit 5bedc01 into master Jul 2, 2019
@freiksenet
Copy link
Contributor

Thank you @ryanditjia!

@sidharthachatterjee sidharthachatterjee deleted the ryanditjia-patch-1 branch July 2, 2019 20:38
@sidharthachatterjee
Copy link
Contributor

Published in gatsby@2.12.1

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

Successfully merging this pull request may close these issues.

3 participants