单独的页面graphQL查询在单独的文件中(.graphql)

问题描述 投票:2回答:4

是否可以在单独的.graphql文件中分离页面graphql查询,然后将其导入页面文件?我假设我需要添加一些og .graphQL加载器?

graphql gatsby
4个回答
0
投票

检查'apollo-universal-starter-kit' - 应该适应gatsby


0
投票

如果您想用querymutation文件扩展名来编写.graphql.gql

你需要graphql-tag/loader,这是我的webpack.config.js

  {
    test: /\.(graphql|gql)$/,
    exclude: /node_modules/,
    use: 'graphql-tag/loader'
  }

0
投票

据我所知,开箱即用是不可能的。分割查询的唯一方法是使用Fragments:https://www.gatsbyjs.org/docs/querying-with-graphql/#fragments

但是,如果您想开始讨论,我建议您在GitHub上打开一个问题。


0
投票

你可以使用graphql-import来做到这一点。

示例:假设以下目录结构:

.
├── schema.graphql
├── posts.graphql
└── comments.graphql

schema.graphql

# import Query.*, Mutation.* from "posts.graphql"
# import Query.*, Mutation.* from "comments.graphql"

posts.graphql

# import Comment from 'comments.graphql'

type Post {
  comments: [Comment]
  id: ID!
  text: String!
  tags: [String]
}

comments.graphql

type Comment {
  id: ID!
  text: String!
}

您可以在here中看到完整的文档

© www.soinside.com 2019 - 2024. All rights reserved.