Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a long GraphQL 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.

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.

like image 330
Scarecrowsama Avatar asked Jul 15 '19 13:07

Scarecrowsama


People also ask

Can GraphQL have multiple schemas?

The idea is to define several GraphQL schemas, and tell the server which one to use on runtime, based on the requested endpoint. When using a JavaScript server, a convenient way to achieve this is with GraphQL Helix, which decouples the handling of the HTTP request from the GraphQL server.

Is GraphQL an overkill?

GraphQL can be overkill Since GraphQL is so complex and powerful, it can commonly be overkill for smaller applications. If you have a simple/straight-forward app in front of you, it will be easier to work with a REST API.


1 Answers

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.
like image 126
Liron Navon Avatar answered Sep 19 '22 07:09

Liron Navon