1

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.

Batman
  • 5,563
  • 18
  • 79
  • 155
  • I'm not sure what you're asking here. You've provided two different _mutations_, not fragments. But given it's the same mutation just with different field selections, i'd just always use the second one since it's a superset of the first. – Andrew Ingram Oct 25 '20 at 12:23
  • I have the similar issue I've one pagination container which has two fragments: `...EntityRow_entity ...EntityCard_entity` That fragments completely define the same set of fields. I know that we can import two types and use them, but actually it's a code duplication. – BILL Feb 05 '21 at 12:02

0 Answers0