question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for Fragments to be defined in their own files.

See original GitHub issue

Feature Information

I don’t currently see a way to define GraphQL Fragments in their own files, and use (import) them within Queries. This would be great to have, as it can significantly cut down on query object/property duplication. This is a feature that Apollo has, but appears to be missing from this tool.

I am offering to take a stab at adding this feature via PR, if your team sees the value.

Solution Information

Having only been a user of your library, my (probably naive) idea to solve this would be to simply introduce a new sibling folder to the Mutation and Query folders, called Fragment.

It seems like the flow of logic would be to first check whether the Fragment is defined within the Query file first. If not, fallback to looking for the Fragment in the Fragment folder. The contents would be read, and appended to the original Query String before sending it to the server.

This might require a level of introspection into the Query that doesn’t exist yet. I don’t believe you guys currently validate that a Query has all of its dependent Fragments defined; letting the server handle that instead (correct?). So in order to avoid opening the door to really complicated query parsing logic (into structured data), perhaps the dependent fragments can be located via simple regex search? A similar strategy can be used to determine whether the Fragment already exists in the Query file, or if the logic should check the Fragment folder.

It seems really straight forward, but I fully acknowledge that I may be missing something. Thoughts?

An Example Scenario

Directory structure:

assets
  Mutation
  Query
    SomeQuery.graphql
  Fragment
    ObjectAFragment.graphql
    ObjectBFragment.graphql

SomeQuery.graphql:

query SomeQuery($id: ID!) {
  objectA(id: $id) {
      ... ObjectAFragment
  }
  objectB(id: $id) {
      ... ObjectBFragment
  }
}

ObjectAFragment.graphql:

fragment ObjectAFragment on ObjectA {
  id
  name
}

ObjectBFragment.graphql:

fragment ObjectBFragment on ObjectB {
  id
  name
}

Additional context

We recently switched from using Apollo to this library, and have loved it so far. We are just at the very beginning of migrating API calls over to use this library. And we have a ton of queries, and heavily use fragments that are separated out into their own files for reuse. We’d love to be able to contribute back!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
eschlenzcommented, May 30, 2019

Sounds good! I’ll get started on this. Thanks!

1reaction
eschlenzcommented, Jun 1, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and Using Fragments | CodePath Android Cliffnotes
A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library,...
Read more >
Create a fragment - Android Developers
A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input...
Read more >
Fragments - Apollo GraphQL Docs
You can declare fragments in any file of your application. The example above export s the fragment from a fragments.js file. We can...
Read more >
Creating and using code fragments - Code VBA
Fragments are files stored in a \My Documents\VBA Code\ subfolder - as an example the fragments on the right image are located in...
Read more >
Creating fragment projects - IBM
An OSGi fragment is a Java™ archive file with specific manifest headers that enable it to attach to a specified host bundle or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found