Skip to content
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

Fix ambiguity around when schema definition may be omitted #987

Merged
merged 6 commits into from
Feb 9, 2023

Conversation

benjie
Copy link
Member

@benjie benjie commented Aug 4, 2022

@rivantsov Pointed out in #978 that there's some ambiguity around when the schema keyword can be omitted from the SDL. Upon careful reading I've noticed that there is additional ambiguity around this topic.

While any type can be the root operation type for a GraphQL operation, the type
system definition language can omit the schema definition when the {query},
{mutation}, and {subscription} root types are named {"Query"}, {"Mutation"},
and {"Subscription"} respectively.

This seems to imply that all the root types are required in order to omit the schema definition. I've modified the text to indicate that the names only need to match for the root types that are actually present.

Likewise, when representing a GraphQL schema using the type system definition
language, a schema definition should be omitted if it only uses the default root
operation type names.

Imagine we're doing biological research, tracking mutations in a virus. We might have a schema like:

type Query {
  viruses: [Virus!]
}
type Virus {
  name: String!
  knownMutations: [Mutation!]!
}
type Mutation {
  name: String!
  geneSequence: String!
}
schema {
  query: Query
}

In this case we must not omit the schema definition when representing the schema using the SDL, because doing so would make it seem that the Mutation type was the root type for the mutation operation, when in fact the schema does not support a mutation operation.

I've clarified the wording to deal with this possibility too.

@netlify
Copy link

netlify bot commented Aug 4, 2022

Deploy Preview for graphql-spec-draft ready!

Name Link
🔨 Latest commit 058b4ec
🔍 Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/63e5482961e4c30008f85266
😎 Deploy Preview https://deploy-preview-987--graphql-spec-draft.netlify.app/draft
📱 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 settings.

@benjie benjie added the ✏️ Editorial PR is non-normative or does not influence implementation label Aug 4, 2022
@benjie
Copy link
Member Author

benjie commented Aug 4, 2022

I've submitted a larger edit that I think improves accuracy and makes reading (and referencing) easier by using a definition.

@benjie benjie changed the title Fix ambiguity when discussing the schema definition Fix ambiguity around when schema definition may be omitted Aug 4, 2022
@benjie
Copy link
Member Author

benjie commented Sep 3, 2022

@leebyron "default root type name" read better in context than "default operation type name" so I've gone with that. If you approve, I think this is ready to merge 👍

@benjie
Copy link
Member Author

benjie commented Jan 30, 2023

@graphql/tsc I'm going to add this to Thursday's WG, a review/merge before then would make the discussion shorter 😉

@leebyron leebyron added the 💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md) label Feb 2, 2023
@leebyron
Copy link
Collaborator

leebyron commented Feb 2, 2023

Marking this as a Proposal since this is a meaningful change. Two changes here:

  1. The clarity around default type names and whether the schema definition needs to exist is great, that's an editorial change.
  2. The insight that we must not omit the schema definition if a default type name exists in a non-root type is a new change and we need to ensure it's built into the reference impl.

The test case we need to see is your description above: when putting SDL in to in-memory, then back to SDL, does a non-root type named "Mutation" end up in the right place?

@benjie
Copy link
Member Author

benjie commented Feb 3, 2023

@graphql/implementers Please could you add the following to your test suites, and report back if there are any issues:

  1. Create a schema from the following SDL (or, if you don't support SDL, construct an equivalent schema)
  2. Assert that the schema does not support mutations
  3. Print that schema
  4. Assert that the result matches the schema, namely that the schema has a query operation named Query but does not have a mutation operation (the Mutation type being just a regular type)
type Query {
  viruses: [Virus!]
}

type Virus {
  name: String!
  knownMutations: [Mutation!]!
}

type Mutation {
  name: String!
  geneSequence: String!
}

schema {
  query: Query
}

I've raised a PR against GraphQL.js that you can use for inspiration: graphql/graphql-js#3839

@spawnia
Copy link
Member

spawnia commented Feb 4, 2023

@benjie I have added the described test case in webonyx/graphql-php#1303.

Constructing the schema from your example SDL worked fine, it did correctly not support mutations. I had to make the same fix as you did in graphql-js to correct the schema printer output.

leebyron added a commit to graphql/graphql-js that referenced this pull request Feb 9, 2023
This PR implements the tests and fixes necessary for [Spec RFC
#987](graphql/graphql-spec#987).

This PR is made of three main commits:

1. Test printing a schema that has `Query`, `Mutation` and `Virus`
types, but only supports `query` operations (via the `Query` type) and
does _not_ support `mutation` operations.
2. Test parsing this same schema text, and assert that the schema does
not have a mutation type.
3. Fix the printing of the schema.

Co-authored-by: Lee Byron <lee@leebyron.com>
@leebyron leebyron added 📄 Draft (RFC 2) RFC Stage 2 (See CONTRIBUTING.md) and removed 💡 Proposal (RFC 1) RFC Stage 1 (See CONTRIBUTING.md) labels Feb 9, 2023
@leebyron
Copy link
Collaborator

leebyron commented Feb 9, 2023

Necessary code changes looks great, key decisions look correct, good feedback from implementers - this is now RFC 2

@leebyron
Copy link
Collaborator

leebyron commented Feb 9, 2023

Would love feedback on my edits @benjie

  • I thought it would be more clear if we made root operation type a definition as well to see how these two relate.
  • I made some minor copy edits, please let me know if this improved clarity by your eye
  • I added a simplified version of the test case above

Comment on lines 238 to 239
While this example describes a valid GraphQL schema which happens to contain a
type called {"Mutation"} which is not the {`mutation`} _root operation type_.
Copy link
Member Author

Choose a reason for hiding this comment

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

This reads awkwardly, I think simply removing While would fix it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

reworded

Copy link
Member Author

Choose a reason for hiding this comment

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

Much better; spot on 👍

@benjie
Copy link
Member Author

benjie commented Feb 9, 2023

@leebyron Looks good to me! While we're at it... do you think we should rename "root operation type" to simply "root type"? That would fit better with "default root type name", and I don't think it loses clarity.

@leebyron
Copy link
Collaborator

leebyron commented Feb 9, 2023

While we're at it...

Yes this would be clarifying but to keep this one from expanding further I'll do a follow up

@leebyron leebyron force-pushed the schema-definition-ambiguity branch 2 times, most recently from 0ae0071 to 1684fde Compare February 9, 2023 19:12
@leebyron leebyron added 🏁 Accepted (RFC 3) RFC Stage 3 (See CONTRIBUTING.md) and removed 📄 Draft (RFC 2) RFC Stage 2 (See CONTRIBUTING.md) labels Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏁 Accepted (RFC 3) RFC Stage 3 (See CONTRIBUTING.md) ✏️ Editorial PR is non-normative or does not influence implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants