Search code examples
graphqlrelayjs

(Relay) What is the correct syntax to re-run a query based on the result of that query


Lets say I have a query that gets an advisors. That query returns a list of advisor ids that are connected to that advisor. Now I want to rerun the query that got the advisor in the first place, with the n number of ids, and get those advisors. I only need to do this once, and not recursively.

const query = graphql`
  query AdvisorProfileQuery($id: ID!) {
    advisor: node(id: $id) {
      ... on Advisor {
        name
        assignments (first: 100) {
          edges {
            node {
              id <---- i want these ids and plug them back into the query to just get "name" of the list of ids
            }
          }
        }
      }
    }
  }
`;

const QueryRenderer = LoadingQueryRenderer(AdvisorProfile, query);
export default ({ i18n }) => {
  const { id } = useParams();
  return (
    <>
      <QueryRenderer params={{ id: id }} />
    </>
  );
};

Solution

  • Answer was to create a fragment and splat it in...

    Parent component

    const query = graphql`
      query AdvisorProfileQuery($id: ID!) {
        advisor: node(id: $id) {
          ... on Advisor {
            ... BottomCardTable_advisor
            name
          }
        }
      }
    `;
    
    const QueryRenderer = LoadingQueryRenderer(AdvisorProfile, query);
    export default ({ i18n }) => {
      const { id } = useParams();
      return (
        <>
          <QueryRenderer params={{ id: id }} />
        </>
      );
    };
    

    on child component

    export default createFragmentContainer(TableComponent, graphql`
      fragment BottomCardTable_advisor on Advisor {
        assignments: assignments (first: 100) {
          edges {
            node {
              id
              primaryName
              primaryEmail
              assignedOn
              type
            }
          }
        }
      }
    `);