graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Handlers for custom directives

Open xamgore opened this issue 6 years ago • 1 comments

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 avatar May 07 '19 19:05 xamgore

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

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

IvanGoncharov avatar May 08 '19 05:05 IvanGoncharov

@IvanGoncharov i guess the method of providing this hook may depend on the upcoming js-wg discussion

yaacovCR avatar May 22 '22 17:05 yaacovCR