glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.14k stars 1.06k forks source link

Nullable GraphQL scalars made non-nullable in Go #1296

Open matiasanaya opened 5 years ago

matiasanaya commented 5 years ago

What I did

Generated Go code from graphQL schema and query, using quicktype:

quicktype \
  --lang go \
  --src-lang graphql \
  --graphql-schema schema.graphql \
  query.graphql

What I expected

Nullable GraphQL fields to be nullable in the generated Go code.

What happened

Nullable scalars get defined as non-nullable in the Go code.

Source files

{
  "data": {
    "__schema": {
      "queryType": {
        "name": "QueryRoot"
      },
      "mutationType": null,
      "subscriptionType": null,
      "types": [
        {
          "kind": "SCALAR",
          "name": "String",
          "description": "Represents textual data as UTF-8 character sequences. This type is most often used by GraphQL to represent free-form human-readable text."
        },
        {
          "kind": "OBJECT",
          "name": "QueryRoot",
          "description": "The schema's entry-point for queries.",
          "fields": [
            {
              "name": "entry",
              "description": "the only query",
              "args": [],
              "type": {
                "kind": "OBJECT",
                "name": "EntryResponse",
                "ofType": null
              },
              "isDeprecated": false,
              "deprecationReason": null
            }
          ],
          "inputFields": null,
          "interfaces": [],
          "enumValues": null,
          "possibleTypes": null
        },
        {
          "kind": "OBJECT",
          "name": "EntryResponse",
          "description": "entry response",
          "fields": [
            {
              "name": "nullable",
              "description": "nullable scalar",
              "args": [],
              "type": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              },
              "isDeprecated": false,
              "deprecationReason": null
            },
            {
              "name": "non_nullable",
              "description": "non nullable scalar",
              "args": [],
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                }
              },
              "isDeprecated": false,
              "deprecationReason": null
            }
          ],
          "inputFields": null,
          "interfaces": [],
          "enumValues": null,
          "possibleTypes": null
        }
      ]
    }
  }
}
{
  entry {
    non_nullable
    nullable
  }
}

Output

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
//    query, err := UnmarshalQuery(bytes)
//    bytes, err = query.Marshal()

package main

import "encoding/json"

func UnmarshalQuery(data []byte) (Query, error) {
    var r Query
    err := json.Unmarshal(data, &r)
    return r, err
}

func (r *Query) Marshal() ([]byte, error) {
    return json.Marshal(r)
}

type Query struct {
    Data   *Data   `json:"data"`
    Errors []Error `json:"errors"`
}

type Data struct {
    Entry *EntryResponse `json:"entry"`
}

type EntryResponse struct {
    NonNullable string `json:"non_nullable"`
    Nullable    string `json:"nullable"`
}

type Error struct {
    Message string `json:"message"`
}

Expected output

type EntryResponse struct {
    NonNullable string `json:"non_nullable"`
    Nullable    *string `json:"nullable"`
}
matiasanaya commented 5 years ago

Related to https://github.com/quicktype/quicktype/issues/1074