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 batching support with batchRequests #285

Merged
merged 3 commits into from
Oct 12, 2021

Conversation

GeoffreyHervet
Copy link
Contributor

@GeoffreyHervet GeoffreyHervet commented Sep 7, 2021

Add a new function to support GraphQL batching
closes #13
closes #75


import { batchRequests } from '../src'
;(async function () {
  const endpoint = 'https://api.spacex.land/graphql/'

  const query1 = /* GraphQL */ `
    query ($id: ID!) {
      capsule(id: $id) {
        id
        landings
      }
    }
  `
  const variables1 = {
    id: 'C105',
  }

  interface TData1 {
    data: { capsule: { id: string; landings: number } }
  }

  const query2 = /* GraphQL */ `
    {
      rockets(limit: 10) {
        active
      }
    }
  `

  interface TData2 {
    data: { rockets: { active: boolean }[] }
  }

  const query3 = /* GraphQL */ `
    query ($id: ID!) {
      core(id: $id) {
        id
        block
        original_launch
      }
    }
  `

  const variables3 = {
    id: 'B1015',
  }

  interface TData3 {
    data: { core: { id: string; block: number; original_launch: string } }
  }

  const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [
    { document: query1, variables: variables1 },
    { document: query2 },
    { document: query3, variables: variables3 },
  ])
  console.log(data[0].data.capsule)
  console.log(data[1].data.rockets)
  console.log(data[2].data.core)
  // console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))

@GeoffreyHervet GeoffreyHervet force-pushed the feat/batching branch 2 times, most recently from 997d867 to d3ce170 Compare September 7, 2021 12:34
@GeoffreyHervet GeoffreyHervet changed the title GraphQL bathing support with batchRequests feat: add batching support with batchRequests Sep 7, 2021
@GeoffreyHervet GeoffreyHervet force-pushed the feat/batching branch 2 times, most recently from ee1212f to 8eccb9f Compare September 7, 2021 13:04
Copy link
Member

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

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

Could you try adding a test or two

@GeoffreyHervet GeoffreyHervet marked this pull request as draft September 8, 2021 08:31
@sswayney
Copy link

If this PR could be merged I would be very happy. This feature is all that is missing to give me the go-ahead to use this lib for my project. @GeoffreyHervet is there anything you need?

@GeoffreyHervet GeoffreyHervet marked this pull request as ready for review October 11, 2021 09:54
@GeoffreyHervet
Copy link
Contributor Author

@sswayney Thanks, I just had to add tests as suggested @jasonkuhrt

Copy link
Member

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

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

LGTM

@jasonkuhrt jasonkuhrt merged commit 2d92782 into graffle-js:master Oct 12, 2021
@jasonkuhrt
Copy link
Member

@GeoffreyHervet Ah, one thing, docs in REDAME would be great thanks!

@GeoffreyHervet
Copy link
Contributor Author

@jasonkuhrt I'll add it !

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.

Request batching support Support for query batching
4 participants