Skip to content

graphql-tag-pluck inline fragment support #5140

@louy

Description

@louy

Is your feature request related to a problem? Please describe.
I'm trying to create a graphql-codegen plugin that supports the use of fragments inline with what graphql-tag supports.

The idea is: You can declare a fragment, and embed it in a tagged template literal, and that fragment will be inlined.

# Using rickandmortyapi as an example

# File 1
export const CharacterFragment = gql`
  fragment CharacterFragment on Character {
    id name created
  }
`

# File 2
const query = gql`
  ${CharacterFragment}
  query CharacterListQuery($characterId: ID!) {
    character(id: $characterId) {
      name
      ...CharacterFragment
    }
  }
`

The problem: currently graphql-tag-pluck will ignore the embedded fragment in file 2, resulting in the following 2 documents (notice empty line in doc 2):

[`
  fragment CharacterFragment on Character {
    id name created
  }
`,`

  query CharacterListQuery($characterId: ID!) {
    character(id: $characterId) {
      name
      ...CharacterFragment
    }
  }
`]

The expectation: graphql-tag-pluck should expose the different chunks that make up document 2, and ideally embed the value of ${CharacterFragment} in the exported document

# Expected documents
[`
  fragment CharacterFragment on Character {
    id name created
  }
`,
[`
`, `
  fragment CharacterFragment on Character {
    id name created
  }
`, `
  query CharacterListQuery($characterId: ID!) {
    character(id: $characterId) {
      name
      ...CharacterFragment
    }
  }
`]
]

Reason: Without knowing where the different breakpoints are in the template string literal, it's impossible (or just really difficult) to generate a static tag function signature.

See this example (line 37-41) of the currently generated types by graphql-codegen.

To generate a static type of a tagged template string, we need to know the exact value of the first argument (strings), thus we need the body value of each source as a list of strings.

Describe the solution you'd like
Possibly an addition property on Source, called strings, that only exists if the source was a template literal

Ideally (but I don't know whether this is feasible) the values (as AST nodes) as well

Describe alternatives you've considered

An interpolated string containing the resolved values of fragments stitched into the graphql document source

Additional context

N/A

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