graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.08k stars 2.03k forks source link

Handlers for custom directives #1854

Open xamgore opened 5 years ago

xamgore commented 5 years ago

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.

IvanGoncharov commented 5 years ago

@xamgore Thanks for opening a separate issue 👍 As a temporary workaround, you can use:

buildSchema(fs.readFileSync('./schema.graphql', 'utf8'), { assumeValidSDL: true })