0

Is it possible to optionally include parameters when creating a Relay query?

See below I'm querying resources, and I'm hard coding the params here.

I would like to conditionally include some of these arguments, for example date_gt and date_lt. At the moment I have to set initial values for these, but that falls apart as I need to query for records without dates at all.

If I can't do that is it possible to send null as a value to a param here because I'm not having much luck with that either at the moment.

    fragments: {
    viewer: () => Relay.QL`
        fragment on Viewer {
            resources( 
                    first: $pageSize
                    q: $q
                    type: $types
                    license: $licenses
                    order: $order
                    access_rights: "published"
                    orphan: true
                    date_gt: $dateFrom
                    date_lt: $dateTo
            )
            {
               total
               edges {
                    node {
                        ${ArticleResult.getFragment('resource')}
                    }
                }
                pageInfo {
                    hasNextPage
                }
            }
        }
    `
},
Tim
  • 4,471
  • 5
  • 36
  • 42

1 Answers1

1

Yes, we can provide null as the value for an optional argument in Relay (client-side).

However, we can also provide default values for those optional arguments. For example, in the GraphQL schema, the field resources can be like:

resources: {
  type: ResourceConnection,

  args: {
    // other args go here
    date_gt: {
      type: GraphQLString,
      defaultValue: '1970-01-01'
    },
    date_lt: {
      type: GraphQLString,
      defaultValue: '2030-12-31'
    },
    ...connectionArgs
  },

  resolve: async (root, {...otherArgs, date_gt, date_lt, ...args}) => {

    // Check date_gt and/or date_lt values. If they equal the default
    // invalid values, ignore them while calculating output. Sometimes it
    // is possible that the default values are good enough to be equivalent
    // of user-provided values.

    return output;
  },
},

If we do not provide a default value for an optional argument, we can set value of the optional argument to null. In that case, undefined value is received on the server side:

resources: {
  type: ResourceConnection,

  args: {
    // other args go here
    date_gt: {
      type: GraphQLString,
    },
    date_lt: {
      type: GraphQLString,
    },
    ...connectionArgs
  },

  resolve: async (root, {...otherArgs, date_gt, date_lt, ...args}) => {
    console.log(`date_gt: ${date_gt}, date_lt: ${date_lt}`);

    // Check date_gt and/or date_lt values. If they are not provided,
    // date_gt and date_lt are `undefined`. Calculate output accordingly.

    return output;
  },
},
Ahmad Ferdous
  • 3,351
  • 16
  • 33
  • Thanks, yeah so just setting a param to null seems to cause Relay to not pass that param up to the GraphQL server, which is exactly what I wanted. – Tim May 18 '16 at 09:44