How to split schema.graphql file?

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