35

I am currently loading the GraphQL schema using a separate .graphql file, but it is encapsulated within strings:

schema.graphql

const schema = `
  type CourseType {
    _id: String!
    name: String!
  }

  type Query {
    courseType(_id: String): CourseType
    courseTypes: [CourseType]!
  }
`

module.exports = schema

Then using it for the apollo-server:

index.js

const { ApolloServer, makeExecutableSchema } = require('apollo-server')
const typeDefs = require('./schema.graphql')

const resolvers = { ... }

const schema = makeExecutableSchema({
  typeDefs: typeDefs,
  resolvers
})

const server = new ApolloServer({
  schema: schema
})

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

Is there any way to simply load a .graphql that looks as such? schema.graphql

type CourseType {
  _id: String!
  name: String!
}

type Query {
  courseType(_id: String): CourseType
  courseTypes: [CourseType]!
}

Then it would be parsed in the index.js? I noticed that graphql-yoga supports this, but was wondering if apollo-server does. I cannot find it anywhere in the docs. I can't get fs.readFile to work either.

Sam Sverko
  • 1,480
  • 4
  • 15
  • 32

5 Answers5

42

If you define your type definitions inside a .graphql file, you can read it in one of several ways:

1.) Read the file yourself:

const { readFileSync } = require('fs')

// we must convert the file Buffer to a UTF-8 string
const typeDefs = readFileSync(require.resolve('./type-defs.graphql')).toString('utf-8')

2.) Utilize a library like graphql-tools to do it for you:

const { loadDocuments } = require('@graphql-tools/load');
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');

// this can also be a glob pattern to match multiple files!
const typeDefs = await loadDocuments('./type-defs.graphql', { 
    file, 
    loaders: [
        new GraphQLFileLoader()
    ]
})

3.) Use a babel plugin or a webpack loader

import typeDefs from './type-defs.graphql'
Loren
  • 13,903
  • 8
  • 48
  • 79
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    This is amazing! No way there's an equivalent `GraphQLFileLoader` method from `apollo-server`? – Sam Sverko Jun 09 '20 at 21:38
  • 1
    Maybe in a future version. – Daniel Rearden Jun 09 '20 at 22:15
  • It should be noted that `readFileSync` only returns a string if you specify an encoding, eg: `const typeDefs = readFileSync('./schema.graphql', 'utf-8')`. If you do not include an encoding, it returns as a Buffer which ApolloServer's constructor does not understand. – Soviut Dec 20 '20 at 05:24
  • thank you for noting about `'utf-8'` in `readFileSync`, I would have missed that – Paul Mikulskis Nov 09 '21 at 22:19
  • @DanielRearden I am getting this error when loading a subgraph schema in a federated graph ```ts Unknown directive "@key". Cannot extend type "Membership" because it is not defined. Unknown directive "@key". Unknown directive "@external". Unknown type "Query". ``` – Hazem Alabiad Jan 11 '22 at 12:32
  • @Hazem Alabiad I used the 1st option from this answer and wrapped it within `gql` for the federated subgraph. `const typeDefs = gql(readFileSync(require.resolve('./schema.graphql')).toString('utf-8'));` – MMH Apr 22 '22 at 11:17
  • @DanielRearden where is the file variable coming from in the 2nd example ie in the graphql-tools example – nshathish Feb 06 '23 at 07:28
  • @DanielRearden did you get this working with Apollo Server-I tried the graphql-tools method with Apollo server, but getting this error: " Unable to find any GraphQL type definitions for the following pointers" - wondering if you need to do anything extra to get it working with Apollo Server – nshathish Feb 07 '23 at 06:30
2

Back in the day I wrote a teeny-tiny .graphql loader myself. It is very small, very simple, and the only thing you have to do is import it before you try to import any .graphql files. I have used it ever since even though I am sure that there are some 3rd party loaders available. Here's the code:

// graphql-loader.js

const oldJSHook = require.extensions[".js"];

const loader = (module, filename) => {
  const oldJSCompile = module._compile;
  module._compile = function (code, file) {
    code = `module.exports = \`\r${code}\`;`;
    module._compile = oldJSCompile;
    module._compile(code, file);
  };
  oldJSHook(module, filename);
};

require.extensions[".graphql"] = loader;
require.extensions[".gql"] = loader;

And then in your app:

// index.js

import "./graphql-loader"; // (or require("./graphql-loader") if you prefer)

That's it, you can then import typeDefs from "./type-defs.graphql" wherever you want.

The loader works by wrapping the text in your .graphql file inside a template string and compiling it as a simple JS module:

module.exports = ` ...your gql schema... `;
Avius
  • 5,504
  • 5
  • 20
  • 42
1

This worked for me:

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

//function that imports .graphql files
const importGraphQL = (file) =>{
  return fs.readFileSync(path.join(__dirname, file),"utf-8");
}

const gqlWrapper = (...files)=>{
  return gql`${files}`;
}


const enums = importGraphQL('./enums.graphql');
const schema = importGraphQL('./schema.graphql');

module.exports = gqlWrapper(enums,schema);

0

Figured it out using fs (thanks to Tal Z):

index.js

const fs = require('fs')
const mongoUtil = require('./mongoUtil')
const { ApolloServer, makeExecutableSchema } = require('apollo-server')

function readContent (file, callback) {
  fs.readFile(file, 'utf8', (err, content) => {
    if (err) return callback(err)
    callback(null, content)
  })
}

mongoUtil.connectToServer((error) => {
  if (error) {
    console.error('Error connecting to MongoDB.', error.stack)
    process.exit(1)
  }

  console.log('Connected to database.')

  const Query = require('./resolvers/Query')

  const resolvers = {
    Query
  }

  readContent('./schema.graphql', (error, content) => {
    if (error) throw error

    const schema = makeExecutableSchema({
      typeDefs: content,
      resolvers
    })

    const server = new ApolloServer({
      schema: schema
    })

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

schema.graphql

type CourseType {
  _id: String!
  name: String!
}

type Query {
  courseType(_id: String): CourseType
  courseTypes: [CourseType]!
}
Sam Sverko
  • 1,480
  • 4
  • 15
  • 32
0

I use this helper:

import { readFileSync } from "fs";
const requireGQL = (file) =>
  gql`${readFileSync(require.resolve(file)).toString("utf-8")}`;

Throw that at the top of your file and you can just:

const client = new ApolloClient({ uri: 'https://myendpoint.com/graphql' });
const { data } = await client.query({ query: requireGQL("./myquery.gql") });
Dom Vinyard
  • 2,038
  • 1
  • 23
  • 36