gql-next icon indicating copy to clipboard operation
gql-next copied to clipboard

load_introspection_from_server does not use custom headers

Open rdebroiz opened this issue 6 years ago • 3 comments

Hello I'm trying to test gql but running gql run returned


[...]

  File "/home/rdebroiz/.virtualenvs/test_gql/lib/python3.7/site-packages/gql/utils_schema.py", line 22, in load_schema
    introspection = load_introspection_from_file(uri) if os.path.isfile(uri) else load_introspection_from_server(uri)
  File "/home/rdebroiz/.virtualenvs/test_gql/lib/python3.7/site-packages/gql/utils_schema.py", line 13, in load_introspection_from_server
    raise Exception(f'Query failed to run by returning code of {request.status_code}. {query}')
Exception: Query failed to run by returning code of 401.

[...]

I suspect the cause is that the custom headers are not used to load the schema from the configured endpoint.

def load_introspection_from_server(url):
    query = get_introspection_query()
    request = requests.post(url, json={'query': query})
    if request.status_code == 200:
        return request.json()['data']

    raise Exception(f'Query failed to run by returning code of {request.status_code}. {query}')

Here is what my .gql.json look likes:

{
  "schema": "http://hasura:10004/v1alpha1/graphql",
  "endpoint": "http://hasura:10004/v1alpha1/graphql",
  "documents": "./**/*.graphql",
  "custom_header": {
    "x-hasura-admin-secret": "adminpw",
    "x-hasura-access-key": "userpw"
  }
}

rdebroiz avatar Aug 30 '19 10:08 rdebroiz

Similar issue - pointing the schema configuration directive at GitHub's v4 API produces a similar response - since queries that endpoint must be authenticated. https://developer.github.com/v4/guides/intro-to-graphql/#discovering-the-graphql-api

miketheman avatar Sep 25 '19 12:09 miketheman

Screenshot 2020-02-19 at 20 10 47

After editing the source files in my virtualenv to accept my custom headers I ran into this.

I don't know much about GraphQL, but is there a valid reason for a static code generator to be able to access http servers to gather more info about schemas? If that's the case, then it feels like a major turn off for a project that otherwise looks really promising.

Can you have an offline mode where I don't have to access external schema endpoints and provide authentication information?

P6rguVyrst avatar Feb 19 '20 20:02 P6rguVyrst

@P6rguVyrst I think the reasonj why they need your graphql endpoint is not to access the schema, It's supposed to work with a file.

def load_introspection_from_file(filename):
    with open(filename, 'r') as fin:
        return json.load(fin)


def load_schema(uri):
    introspection = load_introspection_from_file(uri) if os.path.isfile(uri) else load_introspection_from_server(uri)
    return build_client_schema(introspection)

But because they also want to provide a configured client ready to use for later (with the given graphql endpoint) in the generated code. I agree with you that it should definitely be out of the scope of the library.

Anyway it looks like the project has been deserted. I wonder if this was because they were a major flaw in the way it is supposed to work, or just because they move on something else.

In the later case I could be interested to continue the work one day, I like the idea a lot. It reminds me https://github.com/honza/anosql (but better thanks to the generated dataclass) for graphql.

rdebroiz avatar Feb 20 '20 09:02 rdebroiz