Skip to content

the import expression only works if one appears at start of file (seo: graphql-tools import expression not working) #6995

@keilin-anz

Description

@keilin-anz

Overview

The import expression must be the first non-blank line of a file, otherwise no imports are processed.

Not a big deal, but as a first-time user it left me scratching my head for a fair while before discovering the reason - just hoping to save future peoples' scalps 😁

Note

I've made this issue overly detailed since I can't raise a PR from where I am, but also it's unclear what the preferred fix would be
(perhaps this should have been a discussion instead?)

Context

Concern

The above behaviour isn't documented (AFAICT)

link to (presumedly) relevant section:

### Using `#import` Expression
Assume the following directory structure:

Note

Whilst this could be as simple as adding a callout to the documentation, there may be other options..

Examples

quietly fails:

# This schema has some heading context, or a license, etc..
# and then has an import
#import * from 'this-will-fail-silently.graphql'

works fine:

#import * from 'this-will-work-fine.graphql'
# This schema has some heading context, or a license, etc.. after its import

interestingly enough, this also works fine (since invalid import lines are ignored):

#import
# This schema has an invalid import at the top which (unintended benefit!) acts as a pragma
# to enable the following to still work
#import * from 'this-will-work-too.graphql`

## Suggestions

### Option 1

If this is desired behaviour, then perhaps just adding a callout:

````diff
### Using `#import` Expression

+<Callout type="warning">
+  at least one `#import` directive must be the first non-blank in a file
+</Callout>
+
Assume the following directory structure:

```text

Option 2

Since the extractImportLines function will happily extract import expressions from anywhere in the file, you could conceivably loosen the restriction defined in isGraphQLImportFile and simply let it check whether a file has import expressions on any line

Note

This does require searching a (potentially) much longer string

If performance is a concern, you could always retain the current first-line logic as an early-out check.
(and potentially include a note in the documentation along the lines of "if you are working with very large graphql files and performance is a concern
you can regain some speed by ensuring at least one of your import expressions is at the top of the file")

Option 3

Add validation for import lines - this doesn't actually solve the key issue (the validation would still only run if an import line appears at the top) but perhaps if combined with Option 2 this would reduce some headaches

Option 4

Do neither of the above and just trust that people will find this issue 😁

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions