apollographql / persistgraphql

A build tool for GraphQL projects.
MIT License
425 stars 57 forks source link

Mixing .graphql files with tagged template literals #50

Open mergebandit opened 7 years ago

mergebandit commented 7 years ago

Working on a project that uses primarily the gql tagged template literal for defining queries, but .graphql|.gql files for defining fragments.

Currently persistgraphql seems to be an either-or proposition. If you pass in the --js --extension=js arguments, the fragments aren't in the docMap, and as a result the following throws

extractFromAST

export function isOperationDefinition(defn: DefinitionNode): defn is OperationDefinitionNode {
  return (defn.kind === 'OperationDefinition');
}

Trace:

TypeError: Cannot read property 'kind' of undefined
    at isOperationDefinition (/Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/extractFromAST.js:5:17)
    at Array.filter (<anonymous>)
    at Object.getOperationDefinitions (/Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/extractFromAST.js:46:28)
    at ExtractGQL.createMapFromDocument (/Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/ExtractGQL.js:70:49)
    at /Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/ExtractGQL.js:99:26
    at Array.map (<anonymous>)
    at ExtractGQL.createOutputMapFromString (/Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/ExtractGQL.js:95:46)
    at /Users/mergebandit/dev/super-top-secret-but-totes-badass-project/node_modules/persistgraphql/lib/src/ExtractGQL.js:132:31
    at <anonymous>

Seems to me that, if you choose the .js argument, it should still be able to extract .graphql files and merge them into the output document mapping.

mergebandit commented 7 years ago

Fixed this locally. I'll submit a PR for you guys to tear apart and abuse me for :-D