Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gql or .graphql file types with Node

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!

like image 792
Tyler Bainbridge Avatar asked Apr 24 '17 17:04

Tyler Bainbridge


People also ask

What is .GQL file?

The GQL file type is primarily associated with BI/Query. GQL. File extension: GQL. File type: Data Model.

What other types can be used in a GraphQL schema?

A GraphQL schema can contain more than just the scalar types. The two most used types are query and mutation types. Every GraphQL service will have a query type. This is the entry point to a GraphQL schema.

When using GraphQL which defines a set of types?

GraphQL comes with a set of default scalar types out of the box: Int : A signed 32‐bit integer. Float : A signed double-precision floating-point value. String : A UTF‐8 character sequence.

What is GraphQL Nodejs?

GraphQL is defined as a query language for client APIs and server-side runtime to execute those queries. That said, GraphQL is not exactly a language, but it has its own syntax and can be developed within several programming languages, such as Node. js.


2 Answers

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!
}
like image 160
robocat Avatar answered Sep 28 '22 05:09

robocat


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 });
like image 34
Sai Pravesh Avatar answered Sep 28 '22 04:09

Sai Pravesh