graphql-python / gql

A GraphQL client in Python

Home Page:https://gql.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Load request from file

annndruha opened this issue · comments

The concept of graphql involves writing large but precise queries. Which are inconvenient to write as lines in the code. Many implementation of gql clients has a file-store queries as well as extensions for IDEs support syntax highlighting for .graphql and .gql files.

Feature request: add function for easy load query from .graphql or .gql files.

For example, add optional parameter file_patch in gql (or file-like object), or create a separate function to load.

Implementation is also simple like this:

# https://github.com/graphql-python/gql/blob/master/gql/gql.py
def gql_from_file(file_path: str) -> DocumentNode:
      with open(file_path) as f:
          return gql(f.read())

# Call
from gql import gql_from_file
doc_node = gql_from_file('some_query.graphql')

I will be glad to make a pull request after the discussion :)

Sorry, reading a file is a standard Python feature and I don't see the point of integrating it in gql.

It's the same with reading the GraphQL schema which requires the user to use the open method himself to read the file (or any other way to read a file in Python)

The best code is no code at all!