How to use createResovers to convert a Markdown FIELD (not a file) to HTML in Gatsby

rmcsharry

The standard process for converting MD to HTML in Gatsby is for complete files on the local system.

I need to convert a specific field, NOT a file. The top related question is this one, but as you can see that is using Contentful, which now provides a plugin to solve it.

The API I am using (Airtable) is returning a field that contains Markdown.

The recommended solution is to convert using a resolver...but I cannot fully understand the Gatsby docs on the topic.

The error I see when building is this:

UNHANDLED REJECTION Airtable.FAQ provided incorrect OutputType:
 'Object({ resolve: [function resolve], extensions: Object({ createdFrom: "createResolvers" }) })'

I think I am close, but I don't know if I am meant to create a new type or actually what the resolver is meant to return...a new field?

The field in question is FAQ, which you can see in this example of a query in the GraphQL explorer:

query MyQuery {
  allAirtableManufacturer(filter: {data: {Premium: {eq: true}}}) {
    edges {
      node {
        data {
          Premium
          Manufacturer
          Premium_Manufacturers {
            recordId
            data {
              FAQ
              Downloads_File_Name
              Is_Supplier
            }
            internal {
              type
            }
          }
        }
        recordId
        queryName
      }
    }
  }
}

My understanding is that the resolver can/should add a new field with the markdown content converted to html.

So here is my resolver code, where you can see I am trying to add a field called "FAQ_html" to the Airtable node:

  createResolvers({
    Airtable: {
      FAQ_html: {
        resolve(source: any, args: any, context: any, info: any) {
          return remark().use(html).processSync(source.data.FAQ).contents
        },
      },
    }
  })

My gatsby-config for airtable is:

    resolve: `gatsby-source-airtable`,
      options: {
        apiKey: process.env.AIRTABLE_API_KEY,
        concurrency: 5,
        tables: [
          {
            baseId: `appP5vBdAitw6yyDH`,
            tableName: `Manufacturers`,
            queryName: `Manufacturer`,
            tableView: `AppView_Details_DONOTCHANGE`,
            tableLinks: [`Premium_Manufacturers`],
            separateNodeType: true,
            defaultValues: {
              Company_Description: '',
            },
          },
          {
            baseId: `appP5vBdAitw6yyDH`,
            tableName: 'Premium_Manufacturers',
            tableLinks: [`Manufacturers`],
          },

        ],

The table 'Premium_Manufacturers' is obviously linked as a child to 'Manufacturers'.

However, when I explore in GraphQL I also see them appear as a top-level node called 'Airtable', which I did not expect. You can see that here:

allAirtable {
    edges {
      node {
        data {
          FAQ
          Downloads_File_Name
          Last_update
          Is_Supplier
          Section_Name
          Section_No
          Name
          Cell_Number
          Email
          Rep_Name
          Technical_Rep_Name
          Consolidated_Rep
        }
      }
    }
  }

That's why my resolver uses 'Airtable' as the name of the node, but clearly it does not work.

I also tried changing the config to provide a separate node:

          {
            baseId: `appP5vBdAitw6yyDH`,
            tableName: 'Premium_Manufacturers',
            queryName: 'Premium',
            separateNodeType: true,
            tableLinks: [`Manufacturers`],
          },

so now 'allAirtable' becomes 'allAirtablePremium'.

I tried changing the resolver to use that:

  createResolvers({
    allAirtablePremium: {
      FAQ_html: {
        resolve(source: any, args: any, context: any, info: any) {
          return remark().use(html).processSync(source.data.FAQ).contents
        },
      },
    }
  })

But that throws a warning:

warn `createResolvers` passed resolvers for type `allAirtablePremium` that doesn't exist in the schema.

So clearly it doesn't like the 'all', so then I changed it to remove the 'all' like so:

  createResolvers({
    AirtablePremium: {
      FAQ_html: {
        resolve(source: any, args: any, context: any, info: any) {
          return remark().use(html).processSync(source.data.FAQ).contents
        },
      },
    }
  })

And I am back to the original error, which at least tells me there is something wrong with what I am trying to return (as the error clearly says 'wrong OutputType').

So what should that be returning?

Thanks in advance for any help!

UPDATE 1

The code now compiles, but I don't see my custom field.

This is the schema customisation (just create FAQ_HTML as an empty string to start off):

import { GatsbyNode, CreateSchemaCustomizationArgs } from 'gatsby'

export const createSchemaCustomization: GatsbyNode['createSchemaCustomization'] = async ({
  actions,
}: CreateSchemaCustomizationArgs) => {

  const { createFieldExtension, createTypes } = actions
  createFieldExtension({
    name: "FAQ_HTML",
    extend() {
      return ''
    },
  })

  const typeDefs = `
    type airtablePremium implements Node {
      FAQ_HTML: String @FAQ_HTML
    }
`

  createTypes(typeDefs)
}

The resolver is now like this:

  createResolvers({
    airtablePremium: {
      FAQ_HTML: {
        resolve(source: any, args: any, context: any, info: any) {
          console.log("SOURCE IS", source)
          const faqHtml = remark().use(html).processSync(source.FAQ).contents
          console.log("faqHtml IS", faqHtml)
          return faqHtml;
        },
      },
    },
  })

It compiles but the field does not show up in the GraphQL explorer. No console output either.

Derek Nguyen

After a nice chat with @rmcsharry, we confirmed that you'd need to create the type first.

createTypes(`
  type AirtablePremium implements Node @infer {
    FAQ_HTML: String
  }
`)

The biggest issue was the type name. In the code, it should be AirtablePremium vs airtablePremium. You can find out the type name in GraphiQL by clicking on the field name in the left column, or holding cmd and clicking on the field in your query.

You'd use the typename in createResolvers as well:

  createResolvers({
    AirtablePremium: {
      FAQ_HTML: {
        resolve(source: any, args: any, context: any, info: any) {
          ...
        },
      },
    },
  })

Update from @rmcsharry

After @Derek Nguyen's very helpful assistance, I found an alternative and easier solution.

If you want to avoid the hassle of creating types, you can go with a much simpler solution using the plugin markdown-to-jsx.

Then you simply wrap the incoming markdown field:

<Markdown>{field}</Markdown>

and it creates a react JSX component on the fly. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use HTML in Markdown?

From Dev

How to convert markdown to inline HTML?

From Dev

Use find command to convert markdown files to html

From Dev

Convert custom markdown to HTML?

From Dev

How can I make vim use html and markdown syntax highlighting on the same file?

From Dev

How do you apply styling to array from a markdown file for Gatsby built website?

From Dev

Use xtable to print html table in R markdown file

From Dev

how to convert markdown to HTML with different sizes using pandoc

From Dev

How to convert Markdown to HTML in Node.js / Express / MongoDB app

From Dev

How to convert JSON object to markdown using pypandoc without writing to file?

From Dev

How to use Markdown in Elm: is it [markdown| or [markdown |?

From Dev

How to use Markdown in Elm: is it [markdown| or [markdown |?

From Dev

How to Convert HTML file TO GIF

From Dev

how to convert html file to word file in php

From Dev

How to use eqnarray in R markdown for both html and pdf output?

From Dev

Rails 4: how I use the contents of a markdown file in a view?

From Dev

How can I save github markdown file as a HTML or PDF?

From Dev

How can I save github markdown file as a HTML or PDF?

From Dev

How To Fetch Data From Markdown File and Display it as a HTML input Form

From Dev

Eclipse Markdown Plugin - How to open Markdown HTML Preview when double clicking a .md file?

From Java

Convert Markdown to HTML without allowing HTML Tags

From Dev

How to use file_field_tag with carrierwave

From Java

How to upload file field (html form) to database?

From Dev

How to use shell to convert HTML to table

From Dev

How does Gatsby serve up Markdown frontmatter with GraphQL?

From Dev

How to convert local HTML file into String?

From Dev

How to convert a HTML file to PDF (with colors)

From Dev

How to convert a HTML file to PDF (with colors)

From Dev

How to convert an HTML piece of code to a PDF file?

Related Related

  1. 1

    How to use HTML in Markdown?

  2. 2

    How to convert markdown to inline HTML?

  3. 3

    Use find command to convert markdown files to html

  4. 4

    Convert custom markdown to HTML?

  5. 5

    How can I make vim use html and markdown syntax highlighting on the same file?

  6. 6

    How do you apply styling to array from a markdown file for Gatsby built website?

  7. 7

    Use xtable to print html table in R markdown file

  8. 8

    how to convert markdown to HTML with different sizes using pandoc

  9. 9

    How to convert Markdown to HTML in Node.js / Express / MongoDB app

  10. 10

    How to convert JSON object to markdown using pypandoc without writing to file?

  11. 11

    How to use Markdown in Elm: is it [markdown| or [markdown |?

  12. 12

    How to use Markdown in Elm: is it [markdown| or [markdown |?

  13. 13

    How to Convert HTML file TO GIF

  14. 14

    how to convert html file to word file in php

  15. 15

    How to use eqnarray in R markdown for both html and pdf output?

  16. 16

    Rails 4: how I use the contents of a markdown file in a view?

  17. 17

    How can I save github markdown file as a HTML or PDF?

  18. 18

    How can I save github markdown file as a HTML or PDF?

  19. 19

    How To Fetch Data From Markdown File and Display it as a HTML input Form

  20. 20

    Eclipse Markdown Plugin - How to open Markdown HTML Preview when double clicking a .md file?

  21. 21

    Convert Markdown to HTML without allowing HTML Tags

  22. 22

    How to use file_field_tag with carrierwave

  23. 23

    How to upload file field (html form) to database?

  24. 24

    How to use shell to convert HTML to table

  25. 25

    How does Gatsby serve up Markdown frontmatter with GraphQL?

  26. 26

    How to convert local HTML file into String?

  27. 27

    How to convert a HTML file to PDF (with colors)

  28. 28

    How to convert a HTML file to PDF (with colors)

  29. 29

    How to convert an HTML piece of code to a PDF file?

HotTag

Archive