What is graphql-tag and DocumentNode?
Answer
graphql-tag (imported as gql) is a template literal tag that parses GraphQL query strings into DocumentNode objects — the AST (Abstract Syntax Tree) representation that GraphQL clients like Apollo use internally. Instead of sending raw strings, you write: const GET_USER = gql`{ user(id: $id) { name } }`. The parsing happens once (at import time with module bundlers), not per request. DocumentNode is the TypeScript type from the graphql package representing a parsed GraphQL document. Using gql enables static analysis, syntax highlighting in IDEs with the GraphQL extension, and type generation with GraphQL Code Generator.
Previous
What is Apollo Client and how does it work with GraphQL?
Next
How do field resolvers work with the parent object in GraphQL?
More GraphQL Questions
View all →- Intermediate What is the N+1 problem in GraphQL and how does DataLoader solve it?
- Intermediate What are persisted queries in GraphQL?
- Intermediate What is the difference between schema-first and code-first GraphQL approaches?
- Intermediate What are the pagination strategies in GraphQL?
- Intermediate How is authentication handled in GraphQL?