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: add AsyncIterable support to fetcher function #1724

Merged
merged 6 commits into from
Dec 5, 2020

Conversation

n1ru4l
Copy link
Collaborator

@n1ru4l n1ru4l commented Dec 1, 2020

No description provided.

@codecov
Copy link

codecov bot commented Dec 1, 2020

Codecov Report

Merging #1724 (2156060) into main (0c5785c) will decrease coverage by 0.30%.
The diff coverage is 32.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1724      +/-   ##
==========================================
- Coverage   67.07%   66.77%   -0.31%     
==========================================
  Files          87       87              
  Lines        4839     4873      +34     
  Branches     1330     1343      +13     
==========================================
+ Hits         3246     3254       +8     
- Misses       1359     1383      +24     
- Partials      234      236       +2     
Impacted Files Coverage Δ
packages/graphiql/src/components/GraphiQL.tsx 57.84% <32.50%> (-2.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c5785c...2156060. Read the comment docs.

} else if (isAsyncIterable(fetch)) {
(async () => {
try {
for await (const result of fetch) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should be all fine given that we don't support IE11 :)

Copy link
Member

Choose a reason for hiding this comment

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

all of this will be passed through browserslist and babel anyways

also to note re: for await of: afaik this will gaurantee serial execution, this is desired?

@n1ru4l n1ru4l marked this pull request as ready for review December 1, 2020 10:08
@acao
Copy link
Member

acao commented Dec 1, 2020

this looks great! im just wondering how we can best demonstrate this feature at this point? maybe just a quick entry in the readme showing the two alternatives to fetcher?

or a docs/fetcher.md for some more in depth examples? been thinking of this as a way to break down the super long readme and to prepare for a docsite

eventually, i’d like to have a docsite where we could show many graphiql props permutations. we could use storybook or play room or use some other strategy.

@n1ru4l
Copy link
Collaborator Author

n1ru4l commented Dec 1, 2020

For starters, we could just add another example to the readme directly calls graphql/subscribe imported from the graphql package. I can take care of that. I also wish there was like an export from graphql that handles both execute/subscribe, so we don't have to write wrapper logic 👀

@n1ru4l
Copy link
Collaborator Author

n1ru4l commented Dec 1, 2020

Right now I tested the implementation by starting the GraphiQL dev server and pasting the following into packages\graphiql\resources\index.html.ejs :D

<script>
      // graphql-js relies on the global process object without feature detection :(
      window.process = globalThis.process = {
        env: {
          NODE_ENV: "development"
        }
      }
    </script>
    <script type="module">
      import * as graphql from "https://unpkg.com/[email protected]/index.mjs?module"

      const schema = new graphql.GraphQLSchema({
        mutation: new graphql.GraphQLObjectType({
          name: "Mutation",
          fields: {
            ping: {
              type: graphql.GraphQLBoolean,
              resolve: () => true
            }
          }
        }),
        query: new graphql.GraphQLObjectType({
          name: "Query",
          fields: {
            ping: {
              type: graphql.GraphQLBoolean,
              resolve: () => true
            }
          }
        }),
        subscription: new graphql.GraphQLObjectType({
          name: "Subscription",
          fields: {
            count: {
              type: graphql.GraphQLInt,
              subscribe: async function * () {
                for (let counter = 0; counter < 10; counter++) {
                  yield { count: counter };
                  await new Promise(res => setTimeout(res, 1000))
                }
              }
            }
          }
        }),
      });

      const fetcher = (graphQLParams) => {
        const document = graphql.parse(graphQLParams.query);
        const operation = graphql.getOperationAST(document, graphQLParams.operationName);
        if (operation.operation === "subscription") {
          // AsyncIterable
          return graphql.subscribe(
            schema,
            document,
            {},
            {},
            graphQLParams.variables,
            graphQLParams.operationName
          )
        }

        // Promise or AsyncIterable
        return graphql.execute(
          schema,
          document,
          {},
          {},
          graphQLParams.variables,
            graphQLParams.operationName
        )
      }

      window.schemaFetcherContext = {
        schema,
        fetcher
      }

    </script>

You will also have to adjust packages\graphiql\resources\renderExample.js to use window.schemaFetcherContext.fetcher

@acao
Copy link
Member

acao commented Dec 1, 2020

thats nice to see it working, but we will need to keep that example as is for now.

lets just keep it to that readme entry for now, and we will come up with a way to show more detailed examples soon!

@n1ru4l
Copy link
Collaborator Author

n1ru4l commented Dec 1, 2020

Yeah, I just want some way for you to properly test before shipping a broken GraphiQL :D I am currently working on the readme section. In the meantime, I also fixed some issues that I could identify with the custom fetcher.

@n1ru4l n1ru4l force-pushed the feat-async-iterable-fetcher branch from 528ec05 to e18ba6c Compare December 2, 2020 10:53
@acao
Copy link
Member

acao commented Dec 5, 2020

@n1ru4l perhaps for now let's implement it in one of the examples, and then we can cross-reference to that from the readme?

@acao
Copy link
Member

acao commented Dec 5, 2020

so in summary, the two last steps:

  1. just one unit test
  2. implement an example here, and then link to it from the packages/graphiql/Readme

@acao acao merged commit a568af3 into graphql:main Dec 5, 2020
@acao
Copy link
Member

acao commented Dec 8, 2020

now available in [email protected]!

@n1ru4l n1ru4l deleted the feat-async-iterable-fetcher branch December 12, 2020 10:40
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.

2 participants