How to split schema.graphql file?

The GraphQL API uniformly defines the schema in the schema.graphql file, which is difficult to maintain when the number of schemas is large.

If schema.graphql can be split into different types of [name].graphql files, and they can refer to each other, then this problem will be solved.

you can split them up then use the fancy bash script to send it to dgraph/slashgraphgql

cat *.graphql >> schema.graphql

You might want to ensure schema.graphql isn’t there first so

rm schema.graphql && cat *.graphql >> schema.graphql

Thanks

@michaelcompton, can you share here that schema builder that you have on github separating the auth from the schema. I think it sort of applies to the OP in making schema easier to handle. I don’t have that link on hand.

sure … it’s not 100% complete. But I have been doing things like in here

I have a schema.graphql file that uses auth like:

type User @auth(
  # query : anyone can query 
  add: { rule: "<<is-this-user>>" },
  update: { rule: "<<is-this-user>>" },
  delete: { rule: "{$role: {eq: \"Admin\"}}" }
) {
  username: String! @id
  displayName: String
  avatarImg: String
  posts: [Post] @hasInverse(field: author)
  roles: [Permission] @hasInverse(field: user)
}

Then I have a separate file is-this-user.auth.graphql

query isThisUser($username: String!) {
  queryUser(filter: { username: { eq: $username } }) {
    username
  }
}

I also hookup my VS Code setup to my development GraphQL instance. That means I get graphql editing help for the is-this-user.auth.graphql file and if the schema changes, I get a red squiggle.

Then I have a node.js file that deploys to Slash GraphQL for me. It takes the schema.graphql file, substitutes the contents of is-this-user.auth.graphql wherever it sees rule: "<<is-this-user>>" and so on for other rules and uploads to slash.

It works really nicely for me because it removes any magic strings and I never have to repeat my auth rules.

It could be expanded to accept multiple files as the schema … I think one of the VS Code graphql extensions lets you #include ... other graphql files so you get editing help across multiple schema files, but no 100% sure how that works.

5 Likes