How do I read the schema off disk?

Viewed 14

I have the following Apollo example:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`)
});

I'm just not super sure what the gql thing is and how to pass it a string that I read from disk (my schema is too big to inline here). I tried following the definitions but it's a maze of abstraction that's hard to figure out.

1 Answers

You can read a file with fs.readFileSync.

gql`
  type Query {
    hello: String
  }
`

is a tagged template. It's a simple function call: gql(file).

Read the file from disk and call gql:

const fs = require('fs');
const { ApolloServer, gql } = require('apollo-server');

const file = fs.readFileSync('path', { encoding: 'UTF8' });
const typeDefs = gql(file);

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`)
});
Related