question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Directive and specifying the same on a Field and Type

See original GitHub issue

I’ve tried looking at the documentation to figure out how to accomplish this but got no where.

How do I define a schema like this using Graphene?


directive @unique(
  info: String = "Blah blah"
) on FIELD_DEFINITION | ENUM_VALUE

directive @ignore(
  info: String = Blah blah"
) on OBJECT

type ExampleType @ignore(info: "Blah"){
  newField: String
  oldField: String @unique(info: "Blah")
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
prashanthellinacommented, Jun 24, 2019

@jkimbo Thank you for the confirmation. I thought I was missing something. I believe there is merit in implementing directives as I find that a good way to signal behavior formally (rather than in comments) to both the user of the schema/service and the implementer of the resolver logic.

I quite liked the way that prisma (https://www.prisma.io/) uses directives to provide more expressiveness when using the SDL to describe ORM like interactions with a database.

For the moment, I am exploring using GraphQLS2S for defining the schema in an SDL superset (that support inheritance, generics etc to help reduce verbosity) and then transpiling it to standard SDL. After I have the final schema, I am using ariadne to implement the actual resolver logic.

2reactions
jkimbocommented, Jun 24, 2019

@prashanthellina thanks for your patience. So I’ve looked into this a bit more and it looks like it’s not possible to apply directives on a field/object level. This is a conscious limitation of the reference GraphQL server implementation graphql-js. You can read more about the reasoning here: https://github.com/graphql/graphql-js/pull/746#issuecomment-301554231 and follow the ongoing discussion about it here: https://github.com/graphql/graphql-js/issues/1343

The relevant comments from leebyron are:

Right now directives are purely a feature of the GraphQL language and IDL, so a schema not created using IDL definitionally won’t contain directives. When building a schema in code, you have the full power of the host programming environment, so directives shouldn’t be necessary. Directives are a tool for supplying additional intent to be later interpreted by the host programming environment.

and

Directives are purely a feature of the GraphQL language and are not part of type definitions - this is because different tools will use and interpret directives in different ways.

(there is also a great long comment which explains things in detail)

Since Graphene is based on graphql-core which is a direct port of the graphql-js implementation, support for directives on fields/objects won’t happen until that support is available in graphql-js.

I hope that’s helpful. You might also want to checkout ariadne which is an alternative way of building a GraphQL server using the SDL.


I’m going to create an new issue to document adding custom directives to the schema and then close this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Directives - Apollo GraphQL Docs
Configure GraphQL types, fields, and arguments. ... Your schema can define custom directives that can then decorate other parts of your schema: GraphQL....
Read more >
The @custom Directive - GraphQL - Dgraph
The @custom directive is used to define custom queries, mutations, and fields. The result types can be local or remote.
Read more >
Creating a Custom Directive • Angular - codecraft.tv
We create a directive by decorating a class with the @Directive decorator. The convention is to associate a directive to an element via...
Read more >
How to use and create custom directives in Angular
Directive provides the same functionality for the @Directive decorator. The Input decorator is used to communicate between the two components.
Read more >
Field arguments vs. directives in GraphQL - LogRocket Blog
Query-type directives cannot be used to modify the source of the data, however, because their logic is provided via directive resolvers, which ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found