Graphql-js: A way to validate objects against schema?

Created on 7 Nov 2016  路  4Comments  路  Source: graphql/graphql-js

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!

Most helpful comment

What was the outcome of this discussion?

All 4 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adriano-di-giovanni picture adriano-di-giovanni  路  3Comments

benjie picture benjie  路  4Comments

galki picture galki  路  3Comments

davide-ganito picture davide-ganito  路  4Comments

gjuchault picture gjuchault  路  4Comments