如何拆分长GraphQL模式 [英] How to split a long GraphQL schema
问题描述
我正在尝试创建一个Schema,但是会变得太长和混乱,什么是拆分不同查询,变异和输入的最佳实践,所以我只需要它们并组织它们以使其易于阅读即可
I am trying to create a Schema, however is going to get too long and confusing, what are the best practices to split the different queries, mutations and inputs so I can just require them and organize them to make it easy to read.
我尝试过在线查找信息,但目前尚不清楚,我也尝试不使用Apollo.
I have tried to find information online but there is nothing clear at all and I am trying not to use Apollo.
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Region {
_id: ID!
name: String!
countries: [Country!]
}
type Country {
_id: ID!
name: String!
region: [Region!]!
}
type City {
_id: ID!
name: String!
country: [Country!]!
}
type Attraction {
_id: ID!
name: String!
price: Float!
description: String!
city: [City!]!
}
type Eatery {
_id: ID!
name: String!
cuisine: String!
priceRange: String!
location: [Location!]!
typeOfEatery: String!
city: [City!]!
}
type Location {
_id: ID!
latitude: String!
longitude: String!
address: Float
}
type User {
_id: ID!
email: String!
password: String!
}
type AuthData {
userId: ID!
token: String!
tokenExpiration: String!
}
type RegionInput {
name: String!
}
type CountryInput {
name: String!
}
type CityInput {
name: String!
}
type RootQuery {
regions: [Region!]!
countries: [Country!]!
login(email: String!, password: String!): AuthData!
}
type RootMutation {
createRegion(regionInput: RegionInput): Region
createCountry(countryInput: CountryInput): Country
createCity(cityInput: CityInput): City
}
schema {
query: RootQuery
mutation: RootMutation
}
`);
我需要一个井井有条的东西,使我能够井井有条并清除所有内容,将所有文件合并到一个索引中是最好的解决方案.
I need something that is very organized and allows me to get everything in order and clear, merge all files in one index is the best solution.
推荐答案
有多个选项,其中有三个:
There are multiple options, here are three of them:
-
您可以看一下Apollo的博客-他们展示了一种将模式模块化的方法: github存储库
You can take a look at Apollo's blog - they show a way to modularize the schema: Modularizing your GraphQL schema code If you want an example, you can take a look at this github repository
当然,您始终可以始终使用模板文字将模式的一部分作为字符串嵌入:
Of course you can always just use the template literals to embed parts of the schema as strings:
const countryType = `
type Country {
_id: ID!
name: String!
region: [Region!]!
}
`
const regionType = `
type Region {
_id: ID!
name: String!
countries: [Country!]
}
`
const schema = `
${countryType}
${regionType}
# ... more stuff ...
`
module.exports = buildSchema(schema);
- 另一种方法是使用代码优先方法,并使用javascript而不是graphql模式语言编写模式.
这篇关于如何拆分长GraphQL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!