如何连接GraphQL中的两种类型?
所以我在GraphQL中有两种类型: article.mdx categories.json 我想查询我的帖子类型以获得此类结果:如何连接GraphQL中的两种类型?,graphql,schema,gatsby,Graphql,Schema,Gatsby,所以我在GraphQL中有两种类型: article.mdx categories.json 我想查询我的帖子类型以获得此类结果: { "node": { "title": Post n.1 "category": { "name": "Category", "description": "This is a description", "order": 1 } } } 我该怎么做?我现在用的是盖茨比!谢谢。非常简单,因为您知道应该使用gat
{
"node": {
"title": Post n.1
"category": {
"name": "Category",
"description": "This is a description",
"order": 1
}
}
}
我该怎么做?我现在用的是盖茨比!谢谢。非常简单,因为您知道应该使用gatsby transformer remark来读取md文件,所以对于json文件,您应该使用gatsby transformer json,将其添加到插件下的gatsby-config.js文件中。然后你需要查询你的数据,不幸的是我真的不认为你可以按照你的要求组合两个文件来获取数据,但是你可以试试这个 首先,在gatsby-node.js文件中,您需要引用用于过滤查询数据的变量,并将这些字段传递到上下文中 然后,在页面查询中,您可以通过此阅读详细信息访问filterd查询 然后访问您可以通过const{allMarkdownRemark,allDataJson}=data访问您的数据 然后根据您的喜好组合这两个数据
const item = {node : { title: allMarkdownRemark.edges.node[0].frontmatter }};
item.node.category = allDataJson.edges.node[0].nodes
注意,这是假设edges.node是一个数组,因此我们需要按节点[0]精确计算数据的第一个元素,请检查此方法是否有效
json数据的结构是
{ "nodes": [ {
"name": "Category",
"description": "This is a description",
"order": 1
}
]
}
exports.createPages = async function({ actions, graphql }) {
const { data } = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
data.allMarkdownRemark.edges.forEach(edge => {
const slug = edge.node.fields.slug
actions.createPage({
path: slug,
component: require.resolve(`./src/templates/article.js`),
context: { category: category},
})
})
}
export const pageQuery = graphql`
query MyQuery($category: String!) {
allMarkdownRemark(filter: {frontmatter: {category: {eq: $category}}}) {
edges {
node {
frontmatter {
title
}
}
}
}
allDataJson(filter: {name: {eq: $category}}) {
edges {
node {
nodes {
name,
description,
order
}
}
}
}
}`
const item = {node : { title: allMarkdownRemark.edges.node[0].frontmatter }};
item.node.category = allDataJson.edges.node[0].nodes
{ "nodes": [ {
"name": "Category",
"description": "This is a description",
"order": 1
}
]
}