Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace schema parser with graphql-core-legacy #30

Open
execveat opened this issue Mar 30, 2023 · 0 comments
Open

Replace schema parser with graphql-core-legacy #30

execveat opened this issue Mar 30, 2023 · 0 comments
Labels
Enhancement New feature or request Parsing Affects parsing of GraphQL queries

Comments

@execveat
Copy link
Contributor

Use https://github.com/graphql-python/graphql-core-legacy for parsing GraphQL schema and make the GraphQL-Core AST format as the main internal representation.

This should ease the maintenance and provide many features like SDL / JSON interoperability for free.

Parsing JSON schema and converting it into SDL:

from graphql.utils.build_client_schema import build_client_schema
from graphql import print_schema

with open('introspection.json') as f:
  json_schema = json.load(f)

# Parses schema from JSON representation, expectes '__schema'
schema_from_json = build_client_schema(json_schema['data'])

# Iterate over queries
for query_name in schema_from_json.get_type('Query').fields:
  print(query_name)

# Write SDL 
 with open('schema.graphql', 'w') as f:
  f.write(schema_printer.print_schema(schema))

Parsing SDL schema and converting it into JSON:

from graphql import build_ast_schema
from graphql.language.base import parse
from graphql.utils.introspection_query import introspection_query

with open('schema.graphql') as f:
  sdl_string = f.read()

schema_from_sdl = build_ast_schema(parse(sdl_string))

# Iterate over queries
for query_name in schema_from_sdl.get_type('Query').fields:
  print(query_name)

# Write JSON
with open('schema.graphql', 'w') as f:
  introspection_result = graphql.graphql(schema_from_sdl, request_string=introspection_query)
  json.dump(introspection_result, f)
@execveat execveat added Enhancement New feature or request Parsing Affects parsing of GraphQL queries labels Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Parsing Affects parsing of GraphQL queries
Projects
None yet
Development

No branches or pull requests

1 participant