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

feat: allow use of Observables on subscriptions with generic-sdk #4204

Merged
merged 4 commits into from
Jun 24, 2020
Merged

feat: allow use of Observables on subscriptions with generic-sdk #4204

merged 4 commits into from
Jun 24, 2020

Conversation

onhate
Copy link
Contributor

@onhate onhate commented Jun 10, 2020

Allow the use of Observables on subscription operations for Generic SDK

config:
  usingObservableFrom: "import Observable from 'zen-observable';"
OR
  usingObservableFrom: "import { Observable } from 'rxjs';"

documents: '../**/document.graphql'
generates:
  ./index.ts:
    schema: '../**/schema.graphql'
    plugins:
      - typescript
      - typescript-operations
      - typescript-generic-sdk

Generates a method that returns an Observable instead of a Promise.

...
import Observable from 'zen-observable';
OR
import { Observable } from 'rxjs';
...

export function getSdk<C>(requester: Requester<C>) {
  return {
     ...
    onX(variables?: OnXVariables, options?: C): Observable<OnXSubscription> {
      return requester<OnXSubscription, OnXVariables>(OnXDocument, variables, options);
    }
  };
}

When this config is enabled, the Requester response will change to this:

export type Requester<C= {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | Observable<R>

This is an optional configuration that does not breaks existing setup.

@onhate onhate changed the title feat: allow use of Observables on subscriptions with generic-sdk DRAFT: feat: allow use of Observables on subscriptions with generic-sdk Jun 10, 2020
@onhate
Copy link
Contributor Author

onhate commented Jun 10, 2020

it's still not clear for me how to extend the Requester type.

I'm thinking in adding an optional subscribe method to it and the invoke would be something like

requester<>.subscribe(...)

instead of

requester<>(...)

or

export type Requester<C= {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | Observable<R>

@onhate onhate changed the title DRAFT: feat: allow use of Observables on subscriptions with generic-sdk feat: allow use of Observables on subscriptions with generic-sdk Jun 10, 2020
@onhate
Copy link
Contributor Author

onhate commented Jun 10, 2020

@dotansimha WDYT?

@dotansimha
Copy link
Owner

This is a great idea! Thank you for implementing this, @onhate !
I'll review it soon :)

@onhate
Copy link
Contributor Author

onhate commented Jun 15, 2020

hi @dotansimha and @ardatan, did you have the chance to review this PR?

@onhate
Copy link
Contributor Author

onhate commented Jun 17, 2020

@dotansimha, @ardatan ?

@onhate
Copy link
Contributor Author

onhate commented Jun 18, 2020

guys, any chance we could move this PR this week? @dotansimha @ardatan

@onhate
Copy link
Contributor Author

onhate commented Jun 20, 2020

@dotansimha @ardatan are you there?

@ardatan
Copy link
Collaborator

ardatan commented Jun 20, 2020

@onhate Thanks for the PR! LGTM!

@henrique-rotava
Copy link

Great implementation @onhate ! I was looking for some solution like that.

@dotansimha
Copy link
Owner

Thanks @onhate , it looks good.
Can you please move RawGenericSdkPluginConfig to a new file, called config.ts? It will simplify the process of creating auto-generated docs for that plugins.

Thank you :)

@onhate
Copy link
Contributor Author

onhate commented Jun 22, 2020

@dotansimha done!

@onhate
Copy link
Contributor Author

onhate commented Jun 24, 2020

@dotansimha, @ardatan is there any plan to make this available on npm soon?

@ardatan ardatan merged commit 79d1efa into dotansimha:master Jun 24, 2020
@theguild-bot
Copy link
Collaborator

The latest changes of this PR are available as alpha in npm: 1.15.5-alpha-79d1efaa.0

Quickly update your package.json by running:

npx match-version @graphql-codegen 1.15.5-alpha-79d1efaa.0

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

Successfully merging this pull request may close these issues.

None yet

5 participants