Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Add a new GraphiQL fetcher #96

Merged
merged 3 commits into from
Apr 11, 2017
Merged

Add a new GraphiQL fetcher #96

merged 3 commits into from
Apr 11, 2017

Conversation

Urigo
Copy link
Contributor

@Urigo Urigo commented Mar 11, 2017

Add a new GraphiQL fetcher that overrides the default one so we can easily add GraphQL subscriptions support to GraphiQL

subscriptionsClient.unsubscribe(activeSubscriptionId);
}

if (subscriptionsClient && graphQLParams.query.startsWith('subscription')) {
Copy link
Contributor

@NeoPhi NeoPhi Mar 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem like a brittle way to test if it is a subscription.

Copy link
Contributor Author

@Urigo Urigo Mar 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NeoPhi Agreed.
I thought to try to do it without adding a dependency to grpahql.js...
(GrpahiQL is dependent on it but doesn't expose it)
But can definitely change that. what do you think would be better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @NeoPhi. Instead think you can do pretty much what this does:

https://github.com/apollographql/graphql-anywhere/pull/44/files#diff-52bc6e1910f2b72f10d27006c255168fR55

If you don't want to add the graphql-js dependency, you can write that in the comment there and just use any for all the types. But you should definitely respect the operationName as well. GraphiQL is one of the few cases where it's actually useful for people to have many definitions in a document but only execute one. This GraphQL fetcher should support that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GraphiQL exposes string to the fetcher, and not GraphQL parsed object (DocumentNode). So we have two options:

  1. Add dependency for graphql, parse the string from GraphiQL and use it to check for subscriptions operations.
  2. Leave as is - maybe look for a substring instead only startsWith, or maybe other logic you think is better.

@NeoPhi @helfer what do you think is the preferred solution? or maybe I'm missing a third one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GraphiQL already comes with graphql. Is there a way to get it from that bundle? If not, I would suggest this:

  1. if operationName is set, look for a match for subscription <operationName>.
  2. if operationName is not set, check if subscription is present somewhere before the first { in the string.

I think we should only send it over the subscription transport if we're very confident that a subscription should happen, because the error messages from subscriptions aren't as easy to understand as the ones that are returned from the normal graphql executor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is for GraphiQL I don't think the added weight of pulling in GraphQL is too much of a concern.

let activeSubscriptionId: number | null = null;

return (graphQLParams: any) => {
if (subscriptionsClient && activeSubscriptionId !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it would require GraphiQL changes to support stopping a subscription, having multiple active subscriptions would be ideal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm because this is a temporary change until this PR would be merged, it's hard for me to think how keeping multiple subscriptions would look nice in the current GraphiQL UI.
Think that you would have a new subscription but would still keep getting new results of the previous subscriptions. probably would be confusing.
But maybe it would be an interesting thing to think about a better UI for a deeper change in GraphiQL itself.
Do you still think we should leave the previous subscriptions active or leave it like it is now?

Copy link
Contributor

@helfer helfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, but we should make sure we have a solid way of checking if the operation under operationName is a subscription. See the link to get an idea for how to do it (although, use the operationName) instead of a for loop, if it exists.

Some tests for this might be good as well, to make sure we support the main use-cases.

subscriptionsClient.unsubscribe(activeSubscriptionId);
}

if (subscriptionsClient && graphQLParams.query.startsWith('subscription')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @NeoPhi. Instead think you can do pretty much what this does:

https://github.com/apollographql/graphql-anywhere/pull/44/files#diff-52bc6e1910f2b72f10d27006c255168fR55

If you don't want to add the graphql-js dependency, you can write that in the comment there and just use any for all the types. But you should definitely respect the operationName as well. GraphiQL is one of the few cases where it's actually useful for people to have many definitions in a document but only execute one. This GraphQL fetcher should support that.

@Urigo Urigo force-pushed the feat/graphiql-fetcher branch from 8d12840 to 0e97395 Compare March 26, 2017 12:15
@Urigo
Copy link
Contributor Author

Urigo commented Mar 26, 2017

@helfer @NeoPhi
I moved the fetcher implementation into a new package: https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher
This way, I added graphql dependency (and use it for parsing) only for the fetcher package (which loaded only for GraphiQL).

@Urigo Urigo force-pushed the feat/graphiql-fetcher branch from 5fdf14a to 769220a Compare March 26, 2017 17:06
@Urigo Urigo force-pushed the feat/graphiql-fetcher branch from 769220a to 115e4fa Compare March 26, 2017 17:08
@helfer
Copy link
Contributor

helfer commented Apr 11, 2017

Looks good to me!

@helfer helfer merged commit 42156e2 into master Apr 11, 2017
@helfer helfer deleted the feat/graphiql-fetcher branch April 11, 2017 19:41
martijnwalraven pushed a commit that referenced this pull request Oct 17, 2017
use single quotes on import filename
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants