github.com/Desuuuu/genqlient@v0.5.3/generate/testdata/snapshots/TestGenerate-CustomMarshal.graphql-CustomMarshal.graphql.go (about)

     1  // Code generated by github.com/Desuuuu/genqlient, DO NOT EDIT.
     2  
     3  package test
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/Desuuuu/genqlient/graphql"
    11  	"github.com/Desuuuu/genqlient/internal/testutil"
    12  )
    13  
    14  // CustomMarshalResponse is returned by CustomMarshal on success.
    15  type CustomMarshalResponse struct {
    16  	UsersBornOn []CustomMarshalUsersBornOnUser `json:"usersBornOn"`
    17  }
    18  
    19  // GetUsersBornOn returns CustomMarshalResponse.UsersBornOn, and is useful for accessing the field via an interface.
    20  func (v *CustomMarshalResponse) GetUsersBornOn() []CustomMarshalUsersBornOnUser { return v.UsersBornOn }
    21  
    22  // CustomMarshalUsersBornOnUser includes the requested fields of the GraphQL type User.
    23  // The GraphQL type's documentation follows.
    24  //
    25  // A User is a user!
    26  type CustomMarshalUsersBornOnUser struct {
    27  	// id is the user's ID.
    28  	//
    29  	// It is stable, unique, and opaque, like all good IDs.
    30  	Id        testutil.ID `json:"id"`
    31  	Birthdate time.Time   `json:"-"`
    32  }
    33  
    34  // GetId returns CustomMarshalUsersBornOnUser.Id, and is useful for accessing the field via an interface.
    35  func (v *CustomMarshalUsersBornOnUser) GetId() testutil.ID { return v.Id }
    36  
    37  // GetBirthdate returns CustomMarshalUsersBornOnUser.Birthdate, and is useful for accessing the field via an interface.
    38  func (v *CustomMarshalUsersBornOnUser) GetBirthdate() time.Time { return v.Birthdate }
    39  
    40  func (v *CustomMarshalUsersBornOnUser) UnmarshalJSON(b []byte) error {
    41  
    42  	if string(b) == "null" {
    43  		return nil
    44  	}
    45  
    46  	var firstPass struct {
    47  		*CustomMarshalUsersBornOnUser
    48  		Birthdate json.RawMessage `json:"birthdate"`
    49  		graphql.NoUnmarshalJSON
    50  	}
    51  	firstPass.CustomMarshalUsersBornOnUser = v
    52  
    53  	err := json.Unmarshal(b, &firstPass)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	{
    59  		dst := &v.Birthdate
    60  		src := firstPass.Birthdate
    61  		if len(src) != 0 && string(src) != "null" {
    62  			err = testutil.UnmarshalDate(
    63  				src, dst)
    64  			if err != nil {
    65  				return fmt.Errorf(
    66  					"Unable to unmarshal CustomMarshalUsersBornOnUser.Birthdate: %w", err)
    67  			}
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  type __premarshalCustomMarshalUsersBornOnUser struct {
    74  	Id testutil.ID `json:"id"`
    75  
    76  	Birthdate json.RawMessage `json:"birthdate"`
    77  }
    78  
    79  func (v *CustomMarshalUsersBornOnUser) MarshalJSON() ([]byte, error) {
    80  	premarshaled, err := v.__premarshalJSON()
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return json.Marshal(premarshaled)
    85  }
    86  
    87  func (v *CustomMarshalUsersBornOnUser) __premarshalJSON() (*__premarshalCustomMarshalUsersBornOnUser, error) {
    88  	var retval __premarshalCustomMarshalUsersBornOnUser
    89  
    90  	retval.Id = v.Id
    91  	{
    92  
    93  		dst := &retval.Birthdate
    94  		src := v.Birthdate
    95  		var err error
    96  		*dst, err = testutil.MarshalDate(
    97  			&src)
    98  		if err != nil {
    99  			return nil, fmt.Errorf(
   100  				"Unable to marshal CustomMarshalUsersBornOnUser.Birthdate: %w", err)
   101  		}
   102  	}
   103  	return &retval, nil
   104  }
   105  
   106  // __CustomMarshalInput is used internally by genqlient
   107  type __CustomMarshalInput struct {
   108  	Date time.Time `json:"-"`
   109  }
   110  
   111  // GetDate returns __CustomMarshalInput.Date, and is useful for accessing the field via an interface.
   112  func (v *__CustomMarshalInput) GetDate() time.Time { return v.Date }
   113  
   114  func (v *__CustomMarshalInput) UnmarshalJSON(b []byte) error {
   115  
   116  	if string(b) == "null" {
   117  		return nil
   118  	}
   119  
   120  	var firstPass struct {
   121  		*__CustomMarshalInput
   122  		Date json.RawMessage `json:"date"`
   123  		graphql.NoUnmarshalJSON
   124  	}
   125  	firstPass.__CustomMarshalInput = v
   126  
   127  	err := json.Unmarshal(b, &firstPass)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	{
   133  		dst := &v.Date
   134  		src := firstPass.Date
   135  		if len(src) != 0 && string(src) != "null" {
   136  			err = testutil.UnmarshalDate(
   137  				src, dst)
   138  			if err != nil {
   139  				return fmt.Errorf(
   140  					"Unable to unmarshal __CustomMarshalInput.Date: %w", err)
   141  			}
   142  		}
   143  	}
   144  	return nil
   145  }
   146  
   147  type __premarshal__CustomMarshalInput struct {
   148  	Date json.RawMessage `json:"date"`
   149  }
   150  
   151  func (v *__CustomMarshalInput) MarshalJSON() ([]byte, error) {
   152  	premarshaled, err := v.__premarshalJSON()
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  	return json.Marshal(premarshaled)
   157  }
   158  
   159  func (v *__CustomMarshalInput) __premarshalJSON() (*__premarshal__CustomMarshalInput, error) {
   160  	var retval __premarshal__CustomMarshalInput
   161  
   162  	{
   163  
   164  		dst := &retval.Date
   165  		src := v.Date
   166  		var err error
   167  		*dst, err = testutil.MarshalDate(
   168  			&src)
   169  		if err != nil {
   170  			return nil, fmt.Errorf(
   171  				"Unable to marshal __CustomMarshalInput.Date: %w", err)
   172  		}
   173  	}
   174  	return &retval, nil
   175  }
   176  
   177  func CustomMarshal(
   178  	client graphql.Client,
   179  	date time.Time,
   180  ) (*CustomMarshalResponse, error) {
   181  	req := &graphql.Request{
   182  		OpName: "CustomMarshal",
   183  		Query: `
   184  query CustomMarshal ($date: Date!) {
   185  	usersBornOn(date: $date) {
   186  		id
   187  		birthdate
   188  	}
   189  }
   190  `,
   191  		Variables: &__CustomMarshalInput{
   192  			Date: date,
   193  		},
   194  	}
   195  	var err error
   196  
   197  	var data CustomMarshalResponse
   198  	resp := &graphql.Response{Data: &data}
   199  
   200  	err = client.MakeRequest(
   201  		nil,
   202  		req,
   203  		resp,
   204  	)
   205  
   206  	return &data, err
   207  }
   208