GraphQL Lodash
Unleash the power of lodash inside your GraphQL queries
Table of contents
- Why?
- Example queries
- API
- Usage Examples
- Usage on server-side (tl;dr don't)
Why?
GraphQL allows to ask for what you need and get exactly that. But what about the shape?
GraphQL Lodash gives you the power of lodash
right inside your GraphQL Query using @_
directive.
Note: This is an experimental project created to explore the concept of Query and transformation collocation.
We encourage you to try it inside our demo or check detailed walkthrough.
Example queries
Here are a few query examples you can run against StartWars API:
Find the planet with the biggest population
Get gender statistics
Map characters to films they are featured in
Install
npm install --save graphql-lodash
or
yarn add graphql-lodash
API
graphqlLodash(query, [operationName])
- query (required) - query string or query AST
- operationName (optional) - required only if the query contains multiple operations
Returns
{
query: string|object,
transform: Function
}
- query - the original query with stripped
@_
directives - transform - function that recieves
response.data
as a single argument and returns the same data in the intended shape.
Usage Examples
The simplest way to integrate graphql-lodash
is to write wrapper function for graphql client of you choice:
; { let query transform = ; // Make a GraphQL call using 'query' variable as a query // And place result in 'result' variable ... resultdata = ; return result;}
Fetch example
An example of a simple client based on fetch API:
{ return ;} { let query transform = windowGQLLodash; return ;} // then use as bellow;
Caching clients
For caching clients like Relay and Apollo we recommend to apply the transformation after the caching layer. Here is proposed solution for Relay:
We are still figuring out how to do this and any feedback is welcome.
react-apollo
Usage withWhen using with Apollo you can use props
option to apply transformations:
const rawQuery = gql` # query with @_ directives`; const query transform = ;query ...props rawData: propsdata data: Component;
You can write a simple wrapper for simplicity:
;; { const query transform = ; let origProps = config && configprops || props; return comp;}// then use as bellowqueryComponent;
Just replace graphql
with gqlLodash
and you are ready to use lodash in your queries.
Check out the react-apollo-lodash-demo repo.
You can also do the transformation inside an Apollo
Link by rewriting the
parsed GraphQL Document
:
{ const query transform = ; operationquery = query; return ;};
Chaining this link with the other links passed to your ApolloClient
will apply the transformation to every query that
Apollo runs, such as those from the <Query />
component or
subscriptions.
Introspection queries
If your application uses introspection queries (like GraphiQL does to get documentation and autocomplete information), you will also need to extend the introspection query result with the directives from graphql-lodash. One way you could do this is:
; // inside the above ApolloLink functionif responsedata && responsedata__schema const schema = ; return ;
See the demo/
source in this repo for another example of modifying
the introspection query result.
Usage on server side
In theory, this tool can be used on the server. But this will break the contract and, most likely, will break all the GraphQL tooling you use. Use it on server-side only if you know what you do.