graphql-js
graphql-js copied to clipboard
Handlers for custom directives
Hi. Seems that there is no way to handle custom directives. This issue was created at @IvanGoncharov's request.
I use graphql-faker to define schemas with fake data. Frontend developers feel really great with it, cause it consists of easy-to-run-graphql-server and there is no need in heavy backend, databases, etc.
$ graphql-faker --open ./schema.graphql
To fake data, schema must contain some additional directives: @fake(…) and @examples(…). They describe the true nature of entities: names, emails, phone numbers, …
type Query {
hello: String! @fake(type: firstName)
}
As a GraphQL API developer, I write the following code:
import { graphql, buildSchema } from 'graphql'
const schema = buildSchema(fs.readFileSync('./schema.graphql', 'utf8'))
app.use('/graphql', graphqlHTTP({ schema, rootValue }))
And I want the graphql parser just to ignore those @fake and @examples directives, and have a single schema.graphql file both for graphql-faker, and for my graphql API.
As a bypass I wrote a regex, though it's not reliable (context-grammar, braces, quotes, all that stuff). The better way is to resolve custom directives after grammar parsing, and custom directives handlers could help here.
@xamgore Thanks for opening a separate issue 👍 As a temporary workaround, you can use:
buildSchema(fs.readFileSync('./schema.graphql', 'utf8'), { assumeValidSDL: true })
@IvanGoncharov i guess the method of providing this hook may depend on the upcoming js-wg discussion