graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.88k stars 840 forks source link

How to export go graphql schemas as graphql schema file? #353

Open FrontMage opened 6 years ago

FrontMage commented 6 years ago

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

ccbrown commented 6 years ago

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.

FrontMage commented 6 years ago

@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.

atombender commented 5 years ago

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.

javiertury commented 4 years ago

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.

ThomasP1988 commented 2 years ago

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