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!
I'm sure you found solution so far, but for others this might be helpful https://github.com/prismagraphql/graphql-import .
import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'
const typeDefs = importSchema('schema.graphql')
const resolvers = {}
const schema = makeExecutableSchema({ typeDefs, resolvers })
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!
}
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 });
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}`