Skip to content

Allow to validate a full schema #4415

@martinbonnin

Description

@martinbonnin

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: graphql/graphiql#3968

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions