How to reduce duplication in relay fragments

297 Views Asked by At

I have components that are almost identical but target different graphql entities, im trying to figure out if there's a way I can reduce the duplication of code. For example I have these two fragments:

const PROCESS_REQUEST_MUTATION_A = graphql`
  mutation EntityA($input: ProcessRequestInput!) {
    processRequest(input: $input) {
      errors
      currentRequest {
        id
        state
        items {
          edges {
            item {
              id
            }
          }
        }
        events {
          ...RequestHistory_events
        }
      }
      nextRequest {
        id
        primaryType
      }
    }
  }
`;

In another component which is essentially a duplicate, I have:

const PROCESS_REQUEST_MUTATION_ = graphql`
  mutation EntityB($input: ProcessRequestInput!) {
    processRequest(input: $input) {
      errors
      currentRequest {
        id
        state
        items {
          edges {
            item {
              id
              ...EntityB_item
            }
          }
        }
        events {
          ...RequestHistory_events
        }
      }
      nextRequest {
        id
        primaryType
      }
    }
  }
`;

I'd like to have only one component and somehow simplify the fragment to handle this without so much duplication.

0

There are 0 best solutions below