Snapshot của APIs-guru/graphql-lodash: 1.2k★ · TypeScript · API. 🛠 Data manipulation for GraphQL queries with lodash syntax
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Unleash the power of lodash inside your GraphQL queries
Table of contents
Why?
Example queries
API
Usage Examples
fetch
Caching clients
Usage with react-apollo
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 receives 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:
import { graphqlLodash } from 'graphql-lodash';
function lodashQuery(queryWithLodash) {
let { query, transform } = graphqlLodash(queryWithLodash);
// Make a GraphQL call using 'query' variable as a query
// And place result in 'result' variable
...
result.data = transform(result.data);
return result;
}
Fetch example
An example of a simple client based on fetch API:
function executeGraphQLQuery(url, query) {
return fetch(url, {
method: 'POST',
headers: new Headers({"content-type": 'application/json'}),
body: JSON.stringify({ query: query })
}).then(response => {
if (response.ok)
return response.json();
return response.text().then(body => {
throw Error(response.status + ' ' + response.statusText + '\n' + body);
});
});
}
function lodashQuery(url, queryWithLodash) {
let { query, transform } = window.GQLLodash.graphqlLodash(queryWithLodash);
return executeGraphQLQuery(url, query).then(result => {
result.data = transform(result.data);
return result;
});
}
// then use as bellow
lodashQuery('https://swapi.apis.guru', `{
planetWithMaxPopulation: allPlanets @_(get: "planets") {
planets @_(maxBy: "population") {
name
population
}
}
}`).then(result => console.log(result.data));
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.
Usage with react-apollo
When using with Apollo you can use props option to apply transformations:
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:
import {
buildClientSchema,
extendSchema,
graphqlSync,
introspectionQuery,
} from 'graphql';
// inside the above ApolloLink function
if (response.data && response.data.__schema) {
const schema = extendSchema(
buildClientSchema(response.data),
lodashDirectiveAST,
);
return graphqlSync(schema, introspectionQuery);
}
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.
GitHub topics của APIs-guru/graphql-lodash: "api", "functional-programming", "graphql", "lodash". TopGit xếp repo vào nhóm API.
APIs-guru/graphql-lodash có phải mã nguồn mở không?
Có — APIs-guru/graphql-lodash phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/APIs-guru/graphql-lodash.
APIs-guru/graphql-lodash có trang demo không?
Dự án có trang chủ ở https://apis.guru/graphql-lodash/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
APIs-guru/graphql-lodash còn đang phát triển không?
Commit gần nhất trên APIs-guru/graphql-lodash là 4.4 năm trước (theo timestamp GitHub). Repo có 47 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
APIs-guru/graphql-lodash dùng license gì?
APIs-guru/graphql-lodash phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc thêm về APIs-guru/graphql-lodash ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/APIs-guru/graphql-lodash là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
graphql-lodash có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về graphql-lodash.