ButterCMS: Unknown field on RootQueryType - graphql

Hello Im trying to query data into Gatsby from ButterCMS by following the documentation in gatsby-source-buttercms(https://www.gatsbyjs.org/packages/gatsby-source-buttercms/#gatsby-source-buttercms). But got the error "unknown field allButterJob on RootQueryType". I dont know what i did wrong. Someone please take a look at this for me. Here's my gatsby-config.js:
module.exports = {
siteMetadata: {
title: 'Gatsby Default Starter',
},
plugins: [
'gatsby-plugin-react-helmet',
{
resolve: 'gatsby-source-buttercms',
options: {
authToken: '2a926fdcab34e736332a54e24649cedbaf5d0e89',
contentFields: {
keys: [ // Comma delimited list of content field keys.
'job',
'news'
],
test: 0 // Optional. Set to 1 to enable test mode for viewing draft content.
}
}
}
],
}
Here's where i made the query:
import React from 'react'
import Link from 'gatsby-link'
import HeaderlineSection from '../components/headerlineSection'
import FeatureSection from '../components/featureSection'
import TeamSection from '../components/teamSection'
import NewsSection from '../components/newsSection'
import CareerSection from '../components/careerSection'
const IndexPage = ({data}) => (
<div>
<HeaderlineSection />
<FeatureSection />
<TeamSection />
<NewsSection />
<CareerSection />
{console.log(data)}
</div>
)
export default IndexPage
export const query = graphql`
query IndexPageQuery{
allButterJob{
edges{
node{
id
title
}
}
}
}
`

RootQueryType is the top level “item” in your GraphQL schema (Gatsby v1 sets this up). So the relevant part of the error here is “unknown field allButterJob”, which is pretty self explanatory: the field/type you're trying to query doesn't exist at the top level.
It's likely that it's there under a different name. Usually I hop into Graphiql (localhost:8000/___graphql if you're running gatsby develop under the standard port), where you will see something like this in the sidebar (click on the Docs link if it isn't showing):
From here you can click on “Query” to drill down into it. (Note that this screenshot is from a Gatshy v2 app, so instead of RootQueryType it's just listed as Query.) That'll pull up a list of the fields available on Query (or in your case, RootQueryType) that looks something like this:
In this example, allSitePage is a top-level field available to query like this:
query AnythingYouLikeHere {
allSitePage {
edges {
node {
path
}
}
}
}

Related

Gatsby - Cannot query field on type Error when field in CMS is empty

I found out that this is known problem with Strapi + Gatsby setup, but I found some articles about how to get rid of that error like the following ones:
https://www.virtualbadge.io/blog-articles/nullable-relational-fields-strapi-gatsbyjs-graphql
https://medium.com/swlh/gatsby-graphql-and-the-missing-but-necessary-explanation-about-type-definitions-87a5ef83e759
And indeed it helped with the bug itself, but caused another. At the moment when I fill the field inside the CMS graphql cannot see it and interprets it as null.
Without gatsby-node.ts:
And With gatsby-node.ts:
import type { GatsbyNode } from 'gatsby'
export const sourceNodes: GatsbyNode['sourceNodes'] = async ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type STRAPI__COMPONENT_BASE_HERO implements Node {
backgroundVideo: STRAPI__MEDIA
}
`
createTypes(typeDefs)
}

I want to create a category page. I am using Gatsby.js, GraphQL and Strapi

I want to create a category page. I am using Gatsby.js, GraphQL and Strapi.
Probably filter is not working.
But I don't know how to solve it.
{strapiCategory.slug}.js
import * as React from "react";
import { graphql } from "gatsby";
import { Helmet } from "react-helmet";
import Layout from "../../components/Layout";
import PostList from "../../components/PostList";
import *as styles from "./{MicrocmsCategory.slug}.module.css";
const CategoryPage = (props) => {
const categories = props.data.strapiCategory;
const posts = props.data.allStrapiPost.nodes;
return (
<Layout>
<Helmet>
<title>{categories.name}</title>
<meta name="description" content={categories.name} />
</Helmet>
<p className={styles.categoryName}> {categories.name}</p>
<PostList posts={posts} />
</Layout>
);
};
export const query = graphql`
query ($slug: String!){
strapiCategory (slug: { eq: $slug }){
name
slug
}
allStrapiPost(filter:{categories:{slug:{ eq: $slug }}}){
nodes {
slug
title
content
thumbnail
published_at(formatString: "YYYY.MM.DD hh:mm")
categories {
slug
name
}
}
}
}
`;
export default CategoryPage;
error code
ERROR #98123 WEBPACK
Generating development JavaScript bundle failed
/Users/t/WebDevelopment/xxxxxx/src/pages/categories/{xxxx.slug}.js
29:39 error Field "slug" is not defined by type
"StrapiPostCategoriesFilterListInput" graphql/template-strings
✖ 1 problem (1 error, 0 warnings)
failed Re-building development bundle - 0.177s ERROR in
/Users/t/WebDevelopment/xxxx/src/pages/categories/{xxxxxx.slug}.js
29:39 error Field "slug" is not defined by type
"StrapiPostCategoriesFilterListInput"
The link is working fine.
It will be displayed.
const categories = props.data.strapiCategory;
<title>{categories.name}</title>.
This will not be displayed.
const posts = props.data.allStrapiPost.nodes;
<PostList posts={posts} />
Field "slug" is not defined by type
"StrapiPostCategoriesFilterListInput" graphql/template-strings
It's quite self-explanatory. In your case, slug is under categories (collection/nested route in FileSystem API).
Your {xxxxx.slug}.js should become:
{xxxxx.categories.slug}.js
Can you try using where instead of filter in graphql
allStrapiPost(where:{categories:{slug:{ eq: $slug }}}){

How do I use sharp image with Strapi 4 and Gatsby 4

I'm want to use images from my Strapi V4 local backend to my Gatsby 4 using sharp image processing.
I was able to with Strapi 3 + Gatsby 3, but have recently upgraded to Strapi 4 and Gatsby 4 to avoid future deprecation.
This is my gatsby-config.js:
plugins: [
"gatsby-plugin-sass",
"gatsby-plugin-image",
"gatsby-plugin-sharp",
"gatsby-transformer-sharp",
{
resolve: "gatsby-source-graphql",
options: {
// Arbitrary name for the remote schema Query type
typeName: "STRAPI",
// Field under which the remote schema will be accessible. You'll use this in your Gatsby query
fieldName: "strapi",
// Url to query from
url: "http://localhost:1337/graphql",
},
}
]
This is a file (page within my gatsby site) i've been testing on, it doesn't work.
import React from "react";
import { graphql } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"
const Test = ({ data }) => {
const image = getImage(strapi.food.data.attribute.thumbnail.data.attribute)
return (
<div>
<GatsbyImage image={image} alt={"Come on!"} />
</div>
)
}
export const pageQuery = graphql`
query FoodQuery {
strapi {
food(id: "67") {
data {
attributes {
name
thumbnail {
data {
attributes {
childImageSharp {
gatsbyImageData(width: 200)
}
}
}
}
}
}
}
}
}
`
export default Test;
The error I keep getting is.
25:17 error Cannot query field "childImageSharp" on type "STRAPI_UploadFile" graphql/template-strings
I've tried many things. I've checked to see if I can at least pull text and number fields, I can, all of them even attributes in the thumbnails object like createdAt.
I've checked to see if permissions are correct, and they seem fine - find, fineOne are both checked for the content-type and upload.
I've tried to query the uploadFile. And tried to pull food items one at a time and as a array/list of foods.
I've tried changing where I've placed childImageSharp as well as moving brackets around.
Edit: This is my GraphiQl sandbox with everything I can gather.

Graphql upload file shows as empty object

when I'm trying to upload file via Graphql mutation (apollo-angular) with parameter $file: Upload!, the variable value is shown as an empty object in developer tools:
But I'm checking the value of the variable right before the request and the value is correct, it is in fact a File, not an empty object.
import gql from 'graphql-tag';
import { Apollo } from 'apollo-angular';
this.apollo.mutate(
{
mutation: gql`
${uploadMutation}
`,
variables: {
companyId,
file <-- Here the value is correct, it is a File
},
context: {
hasUpload: true,
useMultipart: true
}
},
'upload'
);
Code is simplified for the sake of this example.
Is it just developer tools issue, am I doing something wrong or what is the problem please?
You have to config the Apollo client with this:
import { createUploadLink } from 'apollo-upload-client'
const link = createUploadLink(/* Options */)
Resource: https://github.com/jaydenseric/apollo-upload-client/issues/89#issuecomment-384923295

Can't query data from contentful to gatsby project

Don't get the query to contentful to work.
Receive error message:
TypeError: Cannot read property 'allContentfulMagArticle' of undefined
datais undefined inside the Posts component. Can't see what i'm doing wrong here.
import { graphql } from 'gatsby';
import Post from "./post.js";
import './posts.css';
export const query = graphql`
query {
allContentfulMagArticle{
edges{
node{
index
title
name
subHeading
extract {
raw
}
slug
}
}
}
}
`
const Posts = ({ data }) => {
return (
<section className="posts">
<ul className="post-list">
{data.allContentfulMagArticle.edges.map(({ node }) => (
<Post
key={node.index}
id={node.index}
node={node}
title={node.title}
name={node.name}
// image={node.frontmatter.featuredImage.childImageSharp.fluid}
subheading={node.subheading}
body={node.extract.raw}
/>
))}
</ul>
</section>
)
}
export default Posts
Here my gatsby-config.js:
require('dotenv').config({
path: `.env`,
})
module.exports = {
siteMetadata: {
title: `XX`,
description: `XX`,
author: `Lisa Lee`,
url: `https://www.tortmagazine.com`
},
plugins: [
`gatsby-plugin-react-helmet`,
'gatsby-plugin-fontawesome-css',
'gatsby-plugin-sharp',
`gatsby-transformer-sharp`,
`gatsby-transformer-remark`,
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/`,
},
},
{
resolve: `gatsby-source-contentful`,
options: {
spaceId: process.env.GATSBY_CONTENTFUL_SPACE_ID,
accessToken: process.env.GATSBY_CONTENTFUL_ACCESS_TOKEN,
},
},
],
}
You used the word "component" to describe your Posts but the query you are using only works in a page, or in the context of createPage (so in a template file as well). If you are indeed in a component, that will be the issue. If not, then I'm not clear what is wrong, I use the same pattern (eg: data.edges.node.map()) and it works for me.
The only other difference I noticed is in gatsby-config, I define an environment key. I'm not sure what the behavior is if none is defined, probably defaults to master so you may also want to confirm you're on the right environment.

Resources