Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert GraphQL schema to JSON and best to GraphQL schema?

I have figured out that this gives me GraphQLSchema, which I expect I can convert to JSON. How?

// @flow

import path from 'path';
import fs from 'fs';
import {
  buildSchema,
} from 'graphql';

const main = () => {
  const schemaPath = path.resolve(__dirname, '../schema.graphql');

  console.log(buildSchema(fs.readFileSync(schemaPath, 'UTF8')));
};

main();

And how to do the inverse – how to convert JSON representation of GraphQL schema to GraphQL markup?

One way I have found to do it is:

import {
  printSchema,
  buildSchema,
  buildClientSchema,
  printIntrospectionSchema,
  introspectionFromSchema,
} from 'graphql';

printSchema(buildClientSchema(introspectionFromSchema(buildSchema(fs.readFileSync('./schema.graphql', 'UTF8')))))

However, this looses a lot of data, e.g.

type Venue implements Node @preLoad {
  # test
  id: ID!
  fuid: String!
  url: String!
  name: String!
  cinema: Cinema!
  futureEventCount: Int!
  globalCinemaVenue: GlobalCinemaVenue
}

becomes:

type Venue implements Node {
  id: ID!
  fuid: String!
  url: String!
  name: String!
  cinema: Cinema!
  futureEventCount: Int!
  globalCinemaVenue: GlobalCinemaVenue
}

like image 900
Gajus Avatar asked Oct 27 '19 13:10

Gajus


People also ask

Does GraphQL work with JSON?

GraphQL services typically respond using JSON, however the GraphQL spec does not require it. JSON may seem like an odd choice for an API layer promising better network performance, however because it is mostly text, it compresses exceptionally well with GZIP.

How do I change schema in GraphQL?

Select the database you created in previous articles and go to the GRAPHQL section from the left menu bar. Click on the UPDATE SCHEMA button and select the file containing the updated schema. At the database layer, the update process creates any missing collections, indexes, and functions.

Can GraphQL have multiple schemas?

Hence, we can create two separate schemas, the Admin and Public schemas, and expose them under endpoints /graphql/admin and /graphql respectively.


2 Answers

You could use the following package which wasn't available at the time of this post I believe: https://www.npmjs.com/package/graphql-2-json-schema

import {
    graphqlSync,
    getIntrospectionQuery,
    IntrospectionQuery
} from 'graphql';

import { fromIntrospectionQuery } from 'graphql-2-json-schema';

const options = {
  ignoreInternals: true,
  nullableArrayItems: true
}

const introspection = graphqlSync(schema, getIntrospectionQuery()).data as IntrospectionQuery;

const jsonSchema = fromIntrospectionQuery(introspection, options);
like image 144
Herman Geldenhuys Avatar answered Nov 02 '22 12:11

Herman Geldenhuys


You do not need an external library here, you have all you need with graphql-js :

import { introspectionFromSchema } from "graphql"
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader"
import { loadSchema } from "@graphql-tools/load"

// it can be any source you want here
const schema = loadSchema("./schema.graphql", {
  loaders: [new GraphQLFileLoader()],
})  
const introspection = introspectionFromSchema(schema)

// Tada, you have your introspection
like image 20
Guillaume Lamanda Avatar answered Nov 02 '22 11:11

Guillaume Lamanda