2.3k sao GitHub và vẫn tăng — apollographql/graphql-tag là dự án TypeScript mà TopGit đang theo dõi trên nền tảng. A JavaScript template literal tag that parses GraphQL queries
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.
Helpful utilities for parsing GraphQL queries. Includes:
gql A JavaScript template literal tag that parses GraphQL query strings into the standard GraphQL AST.
/loader A webpack loader to preprocess queries
graphql-tag uses the reference graphql library under the hood as a peer dependency, so in addition to installing this module, you'll also have to install graphql.
gql
The gql template literal tag can be used to concisely write a GraphQL query that is parsed into a standard GraphQL AST. It is the recommended method for passing queries to Apollo Client. While it is primarily built for Apollo Client, it generates a generic GraphQL AST which can be used by any GraphQL client.
Note:While it may seem redundant to have to both embed the userFragment variable in the template literal AND spread the ...User_user fragment in the graphQL selection set, this requirement makes static analysis by tools such as eslint-plugin-graphql possible.
Why use this?
GraphQL strings are the right way to write queries in your code, because they can be statically analyzed using tools like eslint-plugin-graphql. However, strings are inconvenient to manipulate, if you are trying to do things like add extra fields, merge multiple queries together, or other interesting stuff.
That's where this package comes in - it lets you write your queries with ES2015 template literals and compile them into an AST with the gql tag.
Caching parse results
This package only has one feature - it caches previous parse results in a simple dictionary. This means that if you call the tag on the same query multiple times, it doesn't waste time parsing it again. It also means you can use === to compare queries to check if they are identical.
Importing graphQL files
To add support for importing .graphql/.gql files, see Webpack loading and preprocessing below.
Given a file MyQuery.graphql
query MyQuery {
...
}
If you have configured the webpack graphql-tag/loader, you can import modules containing graphQL queries. The imported value will be the pre-built AST.
import MyQuery from 'query.graphql'
Importing queries by name
You can also import query and fragment documents by name.
query MyQuery1 {
...
}
query MyQuery2 {
...
}
And in your JavaScript:
import { MyQuery1, MyQuery2 } from 'query.graphql'
Preprocessing queries and fragments
Preprocessing GraphQL queries and fragments into ASTs at build time can greatly improve load times.
Babel preprocessing
GraphQL queries can be compiled at build time using babel-plugin-graphql-tag. Pre-compiling queries decreases script initialization time and reduces bundle sizes by potentially removing the need for graphql-tag at runtime.
TypeScript preprocessing
Try this custom transformer to pre-compile your GraphQL queries in TypeScript: ts-transform-graphql-tag.
React Native and Next.js preprocessing
Preprocessing queries via the webpack loader is not always possible. babel-plugin-import-graphql supports importing graphql files directly into your JavaScript by preprocessing GraphQL queries into ASTs at compile-time.
E.g.:
import myImportedQuery from './productsQuery.graphql'
class ProductsPage extends React.Component {
...
}
Webpack loading and preprocessing
Using the included graphql-tag/loader it is possible to maintain query logic that is separate from the rest of your application logic. With the loader configured, imported graphQL files will be converted to AST during the webpack build process.
Preprocessing GraphQL imports is supported in create-react-app >= v2 using evenchange4/graphql.macro.
For create-react-app < v2, you'll either need to eject or use react-app-rewire-inline-import-graphql-ast.
Testing
Testing environments that don't support Webpack require additional configuration. For Jest use jest-transform-graphql.
Support for fragments
With the webpack loader, you can import fragments by name:
In a file called query.gql:
fragment MyFragment1 on MyType1 {
...
}
fragment MyFragment2 on MyType2 {
...
}
And in your JavaScript:
import { MyFragment1, MyFragment2 } from 'query.gql'
Note: If your fragment references other fragments, the resulting document will
have multiple fragments in it. In this case you must still specify the fragment name when using the fragment. For example, with @apollo/client you would specify the fragmentName option when using the fragment for cache operations.
Warnings
This package will emit a warning if you have multiple fragments of the same name. You can disable this with:
import { disableFragmentWarnings } from 'graphql-tag';
disableFragmentWarnings()
Experimental Fragment Variables
This package exports an experimentalFragmentVariables flag that allows you to use experimental support for parameterized fragments.
You can enable / disable this with:
import { enableExperimentalFragmentVariables, disableExperimentalFragmentVariables } from 'graphql-tag';
Enabling this feature allows you to declare documents of the form.
fragment SomeFragment ($arg: String!) on SomeType {
someField
}
Resources
You can easily generate and explore a GraphQL AST on astexplorer.net.
apollographql/graphql-tag có 2.3k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/apollographql/graphql-tag. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
apollographql/graphql-tag có phải mã nguồn mở không?
Có — apollographql/graphql-tag 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/apollographql/graphql-tag.
apollographql/graphql-tag còn đang phát triển không?
Commit gần nhất trên apollographql/graphql-tag là 4 tháng trước (theo timestamp GitHub). Repo có 177 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
apollographql/graphql-tag dùng license gì?
apollographql/graphql-tag 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.
apollographql/graphql-tag là gì?
apollographql/graphql-tag (apollographql/graphql-tag) là dự án TypeScript trên GitHub. Theo mô tả gốc: A JavaScript template literal tag that parses GraphQL queries
apollographql/graphql-tag viết bằng ngôn ngữ gì?
apollographql/graphql-tag chủ yếu viết bằng TypeScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc thêm về apollographql/graphql-tag ở đâ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/apollographql/graphql-tag là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về graphql-tag?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về graphql-tag.