简体   繁体   English

gatsby 电子商务 startkit + graphQL 查询问题

[英]gatsby ecommerce startkit + graphQL query issue

I come from a background of React, Mongo, node, a bit of SQL and Shopify (ton's of JS under my belt).我来自 React、Mongo、node、一点 SQL 和 Shopify(我的腰带下有大量的 JS)的背景。

I came across this JAM stack idea and it seemed interesting and decided to try it out.我遇到了这个 JAM 堆栈的想法,它看起来很有趣,并决定尝试一下。 I run into this problem that frankly I can't seem to wrap my head around after all the GraphQL tut's I've watched and articles I read, I'm clearly missing something important.我遇到了这个问题,坦率地说,在我看过所有 GraphQL tut 和我阅读的文章之后,我似乎无法解决这个问题,我显然错过了一些重要的东西。

Traditionally in REST backend, you develop a scheme and your endpoints and then you ask them for the data.传统上,在 REST 后端中,您开发一个方案和您的端点,然后您向它们询问数据。

Following this introduction, I get to the part where I query GraphQL, but I can't understand what or how I'm querying without developing a scheme.介绍之后,我进入了查询 GraphQL 的部分,但如果不开发方案,我将无法理解我在查询什么或如何查询。 Use this code (after setting up Strip with test product / key)使用此代码(使用测试产品/密钥设置 Strip 后)

import React from "react"
import { graphql, StaticQuery } from "gatsby"

export default props => (
  <StaticQuery
    query={graphql`
      query SkusForProduct {
        skus: allStripeSku {
          edges {
            node {
              id
              currency
              price
              attributes {
                name
              }
            }
          }
        }
      }
    `}
    render={({ skus }) => (
      <div>
        {skus.edges.map(({ node: sku }) => (
          <p key={sku.id}>{sku.attributes.name}</p>
        ))}
      </div>
    )}
  />
)

It states:它指出:

You can validate your query and see what data is being returned in GraphiQL, which is available at http://localhost:8000/___graphql when running npm run develop.您可以验证您的查询并查看在 GraphiQL 中返回的数据,在运行 npm run develop 时可在http://localhost:8000/___graphql 获得

Upon visiting this area, I noticed Query setup's and options.访问该区域后,我注意到查询设置和选项。 Is this where I develop the query that I'm using (or the schema?)这是我开发我正在使用的查询(或模式?)

Slightly lost as what this sort of 'connection' looks like.有点像这种“连接”的样子。

After following the full tutorial and replacing API keys, I get this error:遵循完整教程并替换 API 密钥后,我收到此错误:

GraphQL Error Encountered 1 error(s):
- Unknown field 'allStripeSku' on type 'Query'.

      file: /Users/robert/Software/bDev/evu/src/components/products/skus.js

My gatsby-config:我的盖茨比配置:

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


module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },

  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-stripe`,
    {
      resolve: `gatsby-source-stripe`,
      options: {
        objects: ["Sku"],
        secretKey: process.env.STRIPE_SECRET_KEY,
        downloadFiles: true,
      },
    },
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],

}

I just figured this out as I ran into the same head-scratching issue.当我遇到同样令人头疼的问题时,我才发现这一点。

You have to create test products in stripe, not just live products.您必须以条带形式创建测试产品,而不仅仅是实时产品。 Make sure you've toggled the Viewing test data toggle in the Stripe admin.确保您已在 Stripe 管理中切换查看测试数据切换。 Then create your products.然后创建您的产品。

You might need to clear your cache and public folders if it still isn't working.如果仍然无法正常工作,您可能需要清除缓存和公用文件夹。

Hope this helps.希望这可以帮助。

For me the solution was to add STRIPE_PUBLISHABLE_KEY to .env.development and not just STRIPE_SECRET_KEY on its own (which is what the tutorial explicitly says to do).对我来说,解决方案是将STRIPE_PUBLISHABLE_KEY添加到 .env.development 而不仅仅是STRIPE_SECRET_KEY本身(这是教程明确说要做的)。 As soon as I'd added both, the build errors went away and the site could load up as normal;一旦我添加了两者,构建错误就会消失,站点可以正常加载; I also saw 'allStripePrice' appear as a field in GraphiQL.我还看到“allStripePrice”在 GraphiQL 中显示为一个字段。 So the config should take this form:所以配置应该采用这种形式:

# Stripe secret API key
STRIPE_PUBLISHABLE_KEY=pk_test_xyz
STRIPE_SECRET_KEY=sk_test_xyz

I've raised this as a documentation issue in the GitHub repo and made Pull Request that addresses it.我已将此作为 GitHub 存储库中的文档问题提出,并提出了解决它的 Pull Request。 Feel free to upvote that issue.随意upvote这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM