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

Codegen does not work with fragments from external package #1388

Open
prztrz opened this issue Jul 12, 2019 · 2 comments
Open

Codegen does not work with fragments from external package #1388

prztrz opened this issue Jul 12, 2019 · 2 comments
Labels
🤖 component - codegen related to the codegen core packages

Comments

@prztrz
Copy link

prztrz commented Jul 12, 2019

The issue is apollo codegen throw UnkonwFragment error if fragment is imported from other package.

Intended outcome:

One of my monorepo packages exports fragment field on exported react component like here:

// monorepo/packages/Email/src/index.tsx
import gql from 'graphql-tag';

interface Props {
  data: Data // generated as expected by codegen
}

const Email = ({data}: Props) => <span>email is {data.email}</span>

Email.fragment = gql`
  fragment Email on Query {
    __typename
    email
  }
`

export default Email

so after building (just ts compiler) I want to use this package:

 // monorepo/demo/src/index.tsx
import gql from 'graphql-tag';
import Email from 'email'

const App = ({data}) => <Email data={data} />

App.fragment = gql`
  fragment App on Query {
    ...Email
  }
  ${Email.fragment}
`

I expected that runing apollo codegen in application will generate proper type for data prop in App. Regardless this code works fine and data is fetched exactly as expected.

Actual outcome:
Instead of proper code generation i got `unkown fragment error:

.../monorepo/demo/src/index.tsx: Unknown fragment "EmailPreview_data".
{ ToolError: Validation of GraphQL query document failed

Versions

  "apollo": "^2.12.3",
  "graphql": "~14.2.1",
  "graphql-tag": "^2.10.1",
   "react-apollo": "^2.5.6"
@JakeDawkins JakeDawkins added the 🤖 component - codegen related to the codegen core packages label Jul 12, 2019
@dyatko
Copy link

dyatko commented Mar 20, 2020

Run into the same issue. Any solutions?

@KirioXX
Copy link

KirioXX commented Apr 15, 2020

I found two options to get this working.
Apollo codegen can't find the fragments schema because it's not in the search path.

  1. Option: You move codegen to the root of your project and let it generate the type definitions for all packages.
  2. Option: You add the fragments to your includes.

I went with the second option because I still want the global types separated for each package.
This is my apollo config for the app that wants to import the fragments:

module.exports = {
  client: {
    addTypename: true,
    includes: [
      './App/**/*.{ts,tsx,js,jsx,graphql}',
      './node_modules/mypackage/src/**/*.{ts,tsx,js,jsx,graphql}',
    ],
    exclude: ['./**/*.mocks.{ts,tsx,js,jsx,graphql}'],
    },
  },
};

The only caveat is now that it generates the types for my includes package schemas too every time I run codegen on the app. Nice would be an option like external schemas where it's posible to import external schema defenitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 component - codegen related to the codegen core packages
Projects
None yet
Development

No branches or pull requests

4 participants