graphql / graphql/graphql-spec

Document Level Directives

Open
#1,041 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

## **GraphQL Document-Level Directives Proposal**

### **Introduction**
As GraphQL's popularity continues to surge, its potential extends beyond just efficient data fetching. Solutions like Apollo Federation, Hasura, Prisma and Tailcall have already showcased the power and flexibility of the GraphQL DSL. Recognizing this, our proposal seeks to further harness this potential by allowing directives at the document level, opening doors to more intuitive ORMs, CMS configurations, and backend tooling.

### **Problem Statement**
While the current GraphQL specification permits directives on the schema, it does not support them at a broader document level. This limitation restricts the configuration options for applications that use the GraphQL DSL for more than just data-fetching.

### **Proposed Solution**
Allow directives to be attached directly to the document. Instead of the current:

```graphql
schema
@disableHttpCache
@idleConnectionPool(size: 300, timeout: 300)
@namespace(name: "global") {
query: Query
mutation: Mutation
}
```

We propose:

```graphql
@disableHttpCache
@idleConnectionPool(size: 300, timeout: 300)
@namespace(name: "global")

schema {
query: Query
mutation: Mutation
}
```

This change provides a cleaner syntax, especially when directives don't semantically fit at the schema level.

### **Test On Guiding Principles**

- **Backwards Compatibility**: Existing APIs remain unaffected. This proposal introduces a new type for directive definitions: `DOCUMENT`.
```graphql
directive @disableHttpCache on DOCUMENT
```

- **Performance**: No performance implications.

- **Favour No Change**: Implementing this proposal will require changes in existing GraphQL parsers.

- **Real Use Cases**: With GraphQL DSL powering ORMs, proxies (e.g., [Tailcall]), and orchestrations through Federation, this change can make directive definitions even more impactful.

- **Simplicity & Consistency**: The proposal aligns with existing syntax and does not introduce new complexities.

- **Preserve Option**: No implications on this principle.

- **Understandability**: The proposed change aims to be intuitive, but we welcome community feedback.

### **Conclusion**
By embracing document-level directives, GraphQL can further solidify its position as a versatile tool for diverse applications. We look forward to the community's feedback on this proposal.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.