github.com/nathanstitt/genqlient@v0.3.1-0.20211028004951-a2bda3c41ab8/generate/parse.go (about) 1 package generate 2 3 import ( 4 "fmt" 5 goAst "go/ast" 6 goParser "go/parser" 7 goToken "go/token" 8 "io/ioutil" 9 "path/filepath" 10 "strconv" 11 "strings" 12 13 "github.com/vektah/gqlparser/v2" 14 "github.com/vektah/gqlparser/v2/ast" 15 "github.com/vektah/gqlparser/v2/gqlerror" 16 "github.com/vektah/gqlparser/v2/parser" 17 "github.com/vektah/gqlparser/v2/validator" 18 ) 19 20 func getSchema(globs StringList) (*ast.Schema, error) { 21 filenames, err := expandFilenames(globs) 22 if err != nil { 23 return nil, err 24 } 25 26 sources := make([]*ast.Source, len(filenames)) 27 for i, filename := range filenames { 28 text, err := ioutil.ReadFile(filename) 29 if err != nil { 30 return nil, errorf(nil, "unreadable schema file %v: %v", filename, err) 31 } 32 sources[i] = &ast.Source{Name: filename, Input: string(text)} 33 } 34 35 // Multi step schema validation 36 // Step 1 assume schema implicitly declares types that are required by the graphql spec 37 // Step 2 assume schema explicitly declares types that are required by the graphql spec 38 var ( 39 schema *ast.Schema 40 graphqlError *gqlerror.Error 41 ) 42 schema, graphqlError = gqlparser.LoadSchema(sources...) 43 if graphqlError != nil { 44 schema, graphqlError = validator.LoadSchema(sources...) 45 if graphqlError != nil { 46 return nil, errorf(nil, "invalid schema: %v", graphqlError) 47 } 48 } 49 50 return schema, nil 51 } 52 53 func getAndValidateQueries(basedir string, filenames StringList, schema *ast.Schema) (*ast.QueryDocument, error) { 54 queryDoc, err := getQueries(basedir, filenames) 55 if err != nil { 56 return nil, err 57 } 58 59 // Cf. gqlparser.LoadQuery 60 graphqlErrors := validator.Validate(schema, queryDoc) 61 if graphqlErrors != nil { 62 return nil, errorf(nil, "query-spec does not match schema: %v", graphqlErrors) 63 } 64 65 return queryDoc, nil 66 } 67 68 func expandFilenames(globs []string) ([]string, error) { 69 uniqFilenames := make(map[string]bool, len(globs)) 70 for _, glob := range globs { 71 matches, err := filepath.Glob(glob) 72 if err != nil { 73 return nil, errorf(nil, "can't expand file-glob %v: %v", glob, err) 74 } 75 for _, match := range matches { 76 uniqFilenames[match] = true 77 } 78 } 79 filenames := make([]string, 0, len(uniqFilenames)) 80 for filename := range uniqFilenames { 81 filenames = append(filenames, filename) 82 } 83 return filenames, nil 84 } 85 86 func getQueries(basedir string, globs StringList) (*ast.QueryDocument, error) { 87 // We merge all the queries into a single query-document, since operations 88 // in one might reference fragments in another. 89 // 90 // TODO(benkraft): It might be better to merge just within a filename, so 91 // that fragment-names don't need to be unique across files. (Although 92 // then we may have other problems; and query-names still need to be.) 93 mergedQueryDoc := new(ast.QueryDocument) 94 addQueryDoc := func(queryDoc *ast.QueryDocument) { 95 mergedQueryDoc.Operations = append(mergedQueryDoc.Operations, queryDoc.Operations...) 96 mergedQueryDoc.Fragments = append(mergedQueryDoc.Fragments, queryDoc.Fragments...) 97 } 98 99 filenames, err := expandFilenames(globs) 100 if err != nil { 101 return nil, err 102 } 103 104 for _, filename := range filenames { 105 text, err := ioutil.ReadFile(filename) 106 if err != nil { 107 return nil, errorf(nil, "unreadable query-spec file %v: %v", filename, err) 108 } 109 110 switch filepath.Ext(filename) { 111 case ".graphql": 112 queryDoc, err := getQueriesFromString(string(text), basedir, filename) 113 if err != nil { 114 return nil, err 115 } 116 117 addQueryDoc(queryDoc) 118 119 case ".go": 120 queryDocs, err := getQueriesFromGo(string(text), basedir, filename) 121 if err != nil { 122 return nil, err 123 } 124 125 for _, queryDoc := range queryDocs { 126 addQueryDoc(queryDoc) 127 } 128 129 default: 130 return nil, errorf(nil, "unknown file type: %v", filename) 131 } 132 } 133 134 return mergedQueryDoc, nil 135 } 136 137 func getQueriesFromString(text string, basedir, filename string) (*ast.QueryDocument, error) { 138 // make path relative to the config-directory 139 relname, err := filepath.Rel(basedir, filename) 140 if err == nil { 141 filename = relname 142 } 143 144 // Cf. gqlparser.LoadQuery 145 document, graphqlError := parser.ParseQuery( 146 &ast.Source{Name: filename, Input: text}) 147 if graphqlError != nil { // ParseQuery returns type *graphql.Error, yuck 148 return nil, errorf(nil, "invalid query-spec file %v: %v", filename, graphqlError) 149 } 150 151 return document, nil 152 } 153 154 func getQueriesFromGo(text string, basedir, filename string) ([]*ast.QueryDocument, error) { 155 fset := goToken.NewFileSet() 156 f, err := goParser.ParseFile(fset, filename, text, 0) 157 if err != nil { 158 return nil, errorf(nil, "invalid Go file %v: %v", filename, err) 159 } 160 161 var retval []*ast.QueryDocument 162 goAst.Inspect(f, func(node goAst.Node) bool { 163 if err != nil { 164 return false // don't bother to recurse if something already failed 165 } 166 167 basicLit, ok := node.(*goAst.BasicLit) 168 if !ok || basicLit.Kind != goToken.STRING { 169 return true // recurse 170 } 171 172 var value string 173 value, err = strconv.Unquote(basicLit.Value) 174 if err != nil { 175 return false 176 } 177 178 if !strings.HasPrefix(strings.TrimSpace(value), "# @genqlient") { 179 return true 180 } 181 182 // We put the filename as <real filename>:<line>, which errors.go knows 183 // how to parse back out (since it's what gqlparser will give to us in 184 // our errors). 185 pos := fset.Position(basicLit.Pos()) 186 fakeFilename := fmt.Sprintf("%v:%v", pos.Filename, pos.Line) 187 var query *ast.QueryDocument 188 query, err = getQueriesFromString(value, basedir, fakeFilename) 189 if err != nil { 190 return false 191 } 192 retval = append(retval, query) 193 194 return true 195 }) 196 197 return retval, err 198 }