Javascript 如何拆分长GraphQL模式

Javascript 如何拆分长GraphQL模式,javascript,graphql,Javascript,Graphql,我正在尝试创建一个模式,但是会变得太长和混乱,最好的做法是什么来分割不同的查询、突变和输入,这样我就可以只需要它们,并组织它们以便于阅读 我曾试图在网上查找信息,但没有任何明确的信息,我试图不使用阿波罗 const { buildSchema } = require('graphql'); module.exports = buildSchema(` type Region { _id: ID! name: String! countries: [Country!] } type

我正在尝试创建一个模式,但是会变得太长和混乱,最好的做法是什么来分割不同的查询、突变和输入,这样我就可以只需要它们,并组织它们以便于阅读

我曾试图在网上查找信息,但没有任何明确的信息,我试图不使用阿波罗

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
}
`);

我需要一些非常有条理的东西,使我能够把所有的事情都安排得井井有条,将所有文件合并到一个索引中是最好的解决方案。

有多个选项,这里有三个:

  • 您可以查看Apollo的博客-他们展示了一种模块化模式的方法: 如果你想要一个例子,你可以看看这个

  • 当然,您可以始终使用模板文本将部分架构嵌入为字符串:

  • 另一种方法是使用代码优先的方法,用javascript而不是graphql模式语言编写模式

  • 使其独立于文件夹和结构,并使代码可维护,我执行以下操作:

    const express=require('express');
    const glob=要求(“glob”);
    const{graphqlHTTP}=require('express-graphql');
    const{makeExecutableSchema,mergeResolver,mergeTypeDefs}=require('graphql-tools');
    常量app=express();
    //遍历文件夹“graphql/folder/folder/which*-resolver.js”中的解析器文件
    让resolvers=glob.sync('graphql/*/*/*/*-resolver.js')
    让registerResolvers=[];
    for(冲突解决程序的常量冲突解决程序){
    //将解析程序添加到数组
    registerResolvers=[…registerResolvers,需要('./'+解析器),]
    }
    //遍历文件夹“graphql/folder/folder/whatever*-type.js”中的解析器文件
    让types=glob.sync('graphql/*/*/*/*-type.js')
    让registerTypes=[];
    for(类型的常量类型){
    //向数组中添加类型
    registerTypes=[…registerTypes,需要('./'+类型),]
    }
    //使用“graphql工具包(makeExecutableSchema)”从typeDefs和解析器生成架构
    const schema=makeExecutableSchema({
    typeDefs:mergeTypeDefs(registerTypes),//合并数组类型
    冲突解决程序:合并冲突解决程序(registerResolvers,)//合并冲突解决程序类型
    })
    //mongodb连接,如果您更喜欢mongodb
    需要(“/助手/连接”);
    //结束mongodb连接
    //使其与express“express和express graphql包”配合使用
    app.use('/graphql',graphqlHTTP({
    schema:schema,
    graphiql:true,//在浏览器上测试查询或变异(仅限开发)
    }));
    app.listen(4000);
    console.log('在http://localhost:4000/graphql');您也可以使用
    图形SQL工具

    此工具合并了GraphQL类型定义和模式。它旨在合并所有可能的类型、接口、枚举和联合,而不发生冲突

    文档介绍了多种合并SDL的方法


    我举了一个使用阿波罗服务器的例子。
    techstack.

    这样我就可以创建多个文件并将它们嵌入到索引中,对吗?我将在未来研究阿波罗,因为我宁愿先学习如何在不使用任何其他库的情况下完成它。我需要先了解整个过程,谢谢你的帮助!是的,但我喜欢Apollo方法,但要确保至少有一个测试称为“buildSchema”,因为可能存在重复项,如果存在重复项,“buildSchema”会向您抛出错误。
    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);