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.

How to declare fragment in functional component in Typescript?

See original GitHub issue

I can easily declare a fragment in a class component by adding a static method.

export default class PokemonCard extends React.Component {

  static fragments = {
    pokemon: gql`
      fragment PokemonCardPokemon on Pokemon {
        url
        name
      }
    `
  }
  // ...
}

What is the best way to do the same in functional component in Typescript and pass the type checking?

PokemonCard.fragments = {
  pokemon: gql`
    fragment PokemonCardPokemon on Pokemon {
      url
      name
    }
  `,
};
screen shot 2017-12-13 at 11 00 24 pm

// error: ‘fragments’ does not exist on type ‘typeof PokemonCard’

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
astorijecommented, May 19, 2018

Yeah, we ended up doing pretty much what @danielrasmuson described, except we colocate the fragment with the component, and use default and non-default exports to use this pattern:

// In PokemonCard.tsx:

const PokemonCard: SFC = () => <span />;

export default PokemonCard;

export const PokemonCardFragment = gql`
  fragment PokemonCardPokemon on Pokemon {
    id
    url
    name
  }
`;

// In the caller file:

import PokemonCard, { PokemonCardFragment } from './PokemonCard'; // <-- This is the main difference in usage

const GET_POKEMON_CARD_QUERY = gql`
  query getPokemonCard($id: ID!) {
    ...PokemonCardPokemon
  }
  ${PokemonCardFragment}
`;
6reactions
danrasmusoncommented, Dec 25, 2017

My understanding (feedback welcome) is that doing ReactComponent.fragments is just a convention.

We put the fragment on CommentsPage.fragments.comment by convention, and use the familiar gql helper to create it.

I’m using typescript and I’ve decided to opt out of using fragments this way for exactly the reason you detailed above.

Instead I have a file devoted to a fragment…

file: queries/pokemon-card.fragment.tsx

export const PokemonCardFragment = gql`
    fragment PokemonCardPokemon on Pokemon {
      id
      url
      name
    }
`

Then in your query…

const getPokemonCardQuery = gql`
  query getPokemonCard($id:ID!){
     ...PokemonCardPokemon
  }
  ${PokemonCardFragment}
`

Lastly pro tip! Include id in your fragment query and anytime that fragment is fetched it will reload all components that use that fragment data.

I.E. if you have some mutation

mutation addCard($name: String!){
   addPokemonCard(name: $name){
      ... PokemonCardPokemon
   },
   ${PokemonCardFragment}
}

And as long as your mutation returns PokemonCardPokemon it will reload all components using that information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react 16 fragment functional component and typescript TS2605
When I write a functional component that returns a fragment: function MyComponent() { return [<div>1</div>, <div>2</div>]; }. Typescript ...
Read more >
JSX Fragment Syntax in TypeScript - Marius Schulz
The compiler replaces the short fragment syntax by a call to the React.createElement() method and passes it React.Fragment as the first argument ...
Read more >
Fragments - React
Fragments let you group a list of children without adding extra nodes to the DOM. There is also a new short syntax for...
Read more >
React with TypeScript: Components as Function Declarations ...
When using React with Typescript, let's look at using components as function declarations vs. function expressions.
Read more >
Documentation - JSX - TypeScript
If the process succeeds, then TS finishes resolving the expression to its declaration. If the value fails to resolve as a Function Component, ......
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