graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.06k stars 2.02k forks source link

SDL Validation Prior to Schema Creation #2944

Open Cito opened 3 years ago

Cito commented 3 years ago

Recently the issue was raised for the Python port graphql-core that enum values aren't validated against their defintion when they are used as default arguments SDL. This is the example that was given:

const sdl = `
enum Role {
  ADMIN
  USER
}

type Query {
  hello(r: Role = ADMIN): String
  complex(i: Test = { role: DOES_NOT_EXIST }): String
  complexAlt(i: Test): String
}

input Test {
  ignore: String
  role: Role = INVALID
}
`

const schema = graphql.buildASTSchema(graphql.parse(sdl))

const fields = schema.getType('Query').getFields()
const arg1 = fields.hello.args[0]
const arg2 = fields.complex.args[0]

console.log(arg1, arg2)

Should schema validation not show this is an error?

saihaj commented 3 years ago

I guess schema validation should throw an error.

GraphQL services must return one of the defined set of possible values. If a reasonable coercion is not possible they must raise a field error GraphQL enum result

In this case role doesn’t choose from one of the possible defined types in the set which means it should raise an error.

CC: @IvanGoncharov

yaacovCR commented 2 days ago

Keeping this open despite #3814 because it would be nice if this could be validated within the SDL.