quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

How to convert graphql to json-schema?

Open sarink opened this issue 5 years ago • 2 comments

Is this possible? I can't seem to figure it out

Input:

# test.graphql
type Foo {
  required: String!
  optional: String
}
quicktype --src test.graphql --src-lang graphql --lang schema --out test-json-schema.json

Yields:

Error: Please specify a GraphQL schema with --graphql-schema or --graphql-introspect

So, I tried also giving quicktype my entire schema (although I only want the above test.graphql, I'm not sure why quicktype would require the entire schema for this operation?):

quicktype --src test.graphql --src-lang graphql --graphql-schema codegen/schema.json --lang schema --out test-json-schema.json

And that yielded:

Error: Cannot read property '__schema' of undefined.

My codegen/schema.json file is the result of an introspection query (generated by graphql-code-generator) and definitely does have a top-level __schema key, so I'm not sure why this is happening.

sarink avatar May 25 '20 22:05 sarink

I realize this is very old but I stumbled upon this question before ultimately finding the answer (via https://github.com/Shopify/graphql-js-schema/issues/36). It turns out that quicktype expects the json file to be of the format { "data": { "__schema": { ... } } }

So wrapping your json in that should make it work. It seems like the reason for this is that this is the expected format of an introspection (https://graphql.org/learn/introspection/). The tools I was using to generate the schema (graphene in Python) did not output this form either.

rosenbaum avatar Aug 25 '21 06:08 rosenbaum

tmp=$(mktemp)

# fetch the introspection data and save it locally to a temp file
yarn quicktype \
    --graphql-introspect http://localhost:7500/gql/query \
    --graphql-schema "$tmp"

# combine the introspection json, our graphql schema sdl and a graphql query sdl to create jsonschema
yarn quicktype \
    --src-lang graphql --lang schema \
    --graphql-schema "$tmp" \
    --src ./internal/graph/config.graphql \
    --out ./internal/store/config-schema.json

rm "$tmp"

airtonix avatar Aug 06 '24 01:08 airtonix