oliyh / re-graph

A graphql client for clojurescript and clojure
461 stars 40 forks source link

fragments not supported in query #94

Closed hipitihop closed 2 years ago

hipitihop commented 2 years ago

The following query is valid in the Hasura console, however when executed by re-graph, it naively prepends query to the start, making it invalid.

see: https://github.com/oliyh/re-graph/blob/4553d11f67033383c092a967854efde8bb48f04a/src/re_graph/core.cljc#L66

Using 0.2.0

fragment brief_fields on briefly_brief {
    __typename
    id
    schedule_code
    client_desc
    product_desc
    time_saved
  }

 query similar_briefs($sch_code: String!, $cli_code: String!, $pro_code: String!) {

    briefs_for_schedule: briefly_brief(
      where: {schedule_code: {_eq: $sch_code}})
    {...brief_fields}

    briefs_for_client_product: briefly_brief(
      where: {
        _and: [
          {client_code: {_eq: $cli_code}},
          {product_code: {_eq: $pro_code}},
          {schedule_code: {_neq: $sch_code}}
        ]},
      order_by: {time_saved: desc},
      limit: 10)
    {...brief_fields}

    briefs_for_client: briefly_brief(
      where: {
        _and: [
          {client_code: {_eq: $cli_code}},
          {product_code: {_neq: $pro_code}},
          {schedule_code: {_neq: $sch_code}}
        ]},
      order_by: {time_saved: desc},
      limit: 10)
    {...brief_fields}
  }
oliyh commented 2 years ago

Hello,

Yep, should be a relatively simple fix, to do nothing if the first word is a GraphQL keyword.

oliyh commented 2 years ago

By the way the workaround is to declare the query first and the fragment second (I am pretty sure it is valid to do that)

hipitihop commented 2 years ago

By the way the workaround is to declare the query first and the fragment second (I am pretty sure it is valid to do that)

I'll give it a go and let you know

hipitihop commented 2 years ago

@oliyh Looks like that's not supported. It causes error: reference to undefined fragment "brief_fields"

oliyh commented 2 years ago

Sad times, ok will work on a fix

oliyh commented 2 years ago

In the graphql spec the example given shows the query defined before the fragment: https://spec.graphql.org/June2018/#sec-Language.Fragments

So I think your server implementation should support it, that said it's still a relatively simple fix so I can still support it in re-graph.

hipitihop commented 2 years ago

@oliyh Many thanks for the fix. I have raised an issue with Hasura pointing this out.

oliyh commented 2 years ago

The fix is in the latest snapshot on clojars