我正在尝试创建一个Schema,但是会变得太长和混乱,什么是拆分不同查询,变异和输入的最佳实践,所以我只需要它们并组织它们以使其易于阅读
我尝试过在线查找信息,但目前还不清楚,我也尝试不使用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
}
`);
我需要一种井井有条的东西,使我能够井井有条并清除所有文件,将它们合并到一个索引中是最好的解决方案。
答案 0 :(得分:2)
将其分别设置为文件夹和结构,以使代码可维护,我将执行以下操作:
const express = require('express');
const glob = require("glob");
const {graphqlHTTP} = require('express-graphql');
const {makeExecutableSchema, mergeResolvers, mergeTypeDefs} = require('graphql-tools');
const app = express();
//iterate through resolvers file in the folder "graphql/folder/folder/whatever*-resolver.js"
let resolvers = glob.sync('graphql/*/*/*-resolver.js')
let registerResolvers = [];
for (const resolver of resolvers){
// add resolvers to array
registerResolvers = [...registerResolvers, require('./'+resolver),]
}
//iterate through resolvers file in the folder "graphql/folder/folder/whatever*-type.js"
let types = glob.sync('graphql/*/*/*-type.js')
let registerTypes = [];
for (const type of types){
// add types to array
registerTypes = [...registerTypes, require('./'+type),]
}
//make schema from typeDefs and Resolvers with "graphql-tool package (makeExecutableSchema)"
const schema = makeExecutableSchema({
typeDefs: mergeTypeDefs(registerTypes),//merge array types
resolvers: mergeResolvers(registerResolvers,)//merge resolver type
})
// mongodb connection if you prefer mongodb
require('./helpers/connection');
// end mongodb connection
//Make it work with express "express and express-graphql packages"
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,//test your query or mutation on browser (Development Only)
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
答案 1 :(得分:0)
有多个选项,其中有三个:
您可以看一下Apollo的博客-他们展示了一种将模式模块化的方法:this 如果您想举个例子,可以看看这个Modularizing your GraphQL schema code
当然,您总是可以使用模板文字将模式的一部分作为字符串嵌入:
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);
答案 2 :(得分:0)
您也可以使用graphql-tools
中的Type definitions (SDL) merging。
此工具合并了GraphQL类型定义和架构。它旨在合并所有可能的类型,接口,枚举和联合,而不会发生冲突。
该文档介绍了多种合并SDL的方法。
我以apollo-server
技术栈为例modularizing-the-schema。