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

load_introspection_from_server does not use custom headers #25

Open
rdebroiz opened this issue Aug 30, 2019 · 3 comments
Open

load_introspection_from_server does not use custom headers #25

rdebroiz opened this issue Aug 30, 2019 · 3 comments

Comments

@rdebroiz
Copy link

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"
  }
}
@miketheman
Copy link

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

@P6rguVyrst
Copy link

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?

@rdebroiz
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants