gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.31k forks source link

Only a subset of MDX node fields are available to createResolvers for schema customization #32008

Closed klittlepage closed 3 years ago

klittlepage commented 3 years ago

Description

When attempting to customize my Gatsby graphql schema via createResolvers I noticed that only a subset of the fields provided by the Mdx node type are present. More specifically, it appears that anything declared via buildObjectType with an async resolver, e.g., mdxAST is absent.

Steps to reproduce

The Mdx node type is defined as:

type Mdx implements Node {
  rawBody: String!
  fileAbsolutePath: String!
  frontmatter: MdxFrontmatter
  slug: String
  body: String!
  excerpt(pruneLength: Int = 140, truncate: Boolean = false): String!
  headings(depth: HeadingsMdx): [MdxHeadingMdx]
  html: String
  mdxAST: JSON
  tableOfContents(maxDepth: Int): JSON
  timeToRead: Int
  wordCount: MdxWordCount
  id: ID!
  parent: Node
  children: [Node!]!
  internal: Internal!
  testSlug: JSON
}

gatsby-node.ts

const createResolvers = ({
  createResolvers,
}: {
  createResolvers: (args: Record<string, unknown>) => void;
}): void => {
  const testSlug = {
    type: 'JSON',
    resolve: async (source: any, args: any, context: any, info: any) => {
      const resolvedField = context.defaultFieldResolver(
        source,
        args,
        context,
        info
      );
      return {
        sourceKeys: Object.keys(source),
        resolvedField: Object.keys(resolvedField || {}),
        originalResolver: typeof info.originalResolver !== `undefined`,
      };
    },
  };
  createResolvers({
    Mdx: {
      testSlug,
    },
  });
};

export { createResolvers };
query MDXQuery {
  allMdx(limit: 1) {
    nodes {
      id
      testSlug
    }
  }
}

Expected result

All keys present on the node should be present.

Actual result

{
  "data": {
    "allMdx": {
      "nodes": [
        {
          "id": "a414efbd-f7ff-55d4-a6bb-f9acb7c1031d",
          "testSlug": {
            "sourceKeys": [
              "id",
              "children",
              "parent",
              "internal",
              "frontmatter",
              "excerpt",
              "exports",
              "rawBody",
              "fileAbsolutePath",
              "__gatsby_resolved"
            ],
            "resolvedField": [],
            "originalResolver": false
          }
        }
      ]
    }
  },
  "extensions": {}
}

Environment

System:
    OS: macOS 11.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - /usr/local/opt/node@14/bin/node
    Yarn: 2.4.2 - /usr/local/bin/yarn
    npm: 6.14.13 - /usr/local/opt/node@14/bin/npm
  Languages:
    Python: 3.8.5 - /Users/klittlepage/opt/miniconda3/bin/python
  Browsers:
    Chrome: 91.0.4472.106
    Firefox: 89.0
    Safari: 14.1.1
  npmPackages:
    gatsby: ^3.7.2 => 3.7.2
    gatsby-plugin-extract-schema: ^0.2.0 => 0.2.0
    gatsby-plugin-force-trailing-slashes: ^1.0.5 => 1.0.5
    gatsby-plugin-google-gtag: ^3.7.1 => 3.7.1
    gatsby-plugin-image: ^1.7.1 => 1.7.1
    gatsby-plugin-layout: ^2.7.1 => 2.7.1
    gatsby-plugin-manifest: ^3.7.1 => 3.7.1
    gatsby-plugin-material-ui: ^3.0.1 => 3.0.1
    gatsby-plugin-mdx: ^2.7.1 => 2.7.1
    gatsby-plugin-offline: ^4.7.1 => 4.7.1
    gatsby-plugin-react-helmet: ^4.7.1 => 4.7.1
    gatsby-plugin-react-svg: ^3.0.1 => 3.0.1
    gatsby-plugin-robots-txt: ^1.6.2 => 1.6.2
    gatsby-plugin-sharp: ^3.7.1 => 3.7.1
    gatsby-plugin-sitemap: ^4.3.1 => 4.3.1
    gatsby-plugin-typescript: ^3.7.1 => 3.7.1
    gatsby-plugin-web-font-loader: ^1.0.4 => 1.0.4
    gatsby-remark-copy-linked-files: ^4.4.1 => 4.4.1
    gatsby-remark-images: ^5.4.1 => 5.4.1
    gatsby-source-filesystem: ^3.7.1 => 3.7.1
    gatsby-transformer-sharp: ^3.7.1 => 3.7.1
    gatsby-transformer-yaml: ^3.7.1 => 3.7.1
LekoArts commented 3 years ago

Hi, thanks for the issue!

It's the same issue as https://github.com/gatsbyjs/gatsby/issues/30429 and unfortunately expected behavior at the moment