ferry icon indicating copy to clipboard operation
ferry copied to clipboard

Fragment import question

Open michael-woolsey opened this issue 3 years ago • 2 comments

Hello,

We have a lot of queries, mutations and fragments, so I was splitting them off into separate .graphql files so that the codegen doesn't choke on the large files (which it seems to) and so that my team and I have an easier time working with them (which is just as important).

Here is a simplified example, in reality fragments cross feature sets:

  • settings.fragments.graphql (Contains 5 fragments)
  • settings.mutations.graphql (Calls 3 of the fragments)
  • settings.queries.graphql (Calls all 5 of the fragments)

I found that when I import (ex: # import 'settings.fragments.graphql') the fragment file I get the following error when running my app. The code gen completes without issue, this only occurs when talking to the graphql server.

  • GraphQLError(message: Fragment "xyz" is never used.

Since settings.mutations.graphql doesn't use 2 out of the 5 fragments, graphql is complaining about an unused fragment.

Is there a way to do something like import { MyFragment1, MyFragment2 } from 'query.gql' (Source: https://github.com/apollographql/graphql-tag/pull/257/files) so that I can reference the specific fragment without creating a bunch of additional files to reuse my shared fragments?

I've tried, and none seem to work as the codegen complains about a missing fragment:

  • import { MyFragment1, MyFragment2 } from 'settings.fragments.graphql'
  • import MyFragment1, MyFragment2 from 'settings.fragments.graphql'
  • import MyFragment1, MyFragment2 'settings.fragments.graphql'
  • import MyFragment1, MyFragment2 './settings.fragments.graphql'

If anyone knows if this can be done I would appreciate it!

Thanks in advance!

michael-woolsey avatar Mar 08 '22 17:03 michael-woolsey

I don't know why this problem occurs but a quick and simple workaround is making a query with all fragements and fragment parameters. It is not perfect or clean, but it works. Would be great if a ferry developer could tell why this is a problem at all.

Hope this helps in the meantime.

bjoernpy avatar Nov 18 '22 14:11 bjoernpy

Would be great if a ferry developer could tell why this is a problem at all.

That would probably be me, I took over as maintainer recently.

Right now, the client includes all fragments of all referenced file of a request. This is obvious not ideal, but it also would not be a trivial fix and I don't think I will be able to fix it in the short term. Pull request are welcomed though.

As a workaround, I recommend putting each fragment in a separate file for now.

knaeckeKami avatar Nov 18 '22 15:11 knaeckeKami