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

add directives definitions to import?

Open terion-name opened this issue 7 years ago • 5 comments

I have an app that parses schemas and is processing them.

I want to use a directive in these schemas, but not to write there it's definition (alike Prisma directives in datamodel). But I can't run import these schemas:

Error: Directive relation: Couldn't find type relation in any of the schemas.
    at collectDirective (.../node_modules/graphql-import/src/definition.ts:172:15)
    at Array.forEach (<anonymous>)
    at collectNode (.../node_modules/graphql-import/src/definition.ts:161:21)
    at .../node_modules/graphql-import/src/definition.ts:135:7
    at Array.forEach (<anonymous>)

graphql-import only works if I supply path to schema, not the schema content, so I can't simply concat directive definition during import.

So is there a way to overcome this? To add definitions during import? Or make it simply to ignore absence of relation

terion-name avatar Jul 11 '18 22:07 terion-name

Currently what I've done: I've copied scripts to project and changed main func like this:

export function importSchema(
    schema: string,
    schemas?: { [key: string]: string },
    ...typeDefs: string[] // added this
): string {
  const sdl = [read(schema, schemas) || schema, ...typeDefs].join(' '); // changed this

And it works like a charm (const importedSchema = importSchema(schema, null, relationDerictiveDefinition);). Don't know either something like this should be added to library

terion-name avatar Jul 11 '18 23:07 terion-name

on which file did you wrote this code? Thanks

armij avatar Aug 22 '18 13:08 armij

Unless I'm missing something, I'm also struggling with the same. importSchema() does not support adding in schemaDirectives {}, and will throw errors at all the @directives it comes across. ~(While makeExecutableSchema() does support it)~ (Update: That was incorrect)

ecker00 avatar Jan 31 '19 22:01 ecker00

This seem to be related #153 as it's any directive that's not directly declared in the imported schema, but imported at a later stage. In my case I would like to use constraint from graphql-constraint-directive.

~Seems @terion-name suggestion could resolve this, by adding it as an argument into the importSchema() function. Would this feature conflict with other parts of graphql-import?~

Update: Works fine as long as the directive has a basic defenition in the schema, which seems logical:

directive @constraint(
  minLength: Int,
  maxLength: Int,
   …
} on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

Sorry for any confusion.

ecker00 avatar Jan 31 '19 22:01 ecker00

Hi @atten ! In 1.0.0 beta release, we introduced a lot of changes. Could you install graphql-import@beta to try new changes? Don't forget to modify your code regarding to the migration notes in README. https://github.com/ardatan/graphql-import#updating-from-07x

Let us know if your issue still persists.

ardatan avatar Jan 01 '20 18:01 ardatan