0

I did not find anything about it in the Docs, but for me it seems to be a very common task, so is there a possibility/syntax or a workaround to achieve the following?

My GraphQL query looks something like this:

query customerPersons(
  $pagination: PaginationInput!
  $filter: FilterPersonInput
) {
  persons(pagination: $pagination, filter: $filter) {
    totalCount
    data {
      ...PersonsData
    }
  }
}

With apollo and its codegen I get some helper React Hook function calls, which I can use like this:

const { data } = useCustomerPersonsQuery({
  variables: {
    pagination: {
      skip: 0,
      take: 20,
    },
    filter: {
      query: 'peterchen',
      userType: 'Customer'
    }
  });

Is it possible to move the userType: 'Customer' part into the static part of my query? If it would be JavaScript I would say it is assign or merge two objects with the spread operator. Something like:

query customerPersons(
  $pagination: PaginationInput!
  $filter: FilterPersonInput
) {
  persons(pagination: $pagination, filter: { ...$filter, userType: "Customer" }) {
    totalCount
    data {
      ...PersonsData
    }
  }
}
ILCAI
  • 1,164
  • 2
  • 15
  • 35
  • The question I would rather raise is whether it should be apollo graphql to handle this, or just React or Javascript in general. Is your need something implicit in the query or is your need something implicit because of your frontend design? A common pattern I usually see is a function that builds the filters and brings them to graphql apollo rather than the opposite (which is what you're trying to achieve). In your case, the `useCustomerPersonsQuery` should handle `filter.userType = 'Customer'` implicitly, not graphql, neither apollo (imho). – briosheje Jun 17 '22 at 13:12
  • Hm, not sure @briosheje ... I don't want the component's code to be aware of the userType. I just want to reuse the persons resolver, but prepopulate an argument partly to sharpen the focus in that particular way. Of course, I could just make `userType` an argument for it self, then it wouldn't be a problem. I am just curious. BTW, I don't think apollo or react should handle this, because this is just a very specific use case which addresses the gql language and its capabilities. – ILCAI Jun 17 '22 at 13:30

1 Answers1

1

No, GraphQL does not support such syntax in query documents. The closest you could achieve would be creating separate variables for all the fields in FilterPersonInput other than userType.

The two solutions available are:

  • create a separate resolver in your backend that includes this filtering, providing a customers field in the schema next to the persons one. (If you're using unions/interface, this might be an opportunity to choose a more concrete result type as well).
  • create another React hook as a wrapper for the auto-generated one, and add the property to the variable value in there
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Hey! These are two nice solutions. In my case the second one fits even much better. Thank you. – ILCAI Jun 17 '22 at 15:55