reactjs - Gatsby-GraphQL - 从 postgres 服务器获取远程数据
问题描述
我在本地的 postgres 上有一个现有的数据库。
我想在我的 Gatsby 应用程序中通过 GraphQL 获取这些数据。我gatsby-source-pg
用来做这个。
从此处的文档+ 示例中不清楚如何正确设置它。据我了解,您需要以下两个突出显示的部分。
// ./src/components/test.js
import { graphql } from 'gatsby';
export const query = graphql` // (1) graphQL query
{
postgres {
allUsers {
nodes {
id
firstLastName
}
}
}
}
`;
const Test = ({data}) => { // (2) grab the `data` prop (not sure where this comes from)
return (
<AppLayout>
{data.postgres.allUsers.nodes.map(user => <p>{user.firstLastName}</p>)} // data is undefined
</AppLayout>
)
}
export default Test
// gatsby-config.js
{
resolve: "gatsby-source-pg",
options: {
connectionString: "postgres://user@localhost:5432/testdb",
schema: "public",
},
},
问题:
- 如何从
./src/components
目录中查询数据?如果我运行上面的确切代码,data
道具会返回。我undefined
注意到从./src/pages/index.js
. 也许我缺少options
参数gatsby-source-pg
来告诉它要查看哪些目录? query
和从上面的代码是如何data
连接的?我无法通过查看示例来判断。
解决方案
推荐阅读
- javascript - 对象到数组的转换
- jquery - 如何将我的 jQuery 代码转换为 AngularJS?
- c# - Unity3d 我应该缓存 AndroidJavaObject 实例吗?
- npm - npm 模块名称的命名约定
- javascript - 使用spring security登录HTML后重定向到主页
- spring - 无法自省类 org.springframework.data.mongodb.core.SimpleMongoDbFactory 上的注释方法
- c# - RouteData.Values 空重定向
- angular - 如何在父组件中为一个子组件元素使用 2 个不同的 ngModel?
- user-interface - Robot Framework - 焦点/选择下拉弹出窗口
- c# - 在 WCF 和 Windows 身份验证中处理 CORS