New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
custom schemaDirectives not working in custom plugin #2108
Comments
Hmm, I tried to use the same code I used before to test the plugin and it wouldn't load: Here is my codegen.yml: schema: ./example/**/*.graphql
generates:
migrations/0_generated.js:
plugins:
- ./index.js Here is error:
My plugin looks fine, liek this: const { printSchema } = require('graphql')
module.exports = {
plugin: (schema, documents, config) => {
console.log(printSchema(schema))
}
} You can see the whole thing here. |
I tried it locally, and the error I'm getting is:
(comes from It seems like there is a very specific issue with calling your plugin file Change your plugin file to Oh, and also, when using alpha versions, make sure to specify the version in |
Yep, true. I got the error before I made
Well, this would be true, if I was worried about the version cascading, but I'm just testing for you until it's published, at which point I will use
Hmm, this seems like a bug? I moved it to a directive @db(table: String, key: String) on OBJECT
directive @nodb on FIELD | FIELD_DEFINITION
directive @link(field: String) on FIELD | FIELD_DEFINITION
type Post {
id: ID!
title: String!
body: String!
author: User!
}
type User {
id: ID!
email: String!
firstName: String
lastName: String
name: String
posts: [Post]!
} |
I dunno, if I run this in my plugin:
I get the arguments to the directive correctly, so I guess this really is an issue of If the answer is simply "don't use visitor-pattern, because it has a requirement on a stripped schema" I can totally live with that and do it all very manually with loops. Maybe I could even do a quick lookup of directive args and stuff first, then do a visitor thing after. no big deal. |
@konsumer yeah it seems to work.
|
That is perfect. When I do this, it also works to get all the directives and their args in a nice lookup table: module.exports = {
plugin: (schema, documents, config) => {
// printSchema strips directives, so build a table of info first
const tables = Object.keys(schema._typeMap).filter(t => schema._typeMap[t] && schema._typeMap[t].astNode && schema._typeMap[t].astNode.directives.filter(d => d.name.value === 'db').length)
const fields = {}
tables.forEach(table => {
fields[table] = {}
Object.keys(schema._typeMap[table]._fields).forEach(f => {
const directives = schema._typeMap[table]._fields[f].astNode.directives.map(d => {
const args = {}
d.arguments.forEach(a => {
args[a.name.value] = a.value.value
})
return { name: d.name.value, args }
})
fields[table][f] = {
directives,
name: f
}
})
console.log(fields)
})
},
addToSchema: `
directive @db(table: String, key: String) on OBJECT
directive @nodb on FIELD | FIELD_DEFINITION
directive @link(field: String) on FIELD | FIELD_DEFINITION
`
} I like const { visit, parse } = require('graphql')
const { printSchemaWithDirectives } = require('graphql-toolkit')
module.exports = {
plugin: (schema, documents, config) => {
const printedSchema = printSchemaWithDirectives(schema)
const astNode = parse(printedSchema)
const visitor = {
FieldDefinition: node => {
if (node.directives && node.directives.length) {
node.directives.forEach(d => {
const args = {}
d.arguments.forEach(a => {
args[a.name.value] = a.value.value
})
console.log(d.name.value, args)
})
}
},
ObjectTypeDefinition: node => {
// This function triggered per each type
}
}
const result = visit(astNode, { leave: visitor })
},
addToSchema: `
directive @db(table: String, key: String) on OBJECT
directive @nodb on FIELD | FIELD_DEFINITION
directive @link(field: String) on FIELD | FIELD_DEFINITION
`
} |
Yeah, it's much easier to access and check the AST using Keeping open until we'll release a stable version with the fix. |
Fixed in 1.4.0 🎉 |
Describe the bug
schemaDirectives
don't seem to work withvisitor
pattern for making a plugin, but I may be using it wrong. Before it even gets to visitor, the directives have been stripped from the schema.I basically want to generate sql stuff (knex migrations, resolvers & join-monster annotations) with graphql SDL to define config.
To Reproduce
Steps to reproduce the behavior:
gets turned into this:
With this plugin-code:
The text was updated successfully, but these errors were encountered: