Skip to content

Enable 'schema' keyword to be provided without root operations #1166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benjie
Copy link
Member

@benjie benjie commented May 1, 2025

This PR is motivated by:

In the above PR we want to be able to indicate the error propagation behavior of a schema. However, I find it displeasing that to do so we would need to also explicitly detail the operations supported by the schema when they would normally be auto-detected based on their names:

+schema @behavior(onError: NO_PROPAGATE) {
+  query: Query
+  mutation: Mutation
+  subscription: Subscription
+}
+
 type Query {
   q: Int
 }

 type Mutation {
   m: Int
 }

 type Subscription {
   s: Int
 }

This PR makes it so that you can use a schema keyword to add directives and/or description to the schema without needing to also specify the root operation types if they can be inferred by the default naming. The above diff would thus become:

+schema @behavior(onError: NO_PROPAGATE)
+
 type Query {
   q: Int
 }

 type Mutation {
   m: Int
 }

 type Subscription {
   s: Int
 }

Much nicer! ✨

This is also useful without the onError feature, since it allows you to apply a description to a schema without having to detail the operations:

+"""Just an example"""
+schema
+
 type Query {
   q: Int
 }

 type Mutation {
   m: Int
 }

 type Subscription {
   s: Int
 }

PR #1164 applies these changes onto #1163; but this PR is mergeable into the spec as-is.

Copy link

netlify bot commented May 1, 2025

Deploy Preview for graphql-spec-draft ready!

Name Link
🔨 Latest commit 470358c
🔍 Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/681344fa07b2300008ab1b36
😎 Deploy Preview https://deploy-preview-1166--graphql-spec-draft.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@fotoetienne
Copy link
Contributor

What about instead allowing the block to be empty?

i.e.:

schema @behavior(onError: NO_PROPAGATE) {}

This is perhaps less likely to break existing parsers

@benjie
Copy link
Member Author

benjie commented May 1, 2025

This is perhaps less likely to break existing parsers.

It's not.

import { parse } from 'graphql';

const ast = parse(`schema {}`);
console.dir(ast);
/home/benjie/Dev/graphql/graphql-js/npmDist/error/syntaxError.js:13
  return new _GraphQLError.GraphQLError(`Syntax Error: ${description}`, {
         ^

GraphQLError: Syntax Error: Expected Name, found "}".
    at syntaxError (/home/benjie/Dev/graphql/graphql-js/npmDist/error/syntaxError.js:13:10)
    at Parser.expectToken (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:1337:40)
    at Parser.parseOperationType (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:246:33)
    at Parser.parseOperationTypeDefinition (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:724:28)
    at Parser.many (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:1440:26)
    at Parser.parseSchemaDefinition (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:706:33)
    at Parser.parseDefinition (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:171:23)
    at Parser.many (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:1440:26)
    at Parser.parseDocument (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:127:25)
    at parse (/home/benjie/Dev/graphql/graphql-js/npmDist/language/parser.js:28:27) {
  path: undefined,
  locations: [ { line: 1, column: 9 } ],
  extensions: [Object: null prototype] {}
}

Node.js v22.14.0

What about instead allowing the block to be empty?

To me, an empty block would explicitly say there are no operation types, whereas a missing block says to use the defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💭 Strawman (RFC 0) RFC Stage 0 (See CONTRIBUTING.md)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants