23

I'm using Apollo's graphql-server-express with Node and I would like to turn my "typedef" schema definition files into .graphql or .gql files for clarity and syntax highlighting.

What is the best way to do this? I cannot find any good resources online beyond Babel(?) which seems to be the only choice.

Thanks so much for the help!

Tyler Bainbridge
  • 249
  • 1
  • 2
  • 5

5 Answers5

24

I'm sure you found solution so far, but for others this might be helpful https://github.com/prismagraphql/graphql-import .

Usage

import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'

const typeDefs = importSchema('schema.graphql')
const resolvers = {}

const schema = makeExecutableSchema({ typeDefs, resolvers })

Examples

Importing specific fields

Assume the following directory structure:

.
├── a.graphql
├── b.graphql
└── c.graphql

a.graphql

# import B from "b.graphql"

type A {
  # test 1
  first: String
  second: Float
  b: B
}

b.graphql

# import C from 'c.graphql'

type B {
  c: C
  hello: String!
}

c.graphql

type C {
  id: ID!
}
robocat
  • 249
  • 2
  • 6
  • 3
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted. – R Balasubramanian Jul 31 '18 at 20:22
  • 2
    package seems to be discontinued – allanberry Feb 18 '21 at 19:02
11

The schemas folder should have files with .graphql or .gql files

const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');

const typesArray = loadFilesSync(path.join(__dirname, './schemas'));

module.exports = mergeTypeDefs(typesArray, { all: true });
Sai Pravesh
  • 165
  • 2
  • 8
7

Have a same question how to implement it in right way. Anyway this works for me.

import {gql} from 'apollo-server-express'
import * as types from './types.graphql'

export const typeDefs = gql`${types}`
Anton Kalik
  • 333
  • 2
  • 13
2

My solution for when using express-graphql with TypeScript:

import {buildSchema} from 'graphql';
import graphqlHTTP from 'express-graphql';
import {importSchema} from 'graphql-import';

import {resolvers} from './graphql/resolvers';

const server = express();

server.use(
  '/graphql',
  graphqlHTTP({
    schema: buildSchema(importSchema('**/*.graphql')),
    rootValue: resolvers,
  }),
);
lewislbr
  • 1,012
  • 14
  • 23
-1

I had the same question, and I believe the only way accomplish what you are asking without introducing babel/webpack into your server-side pipeline is to write you query as an exported template string.

e.g.

/* query.gql */
module.exports = `
schema {
  query: Query
  mutation: Mutation
}

type Query {
  posts: [Post]
  post(id:String, slug:String): Post
  user(uid:String): User
}
...
`

Thankfully, VS Code syntax highlighters for GraphQL support this method.

Efosa
  • 69
  • 3