graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Allow to validate a full schema

Open martinbonnin opened this issue 6 months ago • 5 comments

GraphQL JS has a function to validate a user schema, validateSchema().

This works well if the schema is a user schema not containing any of the builtin types.

In some cases though, we might want to validate a schema coming from introspection. This is typically the case in GraphiQL but other clients might also want to do this as a sanity check before running code generation or anything else.

In the case of a remote service, there is a high chance that the results of introspection do not match the version of GraphQL JS being used (because the tools haven't been updated).

In those cases, validation may fail for otherwise valid schemas.

Reproducer:

// This is valid SDL returned from an hypothetical newer version of GraphQL
const typeDefs = `
    type Query {
      hello: String
    }
    
    enum __ErrorBehaviour {
      NULL
      PROPAGATE
      HALT
    }

    directive @behavior(onError: __ErrorBehaviour) on SCHEMA
`
let schema = buildSchema(typeDefs)
let errors = validateSchema(schema)

Output:

  GraphQLError: Name "__ErrorBehaviour" must not begin with "__", which is reserved by GraphQL introspection.
      at SchemaValidationContext.reportError (/Users/martinbonnin/git/graphql-js-playground/node_modules/graphql/type/validate.js:73:7)
      at validateName (/Users/martinbonnin/git/graphql-js-playground/node_modules/graphql/type/validate.js:216:13)
      at validateTypes (/Users/martinbonnin/git/graphql-js-playground/node_modules/graphql/type/validate.js:239:7)
      at validateSchema (/Users/martinbonnin/git/graphql-js-playground/node_modules/graphql/type/validate.js:43:3)
      at Object.<anonymous> (/Users/martinbonnin/git/graphql-js-playground/dist/index.js:18:43)
      at Module._compile (node:internal/modules/cjs/loader:1723:14)
      at Object..js (node:internal/modules/cjs/loader:1888:10)
      at Module.load (node:internal/modules/cjs/loader:1458:32)
      at Function._load (node:internal/modules/cjs/loader:1275:12)
      at TracingChannel.traceSync (node:diagnostics_channel:322:14) {
    path: undefined,
    locations: [ [Object] ],
    extensions: [Object: null prototype] {}
  }

It would be nice to have a validateFullSchema() that is able to deal with Full Schemas, including built in types.

Context: https://github.com/graphql/graphiql/issues/3968

martinbonnin avatar May 28 '25 14:05 martinbonnin

hey @martinbonnin is this issue still valid? and if i got the point. we need to create a new function which will validate the full schema including the graphql introspection,

AngadSudan avatar Oct 13 '25 09:10 AngadSudan

@AngadSudan yes, this is pretty much still valid. There is a terminology discussion to be had (see https://github.com/graphql/graphql-wg/blob/main/rfcs/FullSchemas.md) but yea, a new graphql-js function would help.

martinbonnin avatar Oct 13 '25 09:10 martinbonnin

I'd like to contribute to this issue. Can you assign it to me

AngadSudan avatar Oct 14 '25 09:10 AngadSudan

Of course, thank you so much for looking into that ❤️ You should also join our next working group as well as the #wg channel on Discord

martinbonnin avatar Oct 14 '25 09:10 martinbonnin

@AngadSudan are you currently working on this issue? If not, I can take it over

bas0N avatar Dec 01 '25 22:12 bas0N