graphql-kit / graphql-lodash Goto Github PK
View Code? Open in Web Editor NEW๐ Data manipulation for GraphQL queries with lodash syntax
Home Page: https://apis.guru/graphql-lodash/
License: MIT License
๐ Data manipulation for GraphQL queries with lodash syntax
Home Page: https://apis.guru/graphql-lodash/
License: MIT License
Thank you for you great job - I was looking to implement the server version on the express-graphql stack here is my implementation as an example - not perfect but works:
// schema is the initial schema you wanted to deploy
schema = extendSchema(
schema,
lodashDirectiveAST,
);
app.use('/graphql', (...args) => {
graphqlHTTP({
schema: schema,
graphiql: true,
customParseFn : (source) => {
// get the query from the body
let body = source.body;
// Process the lodash parsing
let { query, transform } = graphqlLodash(body);
// Replace the body in the Source instance
source.body = query;
// Use the standard parse method
let documentAST = graphql.parse(source);
// store the transform in the document, an element conveyed in customExecuteFn
documentAST.transform = transform;
return documentAST;
},
customExecuteFn : async ( { document, ...args } ) => {
// Execut first
let result = await graphql.execute( { document: document, ...args });
// Then back transform the query.
result.data = document.transform(result.data);
// Return the result
return result;
}
})(...args);
});
the query
{
Films: allFilms(first:1) @_(get:"films"){
films {
title
producers @_(join:",")
}
}
}
returns
{
"data": {
"Films": [
{
"title": "A New Hope",
"producers": "[object Object],[object Object]"
}
]
}
}
instead of
{
"data": {
"Films": [
{
"title": "A New Hope",
"producers": "Gary Kurtz,Rick McCallum"
}
]
}
}
I have the following query:
movie {
cast {
id
role
person {
actorId: id
fullName
}
}
}
Now I would like to flatten the cast into an array of not-nested objects.
movie {
cast {
id
role
actorId
fullName
}
}
It seems like lodash can't do that. Would be nice to add a functionality, maybe like:
movie {
cast @_(flattenObjectDepth: 1) {
id
role
person {
actorId: id
fullName
}
}ย
}
I could create a PR, with a little help as to how transformations need to look like.
This is freaking cool.
That is all.
Hi, this plugin it's awesome! I'm able to get it working with @apollo/client
with a custom ApolloLink
as suggested in documentation.
It's also possible to make GraphQL Code Generator
generate right types? I'm using it to generate Typescript types and React Hooks.
typescript-operations
is the plugin that generate types, maybe should be possible to pass graphql-lodash
custom directives and use them to generate right the types.
Some advice on how to do it?
I have a data structure like below
{
"MyAsset": {
"Code": "Cod123",
"MyAssetLocation": [{
"Location": {
"LocationName": "Location 1"
}
},{
"Location": {
"LocationName": "Location 2"
}
}
]
}
}
I wanted to write a graphql query against the entity MyAsset in such a way that it should return the data like below.
First record
{code:"Cod123",MyAssetLocation.Location:"Location 1"}
Second Record
{code:"Cod123",MyAssetLocation.Location:"Location 2"}
Basically I wanted to flatten the result by duplicating MyAsset details with MyAssetLocation.Location Items.
Does it support apollo-client?
One of the very common transformations I need (except from the ones that this library already enables) is casting a value into a Date. Would it be possible to add a function for that like:
movie {
title
releaseDate @_(as: "Date")
}
@IvanGoncharov Are you still maintaining this?
I get this error when I try to add this to my apollo client
Hi! this library seems amazing. How can i include it in my own graphiql client?
Hi,
I just get started learning Graphql and found this package extremely useful for formatting data reading from backend. Thank you!
My question is that is is possible to reverse the transformation process without manually manipulating the object? I'm asking because when I followed the Apollo Documentation (https://www.apollographql.com/docs/react/essentials/mutations/#making-all-other-cache-updates) to implement cache updating using useMutation
to create a new object. The newly created object needs to be concatenated to the list of old objects in order to update the cache.
So far I can use graphqlLodash
to get back the query
(readable to the Apollo
client) and use cache.readQuery
to get back the DATA
(before formatting by graphql-lodash
), but it seems not that easy to format the newly created object into the same format of that DATA
before I can concat them to update my list of objects.
If there's another way to work around it. Please let me know! Really appreciate that!
Thank you,
Changsheng
Thanks for this awesome library but I would love to see some more examples. What I see in the code there is more operators that you added but they are not documented.
Thanks for this library, it's really cool!
You mention this will break the contract and, most likely, will break all the GraphQL tooling you use but I don't really understand why - could you elaborate?
I've experimented a bit in a pet project and I have had no problems with it so far :-)
Regards
I am trying to figure out how to make graphql-lodash
work with Apollo mutation refetch queries . Thoughts ?
That's really such a great idea! Do you have already experience with Apollo - Angular integration?
If you have an example it would be really great to see it :)
Hi, I'm a developer advocate and came across this project (it's a great project!). I'd like to politely suggest either modernizing the gender data used in the tool demo, or simply demonstrating queries on a different dataset.
Genderkit is one of many resources helping content producers talk about gender in a modern, ethical way. Almost all such resources recognize "hermaphrodite" as an offensive term. While the usage in this project is only sample data in a query, I would strongly appreciate if you'd change the data source, or remove this offensive term and also use a data-set that does not reflect a male/female binary. Thank you! ๐
The example below will remove id and title from the post object. Is this as intended?
query getPost {
post(id: 1) @_(get:"user") {
id
title
user {
id
name
username
email
}
}
}
When I call
https://apis.guru/graphql-lodash/
the result pane shows "Failed to fetch"
The Saved Queries examples button on https://apis.guru/graphql-lodash/ is broken - tried Chrome & Firefox.
Hello,
First of all, thanks for the nice project.
I am trying to mock graphql-lodash
with Jest, in order to test components decorated with it.
Is there any de-facto / preferred way of mocking it? Below I add my code snippets.
Tests:
import React from 'react';
import chai, { expect } from 'chai';
import { shallow } from 'enzyme';
import chaiEnzyme from 'chai-enzyme';
import Foo from './Foo';
describe('Foo', () => {
const shallowed = shallow(<Foo />);
it('test Foo', () => {
{...}
});
});
chai.use(chaiEnzyme());
I have tried multiple ways to mock, but i keep on getting errors.
Thanks ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.