Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sbstnerhrdt/gql_auto
generate graphql objects from golang structs
https://github.com/sbstnerhrdt/gql_auto
autogenerate go golang gql graphql graphql-go
Last synced: about 1 month ago
JSON representation
generate graphql objects from golang structs
- Host: GitHub
- URL: https://github.com/sbstnerhrdt/gql_auto
- Owner: SbstnErhrdt
- License: mit
- Created: 2023-05-18T16:33:27.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-28T12:15:55.000Z (about 1 year ago)
- Last Synced: 2023-09-28T14:37:16.654Z (about 1 year ago)
- Topics: autogenerate, go, golang, gql, graphql, graphql-go
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang GrahpQL Automatic Schema Generation
This repository contains a library to auto generate a GraphQL schemas based on golang structs.
It based on [github.com/lab259/go-graphql-struct](https://github.com/lab259/go-graphql-struct)
and [github.com/graphql-go/graphql](https://github.com/graphql-go/graphql).Usually, building the schema is a one time task, and it is done
statically. So, this library does not degrade the performance, not even
a little, but in that one-time initialization.## Usage
```go
type Person struct {
Name string `graphql:"!name"`
Age int `graphql:"age"`
Friends []Person `graphql:"friends"`
}
```## Custom Types
The default data types of the GraphQL can be count in one hand, which is
not a bad thing. However, that means that you may need to implement some
scalar types (or event complexes types) yourself.In order to provide custom types for the fields the `GraphqlTyped`
interface was defined:```go
type GraphqlTyped interface {
GraphqlType() graphql.Type
}
```An example:
```go
type TypeA stringfunc (*TypeA) GraphqlType() graphql.Type {
return graphql.Int
}```
Remember, this library is all about declaring the schema. If you need
marshalling/unmarshaling a custom type to another, use the implementation
of the [github.com/graphql-go/graphql](https://github.com/graphql-go/graphql)
library (check on the `graphql.NewScalar` and `graphql.ScalarConfig`).## Resolver
To implement resolvers over a Custom Type, you will implement the
interface `GraphqlResolver`:```go
type GraphqlResolver interface {
GraphqlResolve(p graphql.ResolveParams) (interface{}, error)
}
```**IMPORTANT**: Although the method `GraphqlResolve` is a member of a struct, it is
called statically. So, do not make any references of the struct itself,
inside of this method.An example:
```go
type TypeA stringfunc (*TypeA) GraphqlType() graphql.Type {
return graphql.Int
}
```## License
MIT