How to split a long GraphQL schema

前端 未结 3 1722
情深已故
情深已故 2021-02-08 07:32

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 r

3条回答
  •  太阳男子
    2021-02-08 08:19

    There are multiple options, here are three of them:

    1. 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

    2. 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);
    
    1. Another way is to use the code first approach and write the schemas in javascript instead of graphql schema language.

提交回复
热议问题