[GH-ISSUE #4] Single query, multiple operations #1

Closed
opened 2026-03-03 00:02:38 +03:00 by kerem · 11 comments
Owner

Originally created by @bebraw on GitHub (Mar 11, 2019).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/4

I have a case roughly like this:

query SpeakerTweetTemplateQuery($conferenceId: ID!, $contactName: String!) {
  contact(contactName: $contactName, conferenceId: $conferenceId) {
    name
  }
  conference(id: $conferenceId) {
    name
  }
}

The point is that I would like to execute multiple operations within a single query. As far as I can see, the current API isn't flexible enough for this purpose.

I can work around this by performing multiple separate requests. Any better ideas? Should I try to PR a generalization like this?

Originally created by @bebraw on GitHub (Mar 11, 2019). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/4 I have a case roughly like this: ```graphql query SpeakerTweetTemplateQuery($conferenceId: ID!, $contactName: String!) { contact(contactName: $contactName, conferenceId: $conferenceId) { name } conference(id: $conferenceId) { name } } ``` The point is that I would like to execute multiple operations within a single query. As far as I can see, the current API isn't flexible enough for this purpose. I can work around this by performing multiple separate requests. Any better ideas? Should I try to PR a generalization like this?
kerem closed this issue 2026-03-03 00:02:38 +03:00
Author
Owner

@atulmy commented on GitHub (Mar 11, 2019):

@bebraw yes, current implementation is quite basic. If we have this feature baked in, it'd definitely help.

There is this issue open to support params on nested fields which seems like dependency for this use case as well.

If you can send a PR, that would be awesome. Don't forget to add you name under Authors section of readme. Looking forward to it.

<!-- gh-comment-id:471607859 --> @atulmy commented on GitHub (Mar 11, 2019): @bebraw yes, current implementation is quite basic. If we have this feature baked in, it'd definitely help. There is [this issue](https://github.com/atulmy/gql-query-builder/issues/3) open to support params on nested fields which seems like dependency for this use case as well. If you can send a PR, that would be awesome. Don't forget to add you name under [Authors](https://github.com/atulmy/gql-query-builder#authors) section of readme. Looking forward to it.
Author
Owner

@bebraw commented on GitHub (Mar 11, 2019):

@atulmy Ok, I'll give it a go. Given the feature might couples operation with a specific field, likely the API shape has to change. Would a major version be ok? I can likely support the old format while giving a deprecation warning.

<!-- gh-comment-id:471613702 --> @bebraw commented on GitHub (Mar 11, 2019): @atulmy Ok, I'll give it a go. Given the feature might couples operation with a specific field, likely the API shape has to change. Would a major version be ok? I can likely support the old format while giving a deprecation warning.
Author
Owner

@atulmy commented on GitHub (Mar 11, 2019):

Would a major version be ok? I can likely support the old format while giving a deprecation warning.

Sure.

<!-- gh-comment-id:471626894 --> @atulmy commented on GitHub (Mar 11, 2019): > Would a major version be ok? I can likely support the old format while giving a deprecation warning. Sure.
Author
Owner

@bebraw commented on GitHub (Mar 12, 2019):

Here's are my first proposals for the API:

1.

import queryBuilder from 'gql-query-builder'

const query = queryBuilder([
  {
    type: 'query',
    operation: 'thoughts',
    fields: ['id', 'name', 'thought']
  },
  {
    type: 'query',
    operation: 'prayers',
    fields: ['id', 'name', 'prayer']
  }
])

console.log(query)

// Output
query {
  thoughts {
    id,
    name,
    thought
  }
  prayers {
    id,
    name,
    prayer
  }
}

2.

import queryBuilder from 'gql-query-builder'

const query = queryBuilder.query([{
    operation: 'thoughts',
    fields: ['id', 'name', 'thought']
  },
  {
    operation: 'prayers',
    fields: ['id', 'name', 'prayer']
  }
])

console.log(query)

// Output
query {
  thoughts {
    id,
    name,
    thought
  }
  prayers {
    id,
    name,
    prayer
  }
}

Does it make sense to generate a query that has both mutations and queries in it? If not, then I would go with 2. as then you get .query and .mutation and cannot end up with a conflicting state.

Let me know what you prefer and I'll implement the variant.

<!-- gh-comment-id:472064850 --> @bebraw commented on GitHub (Mar 12, 2019): Here's are my first proposals for the API: **1.** ```javascript import queryBuilder from 'gql-query-builder' const query = queryBuilder([ { type: 'query', operation: 'thoughts', fields: ['id', 'name', 'thought'] }, { type: 'query', operation: 'prayers', fields: ['id', 'name', 'prayer'] } ]) console.log(query) // Output query { thoughts { id, name, thought } prayers { id, name, prayer } } ``` **2.** ```javascript import queryBuilder from 'gql-query-builder' const query = queryBuilder.query([{ operation: 'thoughts', fields: ['id', 'name', 'thought'] }, { operation: 'prayers', fields: ['id', 'name', 'prayer'] } ]) console.log(query) // Output query { thoughts { id, name, thought } prayers { id, name, prayer } } ``` Does it make sense to generate a query that has both mutations and queries in it? If not, then I would go with 2. as then you get `.query` and `.mutation` and cannot end up with a conflicting state. Let me know what you prefer and I'll implement the variant.
Author
Owner

@atulmy commented on GitHub (Mar 12, 2019):

@bebraw the second one looks more comprehensible.

We may also need to support:
Only object:

queryBuilder.query({ ... })

Array of objects:

queryBuilder.query([ { ... }, { ... } ])

And mutation as well:

queryBuilder.mutation()
<!-- gh-comment-id:472177149 --> @atulmy commented on GitHub (Mar 12, 2019): @bebraw the second one looks more comprehensible. We may also need to support: Only object: ``` queryBuilder.query({ ... }) ``` Array of objects: ``` queryBuilder.query([ { ... }, { ... } ]) ``` And mutation as well: ``` queryBuilder.mutation() ```
Author
Owner

@bebraw commented on GitHub (Mar 12, 2019):

@atulmy Ok, sounds good. I'll do something basic around this. Would you mind if I ported the project to TypeScript while at it?

<!-- gh-comment-id:472178395 --> @bebraw commented on GitHub (Mar 12, 2019): @atulmy Ok, sounds good. I'll do something basic around this. Would you mind if I ported the project to TypeScript while at it?
Author
Owner

@atulmy commented on GitHub (Mar 12, 2019):

port the project to TypeScript

Sure. Sounds exciting. Haven't worked with TypeScript yet, this sounds like a good opportunity.

<!-- gh-comment-id:472179871 --> @atulmy commented on GitHub (Mar 12, 2019): > port the project to TypeScript Sure. Sounds exciting. Haven't worked with TypeScript yet, this sounds like a good opportunity.
Author
Owner

@bebraw commented on GitHub (Mar 13, 2019):

@atulmy You can find my initial PR at #5. It doesn't include the work above, though.

<!-- gh-comment-id:472361873 --> @bebraw commented on GitHub (Mar 13, 2019): @atulmy You can find my initial PR at #5. It doesn't include the work above, though.
Author
Owner

@atulmy commented on GitHub (Mar 13, 2019):

I think we should support following:

const { query, mutation } = require('gql-query-builder')

query()

mutation()
const queryBuilder = require('gql-query-builder')

queryBuilder.query()

queryBuilder.mutation()
<!-- gh-comment-id:472365611 --> @atulmy commented on GitHub (Mar 13, 2019): I think we should support following: ``` const { query, mutation } = require('gql-query-builder') query() mutation() ``` ``` const queryBuilder = require('gql-query-builder') queryBuilder.query() queryBuilder.mutation() ```
Author
Owner

@bebraw commented on GitHub (Mar 13, 2019):

Ok, cool. I'll prepare a PR along those lines. It might go to tomorrow. Let's see.

<!-- gh-comment-id:472366765 --> @bebraw commented on GitHub (Mar 13, 2019): Ok, cool. I'll prepare a PR along those lines. It might go to tomorrow. Let's see.
Author
Owner

@atulmy commented on GitHub (Mar 13, 2019):

Sure, we can schedule an npm release once updated API is in good shape.

<!-- gh-comment-id:472368050 --> @atulmy commented on GitHub (Mar 13, 2019): Sure, we can schedule an `npm` release once updated API is in good shape.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gql-query-builder#1
No description provided.