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

Allow multiple queries to be added together ( via a Monoid instance or similar? ) #793

Open
silky opened this issue Nov 11, 2022 · 3 comments

Comments

@silky
Copy link

silky commented Nov 11, 2022

( thanks for the work on this amazing library! I love it :) )

Suppose I have two insert queries:

mutation InsertA ($id: uuid!) {
  insert_a_one (object: {id: $id}) { id }
}
mutation InsertB ($id: uuid!) {
  insert_b_one (object: {id: $id}) { id }
}

then, I believe it's legal to combine this into a single query that inserts these both at once:

mutation InsertBoth ($id_a: uuid!, $id_b: uuid!) {
  insert_a_one (object: {id: $id_a}) { id }
  insert_b_one (object: {id: $id_b}) { id }
}

it'd be really neat if I could perform this combination in haskell from the two independent queries, by say combining the two args:

insertBothArgs = insertAArgs <> insertBArgs 

this isn't currently possible, right? would be extremely handy! :)

@nalchevanidze
Copy link
Member

nalchevanidze commented Nov 12, 2022

thanks silky, i assume you mean in context of morpheus client? don't you? could you provide more detailed example?

@nalchevanidze
Copy link
Member

FYI: did you tried <:> from Data.Morpheus.Internal.Ext? regular semigroup is not applicable for this operation since it may fail if queries are not compatible

@silky
Copy link
Author

silky commented Nov 12, 2022

thanks @nalchevanidze

yes, indeed i'm talking about the client

I'm not sure <:> does what i want (i just tried; it can't combine my a and b, below)

here's my example.

i have the following queries:

defineLocalTypesInline "db.graphql"
  [raw|
mutation InsertA ( $id: Int!  , $name: String!) {
  insert_a_one(object: { id: $id , name: $name }) {
    id
  }
}
  |]

defineLocalTypesInline "db.graphql"
  [raw|
mutation InsertB ( $id: Int!  , $name: String!  , $age: Int!) {
  insert_b_one (object: { id: $id , name: $name , age: $age }) {
    id
  }
}
  |]

defining inserts into two tables:

a
 - id
 - name

b
 - id
 - name
 - age

i can construct an InsertAArgs and an InsertBArgs:

a = InsertAArgs 1 "hello"
b = InsertBArgs 2 "helen" 50

then i can insert them independently like so:

fetch @InsertA config a
fetch @InsertB config b

this all works perfectly and is valid haskell.

but, it's just as a valid to insert them both in one query:

defineLocalTypesInline "db.graphql"
  [raw|
mutation InsertAB
  ( $a_id: uuid!, $a_name: String!,
  , $b_id: uuid!, $b_name: String!, $b_age: Int!
  ) {
  insert_a_one(object: { id: $a_id , name: $a_name }) {
    id
  }
  insert_b_one (object: { id: $b_id , name: $b_name , age: $b_age }) {
    id
  }
}
  |]

i guess what i'm asking is if there is a way to always be able to combine two queries in this way, i.e. so i don't have to manually define InsertAB myself) to produce this unified query (note that this would never be incompatible, as i think it's always possible to combine queries in this way)

i.e. yielding an ideal query of the form

fetch @(InsertA :<> InsertB) config (a <> b)

having gone through the process of writing this all out, it feels to me that maybe this would be just not possible; but i'm not certain; and it would be extremely convenient :)

hope this makes sense!

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

No branches or pull requests

2 participants