gatsby-uc / gatsby-source-strapi

Gatsby source plugin for building websites using Strapi as a data source
MIT License
355 stars 183 forks source link

Unable to fetch singleTypes #312

Closed jasewarner closed 2 years ago

jasewarner commented 2 years ago

Appreciate any help anyone can give on this one…

I'm running gatsby-source-strapi v1.0.2 alongside @strapi/strapi v4.1.5.

This is the relevant part of my gatsby-config.js file:

{
    resolve: `gatsby-source-strapi`,
    options: {
        apiUrl: process.env.STRAPI_API_URL,
        accessToken: process.env.STRAPI_TOKEN,
        singleTypes: [
            {
                singularName: `global`,
                // queryParams: {
                //  populate: {
                //      social: {
                //          populate: `*`,
                //      },
                //  },
                // },
            },
            {
                singularName: `header`,
            },
        ],
    }
},

The .env.development file exists in the root of my Gatsby project folder and consists of the following:

STRAPI_TOKEN=<MY_TOKEN>
STRAPI_API_URL=http://localhost:1337

After successfully starting up my Strapi instance, when running gatsby develop, I get the following error:

info Starting to fetch data from Strapi - http://localhost:1337/undefined with params
{"_limit":100}
info Starting to fetch data from Strapi - http://localhost:1337/undefined with params
{"_limit":100}

From what I've read, I feel fairly confident that I have followed all the setup docs to a T.

Why are those two single types coming through as undefined? The docs definitely state that singularName should be used.

remidej commented 2 years ago

It seems your problem is that gatsby-source-strapi v1 is not compatible with Strapi v4.

For Strapi v4 support, please use version 2.x.x of this source plugin instead. The latest is the beta.0, but we'll release the stable later today

jasewarner commented 2 years ago

Ah, I see! The README opens with "This version of gatsby-source-strapi (1.0.2) is only compatible with Strapi v4", hence the confusion.

remidej commented 2 years ago

Yes the code on master is the one published on the beta releases, so it's not the default on npm, I understand the confusion

remidej commented 2 years ago

We just released the stable 2.0.0 version FYI

jasewarner commented 2 years ago

Thanks for the latest release. Unfortunately after upgrading to 2.0.0 I've come across a new error when running gatsby develop. Here's an error of the snippet with some context:

success createSchemaCustomization - 0.013s

ERROR #11321  PLUGIN

"gatsby-source-strapi" threw an error while running the sourceNodes lifecycle:

connect ECONNREFUSED 127.0.0.1:80

> Error: connect ECONNREFUSED 127.0.0.1:80

warn The gatsby-source-strapi plugin has generated no Gatsby nodes. Do you need it?
success Checking for changed pages - 0.002s
remidej commented 2 years ago

Could you share your gatsby-config.js file?

jasewarner commented 2 years ago

Yep, with pleasure. Thank you for your help on this! Here's the entirety of my gatsby-config.js file (I'm migrating from a static Gatsby build to Strapi, so excuse the mess).

require('dotenv').config({
    path: `.env.${process.env.NODE_ENV}`,
})

const autoprefixer = require('autoprefixer');

module.exports = {
    siteMetadata: {
        title: `Lorem ipsum`, // TODO: update this
        siteUrl: `https://someurl.com/`, // TODO: update this
        description: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` // TODO: update this
    },
    plugins: [
        `gatsby-plugin-gatsby-cloud`,
        `gatsby-plugin-image`,
        {
            resolve: `gatsby-plugin-manifest`,
            options: {
                name: `DUSK`,
                start_url: `/`,
                background_color: `#fff`,
                theme_color: `#000`,
                display: `standalone`,
                icon: `./src/icons/favicon.svg`
            },
        },
        `gatsby-plugin-react-helmet`,
        {
            resolve: `gatsby-plugin-sass`,
            options: {
                postCssPlugins: [
                    autoprefixer()
                ]
            }
        },
        `gatsby-plugin-sitemap`,
        `gatsby-plugin-styled-components`,
        {
            resolve: `gatsby-source-strapi`,
            options: {
                apiUrl: process.env.STRAPI_API_URL,
                accessToken: process.env.STRAPI_TOKEN,
                singleTypes: [
                    {
                        singularName: `global`,
                    },
                    {
                        singularName: `header`,
                    },
                ]
            },
        },
        // {
        //  resolve: `gatsby-source-filesystem`,
        //  options: {
        //      name: `pages`,
        //      path: `${__dirname}/src/pages/`,
        //  },
        // },
        // {
        //  resolve: `gatsby-source-filesystem`,
        //  options: {
        //      name: `data`,
        //      path: `${__dirname}/src/data/`,
        //  },
        // },
        `gatsby-transformer-json`,
        `gatsby-transformer-remark`,
        `gatsby-transformer-sharp`,
    ]
};
remidej commented 2 years ago

Could you try with apiURL instead of apiUrl in your strapi config?

jasewarner commented 2 years ago

Oh! Why is it always a typo?!

Ok, thank you for helping me out there 🙏🏻

remidej commented 2 years ago

No problem 🙂