11import * as path from 'path'
22import { PluginFunction } from '@graphql-codegen/plugin-helpers' ;
3+ import { OperationDefinitionNode } from "graphql"
4+ import defineProperty = Reflect . defineProperty
35
46export const plugin : PluginFunction = async ( schema , documents , config , info ) => {
5- const mappedDocuments = documents . reduce ( ( previous , document ) => {
7+ const mappedDocuments : { [ key : string ] : OperationDefinitionNode [ ] } = documents . reduce ( ( previous , document ) => {
68 const fileName = document . filePath ;
79
8- if ( ! previous [ fileName ] )
9- previous [ fileName ] = [ ] ;
10+ const definitions = document . content . definitions . filter ( ( document ) => document . kind === 'OperationDefinition' )
1011
11- previous [ fileName ] . push ( ...document . content . definitions
12- . filter ( ( document ) => document . kind === 'OperationDefinition' ) ) ;
12+ if ( definitions . length > 0 ) {
13+ if ( ! previous [ fileName ] )
14+ previous [ fileName ] = [ ] ;
15+
16+ previous [ fileName ] . push ( ...definitions ) ;
17+ }
1318
1419 return previous ;
1520 } , { } ) ;
@@ -26,33 +31,28 @@ export const plugin: PluginFunction = async (schema, documents, config, info) =>
2631 const operationsImport = operations . map ( op => op . name . value ) . join ( ',' )
2732
2833 return `import {${ operationsImport } } from "${ relativePath } "`
29- } ) . join ( '\n' ) + '\n' + "import {DocumentNode} from 'graphql'"
30-
31-
32- const definition = Object . keys ( mappedDocuments )
33- . map ( documentFile => {
34- const operations = mappedDocuments [ documentFile ]
34+ } ) . join ( '\n' )
3535
36- return operations . map ( value => `${ value . name . value } : ${ value . name . value } ` ) . join ( ',\n' )
37- } ) . join ( ',\n' )
3836
39- const typeDefinitionMap = Object . keys ( mappedDocuments )
40- . map ( documentFile => {
41- const operations = mappedDocuments [ documentFile ]
37+ const definition = Object . keys ( mappedDocuments ) . map ( val => mappedDocuments [ val ] )
38+ . reduce ( ( previous , definitions ) => {
39+ definitions . forEach ( definition => {
40+ previous [ definition . operation ] . push ( `${ definition . name . value } : ${ definition . name . value } ` )
41+ } )
4242
43- return operations . map ( value => ` ${ value . name . value } : DocumentNode` ) . join ( ',\n' )
44- } ) . join ( ',\n' )
43+ return previous
44+ } , { query : [ ] , mutation : [ ] , subscription : [ ] } )
4545
4646 const injectDefinition = `export const GraphQLInjectDefinition = {
47- ${ definition }
47+ queries: {${ definition [ 'query' ] . join ( ',\n' ) } },
48+ mutations: {${ definition [ 'mutation' ] . join ( ',\n' ) } },
49+ subscriptions: {${ definition [ 'subscription' ] . join ( ',\n' ) } }
4850 }`
4951
5052 const typeDefinition = `declare module 'vue/types/vue' {
5153 interface Vue {
5254 $gql(): {
53- operations: {
54- ${ typeDefinitionMap }
55- }
55+ operations: typeof GraphQLInjectDefinition
5656 }
5757 }
5858 }`
0 commit comments