Skip to content

Commit

Permalink
Set graphql schema on view in history tab before request is sent, mak…
Browse files Browse the repository at this point in the history
…e it visible to user if schema hasn't been set yet
  • Loading branch information
kylebebak committed Oct 4, 2017
1 parent 811fc03 commit 854699a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -475,7 +475,7 @@ Autocomplete depends on [graphql-py](https://github.com/ivelum/graphql-py/), a p

>If autocomplete doesn't work, it's probably being overridden by another autocomplete package, like [All Autocomplete](https://github.com/alienhard/SublimeAllAutocomplete), [Djaneiro](https://github.com/squ1b3r/Djaneiro), [tern_for_sublime](https://github.com/ternjs/tern_for_sublime), etc. Remove these packages, or disable them for Requester response views if possible.
>GraphQL autocomplete is guaranteed not to override autocomplete from your other packages.
>GraphQL autocomplete will never override autocomplete from your other packages.

## Import Any Python Package with Requester
Expand All @@ -493,14 +493,14 @@ Here are a couple of no-brainers:
### Pip3 Quickstart
If you don't have `virtualenv` or you're not comfortable using it, the quick solution is to install Python 3, which will install `pip3` and `python3` executables. Run `which pip3` to make sure you've done this.

>Note: Sublime Text runs Python 3.3, and there are some packages, such as `browsercookie`, that can only be imported by Sublime Text if they are downloaded with `pip3.3`. The best way to download Python 3.3 is with [pyenv](https://gist.github.com/Bouke/11261620), but this can be a bit of pain. My advide: don't bother unless you really want to use one of these packages.
Then run `pip3 install requests-oauthlib`, `pip3 install requests-toolbelt`, `pip3 install graphql-py`, and so on for whatever packages you'd like to use with Requester.

Finally, run `pip3 show requests-oauthlib`, and look for the __LOCATION__ field in the output. Now you know where pip is installing your packages.

Use this path as your `packages_path` setting in Requester's settings file. To open these settings, look for __Requester: Settings__ in the command palette.

>Note: Sublime Text runs Python 3.3, and there are some packages, such as `browsercookie`, that can only be imported by Sublime Text if they are downloaded with `pip3.3`. The best way to download Python 3.3 is with [pyenv](https://gist.github.com/Bouke/11261620), but this can be a bit of pain. My advide: don't bother unless you really want to use one of these packages.

### OAuth1 and OAuth2
__requests-oauthlib__ makes OAuth1 and OAuth2 a lot easier. Let's say you want explore the Twitter REST API, which uses OAuth1 for authentication. Go to <https://apps.twitter.com/app/new>, create a new application, then go to the __Keys and Access Tokens__ tab for your application. Generate an access token and an access token secret, grab your API key and secret, and pass them to `OAuth1`.
Expand Down
15 changes: 11 additions & 4 deletions commands/graphql.py
Expand Up @@ -84,7 +84,7 @@
"""


def set_graphql_on_view(view, req):
def set_graphql_schema_on_view(view, req):
"""If request was to a GraphQL endpoint, send introspection query on a separate
thread, parse response and set it on view.
"""
Expand Down Expand Up @@ -122,12 +122,13 @@ def _set(view, url):

class RequesterGqlAutocompleteListener(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
"""Runs on all views, but is NOOP unless gql schema has been cached on
"""Runs on all views, but is NOOP unless view is response view or history
view. Inside gql query string, only completions returned by this method
are shown.
"""
schema = view.settings().get('requester.gql_schema', None)
if not schema:
response_view = view.settings().get('requester.response_view', False)
history_view = view.settings().get('requester.history_view', False)
if not response_view and not history_view:
return None

content = view.substr(sublime.Region(0, view.size()))
Expand All @@ -145,6 +146,12 @@ def on_query_completions(self, view, prefix, locations):
view.settings().get('requester.env_string', None)
)
req = prepare_request(request, view._env, 1)

schema = view.settings().get('requester.gql_schema', None)
if not schema: # let user know schema is being retrieved
set_graphql_schema_on_view(view, req)
raise Exception('Loading GraphQL schema info')

gql = req.skwargs['gql']
completions = get_completions(gql, idx-offset, schema)
return completions
Expand Down
8 changes: 4 additions & 4 deletions commands/request.py
Expand Up @@ -8,7 +8,7 @@
from urllib import parse
from collections import namedtuple

from .graphql import set_graphql_on_view
from .graphql import set_graphql_schema_on_view
from ..core import RequestCommandMixin
from ..core.parsers import parse_requests
from ..core.responses import prepare_request
Expand Down Expand Up @@ -253,7 +253,7 @@ def handle_response(self, response):
if self.config.get('reorder_tabs_after_requests', False):
self.view.run_command('requester_reorder_response_tabs')
set_response_view_name(view, res)
set_graphql_on_view(view, req)
set_graphql_schema_on_view(view, req)

def handle_responses(self, responses):
"""Change focus after request returns? `handle_response` must be called
Expand Down Expand Up @@ -322,7 +322,7 @@ def handle_response(self, response):
view.settings().erase('requester.request_history_index')
view.settings().set('requester.history_view', False)
set_response_view_name(view, res)
set_graphql_on_view(view, req)
set_graphql_schema_on_view(view, req)


class RequesterExploreUrlCommand(RequesterReplayRequestCommand):
Expand Down Expand Up @@ -375,7 +375,7 @@ def handle_response(self, response):
view.set_syntax_file('Packages/Requester/syntax/requester-response.sublime-syntax')
set_request_on_view(view, res)
set_response_view_name(view, res)
set_graphql_on_view(view, req)
set_graphql_schema_on_view(view, req)

def persist_requests(self, responses):
"""Don't do this for exploratory requests.
Expand Down
6 changes: 3 additions & 3 deletions docs/_content/body.md
Expand Up @@ -389,7 +389,7 @@ Autocomplete depends on [graphql-py](https://github.com/ivelum/graphql-py/), a p

>If autocomplete doesn't work, it's probably being overridden by another autocomplete package, like [All Autocomplete](https://github.com/alienhard/SublimeAllAutocomplete), [Djaneiro](https://github.com/squ1b3r/Djaneiro), [tern_for_sublime](https://github.com/ternjs/tern_for_sublime), etc. Remove these packages, or disable them for Requester response views if possible.
>GraphQL autocomplete is guaranteed not to override autocomplete from your other packages.
>GraphQL autocomplete will never override autocomplete from your other packages.

## Import Any Python Package with Requester
Expand All @@ -407,14 +407,14 @@ Here are a couple of no-brainers:
### Pip3 Quickstart
If you don't have `virtualenv` or you're not comfortable using it, the quick solution is to install Python 3, which will install `pip3` and `python3` executables. Run `which pip3` to make sure you've done this.

>Note: Sublime Text runs Python 3.3, and there are some packages, such as `browsercookie`, that can only be imported by Sublime Text if they are downloaded with `pip3.3`. The best way to download Python 3.3 is with [pyenv](https://gist.github.com/Bouke/11261620), but this can be a bit of pain. My advide: don't bother unless you really want to use one of these packages.
Then run `pip3 install requests-oauthlib`, `pip3 install requests-toolbelt`, `pip3 install graphql-py`, and so on for whatever packages you'd like to use with Requester.

Finally, run `pip3 show requests-oauthlib`, and look for the __LOCATION__ field in the output. Now you know where pip is installing your packages.

Use this path as your `packages_path` setting in Requester's settings file. To open these settings, look for __Requester: Settings__ in the command palette.

>Note: Sublime Text runs Python 3.3, and there are some packages, such as `browsercookie`, that can only be imported by Sublime Text if they are downloaded with `pip3.3`. The best way to download Python 3.3 is with [pyenv](https://gist.github.com/Bouke/11261620), but this can be a bit of pain. My advide: don't bother unless you really want to use one of these packages.

### OAuth1 and OAuth2
__requests-oauthlib__ makes OAuth1 and OAuth2 a lot easier. Let's say you want explore the Twitter REST API, which uses OAuth1 for authentication. Go to <https://apps.twitter.com/app/new>, create a new application, then go to the __Keys and Access Tokens__ tab for your application. Generate an access token and an access token secret, grab your API key and secret, and pass them to `OAuth1`.
Expand Down

0 comments on commit 854699a

Please sign in to comment.