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

Variable and Combined Query example. #11

Open
BartoszLobejko opened this issue Jun 4, 2019 · 0 comments
Open

Variable and Combined Query example. #11

BartoszLobejko opened this issue Jun 4, 2019 · 0 comments

Comments

@BartoszLobejko
Copy link

BartoszLobejko commented Jun 4, 2019

Hey,
I was struggling to combine few queries together to do more advanced filtering and I found that Directives doesn't work. So I came up with this simple solution to combine tax and meta queries but only include them when needed. Hopefully, that improves performance.
(this example also include pagination and fragments)

const ACCOMMODATION_QUERY = (
  first,
  after,
  locationFilter,
  typeFilter,
  ifLocation,
  ifType,
) => gql`
    query AccommodationsQuery(
      $first: Int = ${first}
      $after: String = ${after}
      ${ifLocation ? `$locationFilter: String = "${locationFilter}"` : ``}
      ${ifType ? `$typeFilter: [String] = ["${typeFilter.join('","')}"]` : ``}
    ) {
    accommodations(
      first: $first
      after: $after
      where: {
        orderby: { field: MODIFIED, order: DESC }
        ${
          ifLocation
            ? `metaQuery: {
          metaArray: [{ key: "location", value: $locationFilter, compare: LIKE }]
        }`
            : ``
        }
        ${
          ifType
            ? `taxQuery: {
          taxArray: [
            {
              terms: $typeFilter
              taxonomy: ACCOMMODATIONTYPE
              operator: IN
              field: SLUG
            }
          ]
        }`
            : ``
        }
      }
    ) {
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        cursor
        node {
          date
          modified
          slug
          title
          accommodationDetails {
            ...AccommodationListDetails
          }
        }
      }
    }
  }
  ${AccommodationListDetailsFragment}
`;

And then you can skip variables in render

<Query
              query={ACCOMMODATION_QUERY(
                first,
                after,
                locationFilter,
                typeFilter,
                ifLocation,
                ifType,
              )}
              variables={{}}
            >
...
</Query>

Let me know what do you think.

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

1 participant