ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 815 forks source link

Fragment-only files cause GraphQLDocumentError #1983

Closed chriskrycho closed 3 years ago

chriskrycho commented 4 years ago

Reproduction:

On the graphql-code-generator website, choose the Operations Types (near-operation-file) demo and delete the query from operation.graphql, so that the file's body is simply:

fragment UserFields on User {
  id
  username
  role
}

Expected: operation.generated.tsx should have (something like)—

import * as Types from './types';

import { gql } from '@apollo/client';
export type FindUserQueryVariables = Types.Exact<{
  userId: Types.Scalars['ID'];
}>;

export type UserFieldsFragment = (
  { __typename?: 'User' }
  & Pick<Types.User, 'id' | 'username' | 'role'>
);

export const UserFieldsFragmentDoc = gql`
    fragment UserFields on User {
  id
  username
  role
}
    `;

Actual: it has an error:


    GraphQLDocumentError: Fragment "UserFields" is never used.
        at operation.graphql:1:1

Why it matters: it's useful to be able to programmatically generate fragment-only types for use with components, to be imported e.g. via #import. Fragment-only documents should be allowed!

Related: #1532

I will likely have a PR up for this sooner rather than later; if you have initial context or input, please let me know!

ardatan commented 3 years ago

I think this is an issue on codegen side because it checks if fragments are used or not. So closing it for now. It'd be better to track this on Codegen repo.