I would like to be able to use graphQL schemas to validate my objects against the types defined in graphql schemas like so:
'use strict'
const { expect } = require('chai')
const { buildSchema } = require('graphql/utilities')
describe('validating objects with graphql', function() {
it('returns true or false given an object, type name and a schema', function () {
const schema = buildSchema(`
type User {
id: String
name: String
}
`)
let valid = validate({ name: 123}, 'User', schema)
expect(valid).to.be(false)
let valid = validate({ name: 'swist'}, 'User', schema)
expect(valid).to.be(true)
})
})
Is this possible? Or am I misunderstanding something fundamental about the spec?
I've had small dig around the library and the docs and it doesn't seem like graphql-js provides something to this effect.
Essentially it would allow people who want to use graphql to gradually write out schemas for their models.
A pointer whether it is desirable and possible to implement validate above would be much appreciated!
Moving this to slack!
@swist looks like @abhiaiyer91 is working on something similar! https://github.com/Workpop/typed-validation
What was the outcome of this discussion?
@swist
Moving this to slack!
Is there a (perma)link to the conversation?
Most helpful comment
What was the outcome of this discussion?