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

SDL Validation Prior to Schema Creation

Open Cito opened this issue 4 years ago • 2 comments

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?

Cito avatar Mar 03 '21 17:03 Cito

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

saihaj avatar Mar 03 '21 23:03 saihaj

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

yaacovCR avatar Oct 17 '24 22:10 yaacovCR