Graphql-tools: Testing a query validity against a specific schema

Created on 22 Apr 2016  路  17Comments  路  Source: ardatan/graphql-tools

The Adrenaline library offers a way to verify that a query is valid against a particular schema, you can read more detailed explanations here.

test

This can be useful to implement a test that would import all queries on the client and verify that there are still valid after a change in the server schema. I would be great to have something similar in the Apollo ecosystem.

Most helpful comment

One thing that I think will be really useful is a standard config for where to get a schema for the various dev tools, like a graphql-config.json or something. Maybe it would be like:

{
  // Tell tools/editors where to find your schema for validation
  schema: {
    // One of the below
    introspectionJSON: "./data.json",
    schemaModule: "./schema.js",

    // Allow running actual queries from your editor maybe?
    endpointUrl: "http://localhost:3000/graphql",
  },

  // If you want to keep your queries in separate files
  queryFiles: {
    fileExtension: "gql",
  },

  // Configure various linters
  lint: {
    templateTagName: "gql",
  },

  // .. options for other plugins here
}

That way, if different developers on your team are using different editors, they can share configuration about your GraphQL-based project.

All 17 comments

@mquandalle I'm working on a linter to do this today, let's see how that goes!

One thing that I think will be really useful is a standard config for where to get a schema for the various dev tools, like a graphql-config.json or something. Maybe it would be like:

{
  // Tell tools/editors where to find your schema for validation
  schema: {
    // One of the below
    introspectionJSON: "./data.json",
    schemaModule: "./schema.js",

    // Allow running actual queries from your editor maybe?
    endpointUrl: "http://localhost:3000/graphql",
  },

  // If you want to keep your queries in separate files
  queryFiles: {
    fileExtension: "gql",
  },

  // Configure various linters
  lint: {
    templateTagName: "gql",
  },

  // .. options for other plugins here
}

That way, if different developers on your team are using different editors, they can share configuration about your GraphQL-based project.

One issue with the specific idea above is that ./schema.js would be the most convenient way to do it, but if that JS file requires any transpilation at all -- it's using babel/ts/coffee -- then the linter/completer module will need to be able to interpret the code. So maybe the introspection result is the only feasible way to do it..

Yes we should rely on the client-server model of GraphQL to do things like query validation or autocompletion (as discussed in https://github.com/apollostack/vscode-graphql/issues/2). We shouldn鈥檛 try to emulate a server by taking a schema.js as we will always miss a lot of the server context, ie transpilation as you say or even tricker things like dynamic schemas鈥攊magine a modular app where extensions can modify the GraphQL schema.

The sad part is that in local dev mode, developers will have to launch a GraphQL server to benefit from the features we are discussing, but I would say that with the tooling available today this shouldn鈥檛 be much of an issue.

Yeah, I'm so sad about having to run a server. But I guess having a simple script to do that and print out an introspection JSON wouldn't be a big deal.

The linter approach is one way, but doing things the way adrenaline does it (i.e. mocha/chai) is also quite nice.
We could do the same, but we probably have to implement it in a more generic way that just takes a query and a schema and checks the validity. People could then build on that to implement react, angular and other integrations.

@mquandalle thoughts?

I'm pretty sure linting is better because you get the warnings in your editor directly, and it gives you the line number where you typed your query in the code. But if someone builds a mocha thing that's cool too!

I think you'd probably want both, so you can find errors during CI tests.

You can easily run the linter during CI.

For anyone that comes by this thread, the linter works: https://github.com/apollostack/eslint-plugin-graphql

Not sure if that means the issue should be closed or not.

Is there a recommendation for people who are not using Javascript?

What are you up to, how are you writing your queries, and what's your goal?

I'm writing an application that talks to a graphql server, I'm writing my queries in plain text files and i would like to be able to type check them on my ci server against a downloaded schema.

you can use this package as-is since it can also read in .graphql files! But if you look at the source code it's also easy to write your own tools using graphql-js

Sorry by "this package" I mean the eslint plugin.

Oh wonderful, thank you. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MehrdadKhnzd picture MehrdadKhnzd  路  3Comments

flippidippi picture flippidippi  路  3Comments

confuser picture confuser  路  4Comments

capaj picture capaj  路  4Comments

brennantaylor picture brennantaylor  路  4Comments