当前位置: 首页 > 编程日记 > 正文

gatsby_将您的GraphCMS数据导入Gatsby

gatsby

Let's set up Gatsby to pull data from GraphCMS.

让我们设置Gatsby来从GraphCMS中提取数据。

This will be a walk-through of setting up some basic data on the headless CMS, GraphCMS and then querying that data in Gatsby.

这将是在无头CMS,GraphCMS上设置一些基本数据,然后在Gatsby中查询该数据的演练。

1.设置GraphCMS (1. Set up GraphCMS)

Set yourself up with a GraphCMS account at https://app.graphcms.com/signup and select the developer plan.

在https://app.graphcms.com/signup上使用GraphCMS帐户进行设置,然后选择开发者计划。

2.定义数据 (2. Define Data)

Create a new project and add in some data to query.

创建一个新项目并添加一些数据以进行查询。

Select the Create new project option, call it what you like, in this example it's going to be a list of projects, so I'm calling it Project List.

选择Create new project选项,按您的喜好命名,在本示例中,它将是一个项目列表,因此我将其称为Project List

In the side bar select the Schema and create a model, in this case Project. In the project model we're going to have a Title and a Description.

在侧栏中,选择Schema并创建一个模型,在本例中为Project 。 在项目模型中,我们将具有TitleDescription

Select the fields from the tray on the right by clicking the FIELDS tab and dragging and dropping them into the Project model we created.

通过单击FIELDS选项卡并将其拖放到我们创建的Project模型中,从右侧的托盘中选择字段。

3.配置GraphCMS公共API (3. Configure the GraphCMS public API)

In the GraphCMS settings set the Public API Permissions to READ scroll down to Endpoints and copy the URL for use in configuring Gatsby.

在GraphCMS设置中,将“ 公共API权限”设置为“ 读取”,向下滚动至“ 端点”,然后复制URL以配置Gatsby。

That's it for the CMS configuration, now to pull that data into our Gatsby frontend!

CMS配置就是这样,现在将数据拉入我们的Gatsby前端!

4.配置盖茨比 (4. Configure Gatsby)

In you Gatsby project install gatsby-source-graphql and configure it in gatsby-config.js the configuration should looks something like:

在您的Gatsby项目中,安装gatsby-source-graphql并在gatsby-config.js对其进行配置,其配置应类似于以下内容:

{resolve: 'gatsby-source-graphql',options: {typeName: 'GRAPHCMS',fieldName: 'graphCmsData',url: 'https://api-euwest.graphcms.com/v1/projectid/master',}
},

In this example we're using codesandbox.io for our text editor and the Gatsby Default Starter you get when selecting Gatsby from the SERVER TEMPLATES available to you in codesandbox.io

在此示例中,我们将codeandbox.io用于我们的文本编辑器,以及从codeandbox.io中可用的服务器模板中选择Gatsby时所获得的Gatsby Default Starter。

5.在Gatsby GraphiQL中查询数据 (5. Query the data in Gatsby GraphiQL)

Now that the endpoint is set up we will be able to query the data with Gatsby's GraphiQL UI, we can shape the query we want to use to display the data here.

现在已经设置了端点,我们将能够使用Gatsby的GraphiQL UI查询数据,我们可以调整要用于在此处显示数据的查询。

In the preview of our app in codesandbox.io if you add ___grapgql to the end of the url it will bring up the Gatsby GraphiQL UI, here we can shape the data we want to query.

在codeandbox.io中我们应用程序的预览中,如果在网址末尾添加___grapgql ,它将弹出Gatsby GraphiQL UI,在这里我们可以调整要查询的数据。

If we open up some curly brackets {} and Cmd+space we'll see the available fields where we can pick out the graphCmsData field we defined in the gatsby-source-graphql plugin.

如果打开大括号{}和Cmd + space,我们将看到可用的字段,从中可以选择在gatsby-source-graphql插件中定义的graphCmsData字段。

Selecting the fields we created in GraphCMS then running the query will display our defined data.

选择我们在GraphCMS中创建的字段,然后运行查询将显示我们定义的数据。

{graphCmsData {projects {idstatustitledescription}}
}

6.显示数据 (6. Display the Data)

Use the graphql gatsby export to query the data from the GraphCMS endpoint.

使用graphql gatsby导出可从GraphCMS端点查询数据。

In pages/index.js add the graphql export the the gatsby imports.

pages/index.js添加graphql导出gatsby导入。

import { graphql, Link } from 'gatsby';

At the very bottom define the query:

在最底部定义查询:

export const query = graphql`{graphCmsData {projects {idstatustitledescription}}}
`;

You will then be able to access the data prop in the IndexPage component, we'll need to de-structure the data prop out in the arguments of the component:

然后,您将能够访问IndexPage组件中的data IndexPage ,我们需要在该组件的参数中对data属性进行解构:

const IndexPage = ({ data }) => (

Now we can access the data passed into the component, we just need a way to visualise it! Luckily for use there's a handy component from Wes Bos that we can use called Dump, so create a new dump.js component in components then import it into the index.js file, and add in the component to see what's inside the props:

现在我们可以访问传递到组件中的data ,我们只需要一种可视化的方式即可! 幸运的是,Wes Bos有一个方便的组件,我们可以使用它称为Dump ,因此可以在components创建一个新的dump.js组件,然后将其导入index.js文件,并添加该组件以查看道具内部的内容:

const IndexPage = ({ data }) => (<Layout><h1>Hi people</h1><Dump data={data} /><p>Welcome to your new Gatsby site.</p><p>Now go build something great.</p><div style={{ maxWidth: '300px', marginBottom: '1.45rem' }}><Image /></div><Link to="/page-2/">Go to page 2</Link></Layout>
);

The output should be the same as the result of the Gatsby GraphiQL query we created:

输出应与我们创建的Gatsby GraphiQL查询的结果相同:

data ?{"graphCmsData": {"projects": [{"id": "cjoxa812txqoh0932hz0bs345","status": "PUBLISHED","title": "Project 1","description": "Description 1"},{"id": "cjoxa8cctxqqj0932710u39db","status": "PUBLISHED","title": "Project 2","description": "Description 2"},{"id": "cjoxa8pbqxqt309324z9bcddv","status": "PUBLISHED","title": "Project 3","description": "Description 3"},{"id": "cjoxa959vxqvz0932g1jn5ss3","status": "PUBLISHED","title": "Project 4","description": "Description 4"}]}
}

谢谢阅读 ? (Thanks for reading ?)

If there is anything I have missed, or if there is a better way to do something then please let me know.

如果我错过了任何事情,或者有更好的方法做某事,请告诉我。

Follow me on Twitter or Ask Me Anything on GitHub.

在Twitter上关注我,或在GitHub上询问我 。

You can read other articles like this on my blog.

您可以在我的博客上阅读其他类似的文章 。

翻译自: https://www.freecodecamp.org/news/get-your-graphcms-data-into-gatsby-2018/

gatsby

相关文章:

Java学习笔记07--日期操作类

一、Date类 在java.util包中定义了Date类&#xff0c;Date类本身使用非常简单&#xff0c;直接输出其实例化对象即可。 public class T { public static void main(String[] args) { Date date new Date(); System.out.println("当前日期&#xff1a;"date); //当前…

javascript数组集锦

设计数组的函数方法 toString, toLocaleString, valueOf, concat, splice, slice indexOf,lastIndexOf, push, pop, shift, unshift, sort, reverse map, reduce, reduceRight, filter, every, some, forEach 创建数组 数组字面量创建&#xff1a;var arr [val1, val2, val3];…

JS实现HTML标签转义及反转义

开发交流QQ群: 173683895 173683895 526474645 人满的请加其它群 编码反编码 function html_encode(str) { var s ""; if (str.length 0) return ""; s str.replace(/&/g, "&amp;"); s s.replace(/</g, "<")…

喜欢把代码写一行的人_我最喜欢的代码行

喜欢把代码写一行的人Every developer has their favourite patterns, functions or bits of code. This is mine and I use it every day.每个开发人员都有自己喜欢的模式&#xff0c;功能或代码位。 这是我的&#xff0c;我每天都用。 它是什么&#xff1f; (What is it?) …

智能家居APP开发

智能家居APP开发 APP开发技术qq交流群&#xff1a;347072638 前言&#xff0c;随着智能硬件设备的流行&#xff0c;智能家居開始红火&#xff0c;智能家居就是家用电器的智能化。包含智能锁&#xff0c;灯&#xff0c;空调&#xff0c;灯&#xff0c;音箱等等&#xff0c;移动设…

android小技巧(二)

一、如何控制Android LED等&#xff1f;(设置NotificationManager的一些参数) 代码如下: final int ID_LED19871103; NotificationManager nm(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification new Notification(); notificatio…

JS 验证表单不能为空

开发交流QQ群: 173683895 173683895 526474645 人满的请加其它群 JS 验证表单不能为空的简单demo&#xff0c;先看效果图 实现代码 <!--index.wxml--> <form classform bindsubmitformSubmit bindresetformReset><input namename value{{name}} placeho…

周末不用过来了,好好休息吧_如何好好休息

周末不用过来了&#xff0c;好好休息吧When I wrote about my productive routine in a previous article, I said I’d work for 1.5 hours and take a break for 30 minutes. And I’d repeat this sequence four times a day.当我在上一篇文章中谈到生产性例程时&#xff0c…

HTML实现折现图完整源码及效果图

开发交流QQ群: 173683895 173683895 526474645 人满的请加其它群 效果图 源码 <!DOCTYPE html> <html><head><meta charset"utf-8" /><script src"https://cdnjs.cloudflare.com/ajax/libs/echarts/4.2.1/echarts-en.common…

Date类(java.util)和SimpleDateFormat类(java.text)

在程序开发中&#xff0c;经常需要处理日期和时间的相关数据&#xff0c;此时我们可以使用 java.util 包中的 Date 类。这个类最主要的作用就是获取当前时间&#xff0c;我们来看下 Date 类的使用&#xff1a; 使用 Date 类的默认无参构造方法创建出的对象就代表当前时间&#…

8月12笔记-安卓文件扫描

Android的文件系统 1.Android的项目是运行在Linux操作系统上的 2.Linux的文件系统根目录是/&#xff0c;Windows只有某个盘符根目录 3.mnt文件夹是手机的内存卡根目录&#xff0c;此目录是安卓开发经常使用的 4.在windows下&#xff0c;最高管理员叫做Administrator&#xff0c…

可视化编码_Modulz简介:可视编码的下一步

可视化编码by Colm Tuite通过Colm Tuite Modulz简介&#xff1a;可视编码的下一步 (Introducing Modulz: The Next Step in Visual Coding) Modulz is a visual code editor for designing and building digital products — without writing code. Last week, we launched ou…

SQL执行过程中的性能负载点

一、SQL执行过程 1、用户连接数据库&#xff0c;执行SQL语句&#xff1b; 2、先在内存进行内存读&#xff0c;找到了所需数据就直接交给用户工作空间&#xff1b; 3、内存读失败&#xff0c;也就说在内存中没找到支持SQL所需数据&#xff0c;就进行物理读&#xff0c;也就是到磁…

认识Backbone (五)

Backbone.Router&#xff08;路由&#xff09;/ Backbone.history&#xff08;历史&#xff09; Backbone.Router 为客户端路由提供了许多方法&#xff0c;并能连接到指定的动作&#xff08;actions&#xff09;和事件&#xff08;events&#xff09;。 对于不支持 History API…

if else 你以为你把它吃透了吗?我让你惊讶一下

开发交流QQ群: 173683895 173683895 526474645 人满的请加其它群 if 和 else 是写代码最常用的&#xff0c;但是往往同学们不会去深入的了解他&#xff0c;这里我写几个Demo玩玩。 首先简单列一下什么值会返回true &#xff0c; 什么值会返回false。 示例&#xff1a;…

router路由react_使用React Router在React中受保护的路由

router路由reactIn this video, you will see how to create a protected route using React Router. This route is accessible only when the user is logged in.在此视频中&#xff0c;您将看到如何使用React Router创建受保护的路由。 仅当用户登录时&#xff0c;此路由才可…

SSH框架搭建笔记

1、建立一个web项目&#xff0c;设置编码格式&#xff0c;建立src下的包&#xff0c;建立资源文件夹 2、加入Spring运行必须的jar包(5个jar包)spring-beans-4.1.4.RELEASE.jarspring-context-4.1.4.RELEASE.jarspring-core-4.1.4.RELEASE.jarspring-expression-4.1.4.RELEASE.j…

灵活运用 SQL SERVER FOR XML PATH

FOR XML PATH 有的人可能知道有的人可能不知道&#xff0c;其实它就是将查询结果集以XML形式展现&#xff0c;有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作。那么以一个实例为主. 一.FOR XML PATH 简单介绍 那么还是首先来介绍一下FOR…

小程序画布,随机24个数显示在画布上面,不可重叠

QQ技术交流群 173683866 526474645 欢迎加入交流讨论&#xff0c;打广告的一律飞机票 效果图&#xff08;下面两个图都是随机显示24的圆圈在画布上面&#xff09; 实现代码 <!--pages/test2/test2.wxml--> <canvas style"width: 100%; height:700rpx;" ca…

hacktoberfest_Hacktoberfest 2018:如何获得免费衬衫—即使您是编码新手

hacktoberfestEvery October, Digital Ocean and GitHub ship out free Hacktoberfest shirts to thousands of people around the world.每年10月&#xff0c;Digital Ocean和GitHub都会向全球成千上万的人运送免费的Hacktoberfest衬衫。 I’ve gotten Hacktoberfest shirts …

Android自动化测试框架

1、Monkeyrunner&#xff1a;优点&#xff1a;操作最为简单&#xff0c;可以录制测试脚本&#xff0c;可视化操作&#xff1b;缺点&#xff1a;主要生成坐标的自动化操作&#xff0c;移植性不强&#xff0c;功能最为局限&#xff1b; 2、Rubotium&#xff1a;主要针对某一个…

详解 Date 对象

JS使用Date对象来处理日期和时间 五种调用Date函数的方式 Date() 单纯的作为函数调用&#xff0c;传入的参数会被忽略&#xff0c;返回当前日期和时间的字符串表示。 new Date() 作为构造函数调用。 返回当前日期和时间的Date对象。 new Date(Milliseconds) 作为构造函数调用…

Bootstrap select 多选并获取选中的值

QQ技术交流群 173683866 526474645 欢迎加入交流讨论&#xff0c;打广告的一律飞机票 效果图&#xff1a; 输出日志 代码&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><script src"js/jquery-3.4.1.min.js&quo…

如何在React中使用Typescript

TypeScript can be very helpful to React developers.TypeScript对React开发人员可能非常有帮助。 In this video, Ben Awad teaches how to use Typescript in React and shares some of its benafits. He also tells about a great boilerplate for TypeScript React proje…

java web 开发应用 ----过滤器

过滤器的作用 1.当用户请求web资源时&#xff0c;如果没有过滤器&#xff0c;用户可以直接获取到这个web资源&#xff0c;当有了过滤器之后&#xff0c;当用户请求web资源时&#xff0c;web容器中的过滤器先会拦截到这个请求&#xff0c;然后根据这个请求 做相应的处理&#xf…

小程序在wxml使用indexOf

QQ技术交流群 173683866 526474645 欢迎加入交流讨论&#xff0c;打广告的一律飞机票 demo场景&#xff1a; 有两个数组&#xff0c;页面渲染一个数组1&#xff0c;数组2中有数组1随机下标的值&#xff0c;判断数组1是否包含数组2的值&#xff0c;如果包含了就改变当前下标的…

d3.js图表_如何使用D3.js建立历史价格图表

d3.js图表逐步可视化财务数据集的方法 (A step by step approach towards visualizing financial datasets) It is a challenge to communicate data and display these visualizations on multiple devices and platforms.交流数据并在多个设备和平台上显示这些可视化内容是一…

Harris角点

可参考&#xff1a;http://www.cnblogs.com/ronny/p/4009425.html http://www.cnblogs.com/ztfei/archive/2012/05/07/2487123.html http://blog.csdn.net/crzy_sparrow/article/details/7391511 矩阵M(x)的特征值能表示在水平和竖直方向的变化程度&#xff0c;但Harris给出的角…

【博客美化】公告栏显示个性时间

设置侧边公告栏显示个性化时间 效果图&#xff1a; <div id"myTime"><object classid"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version8,0,0,0"…

微信小程序实现退款

QQ技术交流群 173683866 526474645 欢迎加入交流讨论&#xff0c;打广告的一律飞机票 退款php代码 <?php // ---------------------------------------------------------------------- // | Tplay [ WE ONLY DO WHAT IS NECESSARY ] // -------------------------------…