Skip to content

Allow multiple operations to be executed in one call #806

Closed

Description

@bbakerman

Today you can only execute 1 operation. The spec says you can only do that but I feel its an unnecessary restriction.

I would like graphql-java to introduce a experimental mode where you can provide a list of operation names to execute

So given this valid query

query Orders {
      orders {
        id
        items {
          name
       }
    }
}

query Billing {
    billing {
        dueDate
        amount
    }
}

We could execute both operations by specify the operation names in the ExecutionInput

    ExecutionInput input = newInput().operations(toList("A","B")).build();

We would retain the current single operation name for backwards compatiblity.

If you did this then you need to get a combined execution result. The names of the operations would be the top level values

   executionResult == {
          "A" : {
              "orders" : [ ...]
          },
         "B" : {
              "billing" : {
                 ...
              }
         }
   }

The errors would be a combined list of executing operation A and then operation B

The operations would be executed in the order specified in the input list

Looking at the scope of changes (and spec enhancement) I think this is a good candidate for some library extension.

This is somewhat inspired from the Sangria implementation here : http://sangria-graphql.org/learn/#batch-executor

Activity

  1. bbakerman commented on Oct 28, 2017

    @bbakerman
    MemberAuthor

    This is an example test that might pass with this mechanism

        GraphQL graphQL = newGraphQL(starWarsSchema).build()
    
        def query = '''
            query A {
                hero {
                    name
                }
            }    
    
            query B {
                human(id:"1000") {
                    name
                }
            }    
        '''
        ExecutionInput input = newExecutionInput().operationNames(["A","B"]).query(query).build()
    
        def er = graphQL.execute(input)
    
        then:
        er.errors.size() == 0
        er.data["A"] == [hero: [name: "R2-D2"]]
        er.data["B"] == [human: [name: "Luke"]]
    
  2. bbakerman commented on Nov 2, 2017

    @bbakerman
    MemberAuthor

    I spoke to the lead of Sangria about there implementation and a couple of thigns stood out

    First off the combined batching and @export as a featue.

    When they have say 3 operations. A, B,C they applied a dependency resolution process to decide the order.

    if they got a cyclic dep thet refuse the whole operation.

    Otherwise thet worked out that it sould be B,C,A or A,C,B based on @exported variables

    They streams the results out the other side.

    This allows for an order of say B,C,A and B can finish first as an execution result, then C and then A.

    This allows you to send back B as results, and then C and then A. In order words the story then comments then likes say??

  3. alexgenco commented on Dec 21, 2018

    @alexgenco

    Just wanted to bump this thread. Any reason not to revive this? We definitely have use cases for performing multiple operations in a single request.

  4. bbakerman commented on Jan 8, 2019

    @bbakerman
    MemberAuthor

    We definitely have use cases for performing multiple operations in a single request.

    @alexgenco - can you expand your use cases.

    As for thoughts on this the real challenge is the "protocol" for returning results. Should they be combined? Should they feed into each other via "variables" and hence need dependency resolution?
    Should the results be "streamed" much like subscription such that a client gets operation A, then B then C?
    Should they just assume the top ones are mutations and the last is a query?

    Really its much of the same concerns I had on graphql/graphql-spec#375. I originally proposed this RFC but have not chased it up hard to try to get it in the spec as its a slow process. I am not sure that there will be a "spec" compliant "protocol" to be honest

    This is the blocking design challenge to me.

  5. alexgenco commented on Jan 8, 2019

    @alexgenco

    can you expand your use cases.

    We don't have an immediate use case for just executing multiple operations, except if it would make it easier for us to hack together an ad-hoc way to chain mutations with variables (something like the @export proposal). Also, we don't need to stream downstream responses, we would be ok with just waiting for all operations to finish to respond.

    Bascially, we have designed our API in favor of small, composable mutations and queries, but clients are hesitant to eat the cost of 2-3 HTTP requests to get what they want.

    Here's an example use case with actual mutations from our API:

    mutation Tokenize(input: TokenizeCreditCardInput!) {
      tokenizeCreditCard(input: $input) {
        paymentMethod {
          id @export(as: "singleUseId")
        }
      }
    }
    
    mutation Vault {
      vaultPaymentMethod(input: { paymentMethodId: $singleUseId }) {
        paymentMethod {
          id @export(as: "vaultedId")
        }
      }
    }
    
    mutation Charge {
      chargePaymentMethod(input: { paymentMethodId: $vaultedId }) {
        transaction {
          status
        }
      }
    }

    Basically, you tokenize a credit card (exchange raw details for a single-use token), then use that single-use token to permanently store the payment method in the merchant's vault, then use the vaulted payment method ID to charge the payment method.

    We support a large number of payment method types (credit card, apple pay, google pay, etc.), and would prefer to not have to create a mutation for each of tokenize, vault, and charge for each payment method in order to limit the surface area of our API.

  6. andimarek commented on Jul 19, 2019

    @andimarek
    Member

    closing this for now as multiple queries are not supported by the spec at the moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions