Skip to content
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

Error when var is used in fragment only #25

Open
Yagger opened this issue Jan 18, 2023 · 1 comment
Open

Error when var is used in fragment only #25

Yagger opened this issue Jan 18, 2023 · 1 comment

Comments

@Yagger
Copy link

Yagger commented Jan 18, 2023

Error happens, when var is used in a fragment, but not on the top level

Steps to reproduce:

  1. initialize with Anilist public graph API elm-gql init https://graphql.anilist.co
  2. create the following query
query ($search: String!) {
  Page {
    ...foo
  }
}

fragment foo on Page {
  characters(search: $search) {
    name {
      full
    }
  }
}
  1. run elm-gql run ./api/Api/schema.json
  2. observe the following error in the log
--ELM GQL------------------------------------------------------src/myQuery.gql--

I found the following variables:

    $search

But I ran into an issue:

    $search is unused.

Here's what I think the variables should be:


@georgesboris
Copy link

georgesboris commented Jun 20, 2023

I've had the same issue but the error thrown was a bit misleading. May be worth looking into as well.

I had a query like this:

query ($topVar: String!, $fieldVar: String!) {
  topLevel(topVar: $topVar) {
    ...Record
  }
}

fragment Record on Record {
  fragmentQuery(fragmentVar: $fragmentVar) {
    fieldWithVar(fieldVar: $fieldVar) {
      result
    }
    subRecord {
      ...SubRecord
    }
  }
}

fragment SubRecord on SubRecord {
  fieldA
  fieldB
}

The error thrown was "can't find fragment on type SubRecord".
If I re-run elm-gql the error was ignored but the file was unchanged.
Sometimes it complained about other (valid) fragments but most times it complained about the type nested alongside the fragment that used a variable.

To find the actual error, I inlined the SubRecord fragment into Record - then I got the unused variable error.

query ($topVar: String!, $fieldVar: String!) {
  topLevel(topVar: $topVar) {
    ...Record
  }
}

fragment Record on Record {
  fragmentQuery(fragmentVar: $fragmentVar) {
    fieldWithVar(fieldVar: $fieldVar) {
      result
    }
    subRecord {
      fieldA
      fieldB
    }
  }
}

Finally, I inlined Record so the variable is used in the same scope and re-inserted SubRecord as a fragment and everything worked.

query ($topVar: String!, $fieldVar: String!) {
  topLevel(topVar: $topVar) {
    fieldWithVar(fieldVar: $fieldVar) {
      result
    }
    subRecord {
      ...SubRecord
    }
  }
}

fragment SubRecord on SubRecord {
  fieldA
  fieldB
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants