lvarayut / relay-fullstack

:point_up::running: Modern Relay Starter Kit - Integrated with Relay, GraphQL, Express, ES6/ES7, JSX, Webpack, Babel, Material Design Lite, and PostCSS
https://lvarayut.github.io/relay-fullstack/
MIT License
986 stars 126 forks source link

Relay not generating correct query after setVariable-- am I doing something wrong? #54

Closed rohit-ravikoti closed 7 years ago

rohit-ravikoti commented 7 years ago

Hi! For some reason, if I generate a root query which takes in parameters before injecting the child component, like so:

import Relay from 'react-relay';

export default {
  production: (Component) => Relay.QL`
    query {
      getProduction(id: $productionId) {
        ${Component.getFragment('production')}
      }
    }
  `
};

Relay originally generates this query:

query MyProductionDetailsQuery($id_0:ID!,$where_1:ProductionRoleWhereArgs!) {
  getProduction(id:$id_0) {
    id,
    ...F0
  }
}
fragment F0 on Production {
  id,
  title,
  company,
  city,
  expDate,
  archived,
  start,
  end,
  paymentType,
  paymentDetails,
  description,
  hasFee,
  feeDetails,
  type,
  _roles4oPiwv:roles(first:10,where:$where_1) {
    edges {
      node {
        id,
        title,
        type,
        archived,
        description
      },
      cursor
    },
    pageInfo {
      hasNextPage,
      hasPreviousPage
    }
  }
}

However, If the Component's relay container has variables of its own, running this.props.relay.setVariables({...variables}) completely changes the request query generated by relay into something like this:

query My_production_details_page_ProductionRelayQL($id_0:ID!,$where_1:ProductionRoleWhereArgs!) {
  node(id:$id_0) {
    ...F0
  }
}
fragment F0 on Production {
  id,
  _roles6J5gK:roles(first:10,where:$where_1) {
    edges {
      node {
        id,
        title,
        type,
        archived,
        description
      },
      cursor
    },
    pageInfo {
      hasNextPage,
      hasPreviousPage
    }
  }
}

I'm not sure if this is an error with relay itself or if I am doing something wrong. Could someone help?

lvarayut commented 7 years ago

Sorry for my late reply. I think the query is already correct. Basically, when you use setVariables({...variables}), Relay tries to intersect the changes against the "tracked query". So, Relay could send only the changed parts across the wire for the sake of minimizing the network usage. You can learn more on this video.

lvarayut commented 7 years ago

I'm going to close this issue for now. Please feel free to continue the discussion here if you had more question.