AndreasFaust / gatsby-source-custom-api

Source data from any API and transform it to (File-)Nodes.
52 stars 25 forks source link

Can we define default values for a given key? #13

Closed duranmla closed 3 years ago

duranmla commented 3 years ago

Hi!

In order to avoid more complexity at the development time and maybe crashes at building time due to the possibility of a server not being running can we add default values on a given schema? For instance I have:

    {
      resolve: 'gatsby-source-custom-api',
      options: {
        url: `${process.env.GATSBY_COMMUNITY_URL}/about.json`,
        rootKey: 'about',
        schemas: {
          about: `
            user_count: Int
          `
        }
      }
    }

Which I would like to say something like:

    {
      resolve: 'gatsby-source-custom-api',
      options: {
        url: `${process.env.GATSBY_COMMUNITY_URL}/about.json`,
        rootKey: 'about',
        schemas: {
          about: `
            user_count: Int = 33000 <-- notice the lame attempt here
          `
        }
      }
    }

It feels a bit overkill that the entire page crashes cause the server is not available. Any thoughts? Thanks a lot for this solution btw, really appreciate the work have been done here!

baumant commented 3 years ago

This would be done when you actually set up the graphql query on the page, something like:

allAbouts( filter:  { user_count: { eq: 33000 } } ) {
  edges {
      node {
        user_count
      }
    }
}
duranmla commented 3 years ago

But that doesn't quite solve the issue, isn't? So imagine you're starting to build the site and the server is down (the most easy to describe case will be to do yarn start on Gatsby site locally without starting the server) This will thrown error like:

image

As the query indeed is not there (notice how I toggle between the GraphiQL when the server with the api is running and when the server with the api is not running in my local) allAbout is not created when the server is not available in build time

http://recordit.co/Pie06Z7D6a

And there is a lot going on at https://www.gatsbyjs.com/docs/schema-customization/#creating-type-definitions so I was wondering about a built-in option from gatsby-source-custom-api to tackle this scenario

baumant commented 3 years ago

Ah I see what you mean, misunderstood your question I thought you were trying to filter your query. This seems to be a common issue with Gatsby. Read through this issue (https://github.com/gatsbyjs/gatsby/issues/2392), there are a few proposed solutions in there (creating a resolver in gatsby-node)

duranmla commented 3 years ago

Got it, I will need to test this out if worth the trouble but in the mean time I would close the issue...Thanks for the replies!!