graphql icon indicating copy to clipboard operation
graphql copied to clipboard

How to export go graphql schemas as graphql schema file?

Open FrontMage opened this issue 6 years ago • 5 comments

Wonder if there is a way to export schemas or just have to write it twice.

FrontMage avatar Jul 14 '18 03:07 FrontMage

I don't think this library has built-in support for exporting GraphQL schema files, but you definitely don't need to write your schema twice. There are tools that can generate schemas from running servers or from JSON introspection results. So for example, if you install graphql-cli, you can run graphql init, give it a project name and the endpoint for your server, then run graphql get-schema to get a GraphQL schema language file for your server.

ccbrown avatar Jul 20 '18 21:07 ccbrown

@ccbrown Thanks for the advice. But the graphql-cli is using __Type to query schemas.

Query err=Unknown type "__Type".
Query err=Unknown type "__InputValue".
Query err=Unknown type "__Type".

Apparently this lib does not implement these by default. I'm thinking a __Type field for the query and then use reflect to return types.

FrontMage avatar Jul 23 '18 06:07 FrontMage

I'd also like this. At the moment, you have to fire up a server and run a tool such as graphql-cli or (more conveniently) get-graphql-schema to get the schema printed, which is awkward.

Interestingly, this repo does have a parser that parses a schema grammar into an AST, which can then be printed. But there's no way to print a schema that's expressed as Go types.

You can get the schema with a standard introspection query such as this by running it through graphql.Do(), but the results are, of course, JSON, not GraphQL's grammar.

atombender avatar Jan 31 '19 23:01 atombender

I've found a way to print the schema

import { buildASTSchema, printSchema } from 'graphql';
import gql from 'gql-tag';

const typeDefs = gql`
type Query {
  hello: String
}
`;
const schema = buildASTSchema(typeDefs);
const printedSchema = printSchema(buildASTSchema);
console.log(printedSchema);

This process can combine the utilities makeExecutableSchema, mergeTypeDefs, stitchSchemas, etc. from 'graphql-tools' to print your stiched schemas.

javiertury avatar Jul 20 '20 17:07 javiertury

https://stackoverflow.com/questions/60451843/how-can-one-get-a-complete-graphql-schema-from-an-apollo-server-endpoint

t0mpl avatar May 14 '22 11:05 t0mpl