Skip to content

Instantly share code, notes, and snippets.

@relwell
Created August 7, 2018 18:25
Show Gist options
  • Save relwell/96119c186838dd31e07a54d92ea9c6b3 to your computer and use it in GitHub Desktop.
Save relwell/96119c186838dd31e07a54d92ea9c6b3 to your computer and use it in GitHub Desktop.
import React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import _ from 'lodash';
class SomeComponent extends React.Component {
render() {
const someFieldMap = { nctId: 'nct_id', averageRating:'average_rating' };
const fields = _.map(someFieldMap, (value, key) => `${key}:field(name:"${value}")`);
const query = gql`
query mySearch($q:String! $pageSize:Int $sorts:[String]
search(params:{
q:$q pageSize:$pageSize sorts:$sorts
}) {
data {
${fields.join("\n")}
}
}
}
`
const MyComponent = graphql(query, {
options: (params) => ({
variables: ({ q }) => ({ q, ...params }),
}),
});
return (
<MyComponent {...this.props} />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment