github.com/mstephano/gqlgen-schemagen@v0.0.0-20230113041936-dd2cd4ea46aa/integration/remote_api/user.go (about)

     1  package remote_api
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strconv"
     7  
     8  	"github.com/mstephano/gqlgen-schemagen/graphql"
     9  	"github.com/mstephano/gqlgen-schemagen/integration/testomitempty"
    10  )
    11  
    12  type User struct {
    13  	Name  string
    14  	Likes []string
    15  }
    16  
    17  type DummyUserWithRemoteNamedBasics struct {
    18  	NewString  testomitempty.NamedString  `json:"newString"`
    19  	NewInt     testomitempty.NamedInt     `json:"newInt"`
    20  	NewInt8    testomitempty.NamedInt8    `json:"newInt8"`
    21  	NewInt16   testomitempty.NamedInt16   `json:"newInt16"`
    22  	NewInt32   testomitempty.NamedInt32   `json:"newInt32"`
    23  	NewInt64   testomitempty.NamedInt64   `json:"newInt64"`
    24  	NewBool    testomitempty.NamedBool    `json:"newBool"`
    25  	NewFloat32 testomitempty.NamedFloat32 `json:"newFloat32"`
    26  	NewFloat64 testomitempty.NamedFloat64 `json:"newFloat64"`
    27  	NewUint    testomitempty.NamedUint    `json:"newUint"`
    28  	NewUint8   testomitempty.NamedUint8   `json:"newUint8"`
    29  	NewUint16  testomitempty.NamedUint16  `json:"newUint16"`
    30  	NewUint32  testomitempty.NamedUint32  `json:"newUint32"`
    31  	NewUint64  testomitempty.NamedUint64  `json:"newUint64"`
    32  	NewID      testomitempty.NamedID      `json:"newID"`
    33  }
    34  
    35  // Lets redefine the base Uint type to use an id from an external library
    36  func MarshalUint(id testomitempty.NamedUint) graphql.Marshaler {
    37  	return graphql.WriterFunc(func(w io.Writer) {
    38  		io.WriteString(w, strconv.Quote(fmt.Sprintf("=%d=", id)))
    39  	})
    40  }
    41  
    42  // And the same for the unmarshaler
    43  func UnmarshalUint(v interface{}) (testomitempty.NamedUint, error) {
    44  	str, ok := v.(string)
    45  	if !ok {
    46  		return 0, fmt.Errorf("ids must be strings")
    47  	}
    48  	i, err := strconv.Atoi(str[1 : len(str)-1])
    49  	return testomitempty.NamedUint(i), err
    50  }
    51  
    52  // Lets redefine the base ID type to use an id from an external library
    53  func MarshalID(id testomitempty.NamedID) graphql.Marshaler {
    54  	return graphql.WriterFunc(func(w io.Writer) {
    55  		io.WriteString(w, strconv.Quote(fmt.Sprintf("=%d=", id)))
    56  	})
    57  }
    58  
    59  // And the same for the unmarshaler
    60  func UnmarshalID(v interface{}) (testomitempty.NamedID, error) {
    61  	str, ok := v.(string)
    62  	if !ok {
    63  		return 0, fmt.Errorf("ids must be strings")
    64  	}
    65  	i, err := strconv.Atoi(str[1 : len(str)-1])
    66  	return testomitempty.NamedID(i), err
    67  }